标签: winforms

C#:将派生类作为一个通用参数传递

我最近开始学习更多关于事件/代理以及类扩展的知识.

我想通过向Windows Form控件中添加一个扩展方法将我学到的东西付诸实践SetDraggable(),后者又使用a MouseDownMouseMove事件来移动控件.

一切正常,除了它只适用于特定控件 - 在我的情况下,a Button.

namespace Form_Extensions
{
    public static class Extensions
    {
        private static System.Windows.Forms.Button StubButton;
        private static Point MouseDownLocation;
        public static void SetDraggable(this System.Windows.Forms.Button b)
        {
            b.MouseDown += b_MouseDown;
            b.MouseMove += b_MouseMove;
            StubButton = b;
        }

        private static void b_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                MouseDownLocation = e.Location;
            }
        }

        static void b_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                StubButton.Left …
Run Code Online (Sandbox Code Playgroud)

c# oop winforms

3
推荐指数
1
解决办法
133
查看次数

使用SSH.NET显示ProgressBar中文件上载的进度

我想在我的上传过程中显示上传过程的进度ProgressBar,这是我的按钮"上传"的代码:

private void button2_Click(object sender, EventArgs e)
{
    int Port = int.Parse(textBox2.Text);
    string Host = textBox1.Text;
    string Username = textBox3.Text;
    string Password = textBox4.Text;
    string WorkingDirectory = textBox6.Text;
    string UploadDirectory = textBox5.Text;

    FileInfo FI = new FileInfo(UploadDirectory);
    string UploadFile = FI.FullName;
    Console.WriteLine(FI.Name);
    Console.WriteLine("UploadFile" + UploadFile);

    var Client = new SftpClient(Host, Port, Username, Password);
    Client.Connect();
    if (Client.IsConnected)
    {
        var FS = new FileStream(UploadFile, FileMode.Open);
        if (FS != null)
        {
            Client.UploadFile(FS, WorkingDirectory + FI.Name, null);
            Client.Disconnect();
            Client.Dispose();
            MessageBox.Show(
                "Upload complete", …
Run Code Online (Sandbox Code Playgroud)

.net c# sftp winforms ssh.net

3
推荐指数
1
解决办法
4713
查看次数

无法将复选框添加到ToolStrip

我想在我的C#winform中添加一些复选框以及标准的工具条控件。toolstrip提供的标准控件没有问题,但是我的复选框有问题。在toolstrip中,第一个控件是combobox(toolstrip组合框),第二个控件是label(toolstrip标签)。到现在为止没有问题。我必须添加两个复选框,然后再添加tollstrip组合框。我正在添加第一个复选框,如下所示

    System.Windows.Forms.CheckBox c1 = new System.Windows.Forms.CheckBox();
    c1.CheckState = System.Windows.Forms.CheckState.Unchecked;
    System.Windows.Forms.ToolStripControlHost host = new   
    System.Windows.Forms.ToolStripControlHost(c1);
    toolStrip1.Items.Add(host);
Run Code Online (Sandbox Code Playgroud)

如果我正在运行应用程序,则可以看到带有工具条的此复选框。保存表单后,所有内容都会丢失。我甚至在cs文件中找不到c1。designer.cs文件中带有host的其他条目。我在这里想念的是什么?如何在工具条中添加这2个复选框?

c# controls toolstrip winforms

3
推荐指数
1
解决办法
2469
查看次数

如何在左侧或右侧以不同比例向MSChart添加更多Y轴

我想为图表添加3个不同比例的Y轴。

我想要一个x轴和另一个y轴。我这样做像下面的代码,但我想显示一个y轴,如我附加的第二张图片中所示。

到目前为止,我的C#代码:

   private void checkBoxUseMultipleYAxis_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBoxUseMultipleYAxis.Checked)
        {
            // Set custom chart area position
            chart1.ChartAreas["ChartArea1"].Position = new ElementPosition(25, 10, 68, 85);
            chart1.ChartAreas["ChartArea1"].InnerPlotPosition = new ElementPosition(10, 0, 90, 90);``



            // Create extra Y axis for second and third series
            CreateYAxis(chart1, chart1.ChartAreas["ChartArea1"], chart1.Series["Current"], 13, 8);
            CreateYAxis(chart1, chart1.ChartAreas["ChartArea1"], chart1.Series["Capacity"], 22, 8);
        }
        else
        {
            // Set default chart areas
            chart1.Series["Current"].ChartArea = "ChartArea1";
            chart1.Series["Capacity"].ChartArea = "ChartArea1";

            // Remove newly created series and chart areas
            while (chart1.Series.Count > 3)
            { …
Run Code Online (Sandbox Code Playgroud)

c# mschart winforms yaxis

3
推荐指数
1
解决办法
2452
查看次数

的WinForms; 检测形式从另一种形式关闭

是否可以检测从另一个表单关闭的表单.例如.如果我有一个打开subForm的mainForm,我可以在mainForm中检测到subForm已关闭并执行代码吗?

我知道我可以在subForm中创建一个事件处理程序,但这并不是我真正想要的,因为在subForm关闭之后我将要做的是在mainForm中(对mainForm的更改).

c# forms winforms

3
推荐指数
1
解决办法
2103
查看次数

尝试从ZipArchive C#读取ZipFile时出现System.MissingMethodException

我有一个C#.NET(v4.6.2)WinForms应用程序,我正在访问一个文件,该文件可能是/可能不是使用"System.IO.Compression;"创建的.zip存档.我在项目中有"System.IO.Compression"和System.IO.Compress.FileSystem"引用,在顶部使用"System.IO.Compression;",它是使用NuGet包安装程序安装的.

以下是尝试以.zip存档打开文件的代码:

      try
        {
            string extractPath = Path.GetTempFileName();
            string strGameVersion = "";
            string strProjectType = "";

            using (ZipArchive archive = ZipFile.OpenRead(OpenFilePath))
            {
                FileStream fs = new FileStream(extractPath, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (entry.FullName.Contains("ProjectData.txt"))
                    {
                        entry.ExtractToFile(Path.Combine(extractPath, entry.FullName));
                        strGameVersion = sr.ReadLine();
                        strProjectType = sr.ReadLine();
                    }
                    File.Delete(extractPath);
                }
                sr.Close();
                fs.Close();
                archive.Dispose();
            }
    }
    catch(System.IO.FileFormatException flex1)
    {
        MessageBox.Show(flex1.ToString(), "oops.", MessageBoxButtons.OK,  MessageBox.Icon.Error);
    }
Run Code Online (Sandbox Code Playgroud)

错误消息是"System.MissingMethodException:Method not found:'System.IO.Compression.ZipArchive System.IO.Compression.ZipFile.OpenRead(System.String)'." 那么我做错了什么或者根本没做什么?

.net c# winforms ziparchive

3
推荐指数
1
解决办法
3555
查看次数

在用户控件之间传递数据C#

众所周知,有些解决方案与此类似,但是我无法用它们解决问题。我有两个用户控件:

  • 第一个创建一个Report对象。
  • 第二个显示它。

我有一个链接两个控件的主窗体。

这两个控件是在DLL中创建的,并添加到主窗体中,如下所示:

//ADDS THE FIRST CONTROL TO THE PANEL CONTROL
  myDll.controlUserReport userControlA = new myDll.controlUserReport();
  panelControl1.Controls.Add(userControlA);
  userControlA.Dock = DockStyle.Left;

//ADDS THE SECOND CONTROL TO THE PANEL CONTROL
   myDll.controlDocViewer userControlB = new myDll.controlDocViewer();
   panelControl1.Controls.Add(userControlB);
   userControlB.Dock = DockStyle.Fill;
Run Code Online (Sandbox Code Playgroud)

如何将controlUserReport单击一个按钮时在第一个控件中创建的Report对象传递给另一个用户控件controlDocViewer 以显示它?

在此处输入图片说明

c# user-controls report winforms

3
推荐指数
1
解决办法
1455
查看次数

在Visual Studio 2017中使用Windows窗体应用程序?

我正在关注Udemy系列,它首先要求的是创建一个Windows窗体应用程序(使用C#).我不认为这是我的选择之一.这是因为我使用的是免费版吗?我已经安装了Windows Template Studio.

.net visual-studio winforms visual-studio-2017

3
推荐指数
2
解决办法
5645
查看次数

根据字符列表设置文本框的文本

所以我有以下形式:

在此输入图像描述

我想要实现的目标如下:

输入名称后,我希望只需单击首字母文本框即可生成名称的首字母.

我找到了以下方法来获取字符串的第一个字符:

string EngineerName = tb_Name.Text.ToString();
EngineerName.Split(' ').ToList().ForEach(i => Console.Write(i[0] + " "));
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何将字符列表分配给文本textbox

我试过了:

private void Tb_Initials_Click(object sender, EventArgs e)
        {
            string EngineerName = tb_Name.Text.ToString();
            EngineerName.Split(' ').ToList().ForEach(i => Console.Write(i[0] + " "));

            tb_Initials.Text = EngineerName;

        }
Run Code Online (Sandbox Code Playgroud)

但这只是填写了完全相同名称的文本框...

c# split winforms

3
推荐指数
1
解决办法
96
查看次数

VB.NET:如果我使用Await运行CPU绑定代码会发生什么?

我想了解async/await.我知道你不应该等待一个受CPU限制的方法,但是为了帮助我理解我很好奇如果你这样做会发生什么.考虑:

Public Async Function DoSomeTasks()
    Await LongRunningCPUBoundMethod1()

    LongRunningCPUBoundMethod2()
End Function

Public Async Function LongRunningCPUBoundMethod1() As Task
     ' Do stuff synchronously
End Function

Public Sub LongRunningCPUBoundMethod2()
     ' Do stuff synchronously
End Sub
Run Code Online (Sandbox Code Playgroud)

Task Scheduler如何处理CPU资源?这些方法将以什么顺序执行?LongRunningCPUBoundMethod1或LongRunningCPUBoundMethod2会先执行吗?

vb.net asynchronous winforms async-await

3
推荐指数
1
解决办法
78
查看次数