【问题标题】:Check if button is clicked from scratched made button检查是否从划伤的按钮上单击了按钮
【发布时间】:2019-03-24 08:27:48
【问题描述】:

我有控制台应用程序,它像这样调用表单,我把它放在其他类只是为了让我的代码有条理。

我是这样从头开始制作表单代码的:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Ultra_Script.MessageBoxes
{
    public class MsgBoxes
    {
        #region MsgBox k výběru základního SW (stažení z netu nebo Synology)
        public static void SynoInternet()
        {
            Application.EnableVisualStyles();
            Form SynoInternet = new Form();  

            Button Synology = new Button()
            {
                Left = 80,
                Width = 90,
                Height = 30,
                Top = 75,
                Text = "Synology"
            };

            Button Internet = new Button()
            {
                Left = 190,
                Width = 90,
                Height = 30,
                Top = 75,
                Text = "Internet"
            };

            Label SynoInternetLabel = new Label()
            {
                Left = 60,
                Width = 350,
                Height = 25,
                Top = 30,
                Text = "Chceš SW stáhnout z internetu nebo HD Synology?"
            };

            Label fasterLabel = new Label()
            {
                Left = 80,
                Width = 60,
                Height = 20,
                Top = 110,
                Text = "(Rychlejší)"
            };

            SynoInternet.Width = 380;
            SynoInternet.Height = 170;
            SynoInternet.Controls.Add(fasterLabel);
            SynoInternet.Controls.Add(SynoInternetLabel);
            SynoInternet.Controls.Add(Synology);
            SynoInternet.Controls.Add(Internet);
            SynoInternet.ShowDialog();

        }
        #endregion

    }
}

但不知道如何(在此类中)检查按钮是否被单击。

我知道我需要创建 EventHandler 但没有成功。

我试过这样:

  Synology.Click += (sender, args) =>
            {
                MessageBox.Show("You clicked Synology");
            };

我没有收到任何错误,但是当我单击 Synology 时没有任何反应。

有什么想法吗?

感谢所有回答,

约翰

【问题讨论】:

    标签: c# forms button


    【解决方案1】:

    你只需要做这个活动

           Synology.Click += (sender, args) =>
            {
                MessageBox.Show("You clicked Synology");
            }; 
    

    showDialog() 方法之前;

    showDialog 事件之后的行在 showDialog 方法返回值之前不会运行

    另外,我认为您需要重新设计该类,以便能够正确处理它。

    我希望这对你有用。

    【讨论】:

      【解决方案2】:

      所以我还有一个问题。

      我现在有这样的课:

      using System;
      using System.ComponentModel;
      using System.Drawing;
      using System.Windows.Forms;
      
      namespace Ultra_Script.MessageBoxes
      {
          public class MsgBoxes
          {
              #region MsgBox k výběru základního SW (stažení z netu nebo Synology)
              public static void SynoInternet()
              {
                  Application.EnableVisualStyles();
                  Form SynoInternet = new Form();  
      
                  Button Synology = new Button()
                  {
                      Left = 80,
                      Width = 90,
                      Height = 30,
                      Top = 75,
                      Text = "Synology"
                  };
      
                  Button Internet = new Button()
                  {
                      Left = 190,
                      Width = 90,
                      Height = 30,
                      Top = 75,
                      Text = "Internet"
                  };
      
                  Label SynoInternetLabel = new Label()
                  {
                      Left = 60,
                      Width = 350,
                      Height = 25,
                      Top = 30,
                      Text = "Chceš SW stáhnout z internetu nebo HD Synology?"
                  };
      
                  Label fasterLabel = new Label()
                  {
                      Left = 80,
                      Width = 60,
                      Height = 20,
                      Top = 110,
                      Text = "(Rychlejší)"
                  };
      
                  SynoInternet.Width = 380;
                  SynoInternet.Height = 170;
                  SynoInternet.Controls.Add(fasterLabel);
                  SynoInternet.Controls.Add(SynoInternetLabel);
                  SynoInternet.Controls.Add(Synology);
                  SynoInternet.Controls.Add(Internet);
      
                  Synology.Click += (sender, args) =>
                  {
                      SynoInternet.Dispose();
                      Installation_Functions.InstallBasicSW();
                  };
      
                  Internet.Click += (sender, args) =>
                  {
                      MessageBox.Show("You clicked Internet");
                  };
      
                  SynoInternet.ShowDialog();
      
              }
              #endregion
      
          }
      }
      
      

      InstallFunctions 类只调用这个文件下载器:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading;
      using System.Threading.Tasks;
      using System.ComponentModel;
      using System.IO;
      using System.Net;
      using System.Threading;
      
      namespace Ultra_Script
      {
          class FileDownloader
          {
      
              private readonly string _url;
              private readonly string _fullPathWheretoSave;
              private bool _result = false;
              private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);
      
              public FileDownloader(string url, string fullPathWheretoSave)
              {
      
                  if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
                  if (string.IsNullOrEmpty(fullPathWheretoSave)) throw new ArgumentNullException("fullPathWhereToSave");
      
                  this._url = url;
                  this._fullPathWheretoSave = fullPathWheretoSave;
      
              }
      
              public bool StartDownload(int timeout)
              {
      
                  try
                  {
                      System.IO.Directory.CreateDirectory(Path.GetDirectoryName(_fullPathWheretoSave));
      
                      if (File.Exists(_fullPathWheretoSave))
                      {
                          File.Delete(_fullPathWheretoSave);
                      }
                      using (WebClient client = new WebClient())
                      {
                          var ur = new Uri(_url);
                          //client.Credentials = new NetworkCredential("username", "password");
                          client.DownloadProgressChanged += WebClientDownloadProgressChanged;
                          client.DownloadFileCompleted += WebClientDownloadCompleted;
                          Console.WriteLine(@"Stahuji potrebne soubory:");
                          client.DownloadFileAsync(ur, _fullPathWheretoSave);
                          _semaphore.Wait(timeout);
                          return _result && File.Exists(_fullPathWheretoSave);
                      }
      
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine("Cant download file");
                      Console.Write(e);
                      return false;
                  }
                  finally
                  {
                      this._semaphore.Dispose();
                  }
      
              }
      
              private void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
              {
                  Console.Write($"/r --> {e.ProgressPercentage}%");  
              }
      
              private void WebClientDownloadCompleted(object sender, AsyncCompletedEventArgs args)
              {
                  _result = !args.Cancelled;
                  if (!_result)
                  {
                      Console.Write(args.Error.ToString());
                  }
                  Console.WriteLine(Environment.NewLine + "Download Finished!");
                  _semaphore.Release();
              }
      
              public static bool DownloadFile(string url, string fullPathWhereToSave, int timeoutInMilliSec)
              {
                  return new FileDownloader(url, fullPathWhereToSave).StartDownload(timeoutInMilliSec);
              }
      
          }
      
      }
      
      
      

      什么是关闭表单然后在控制台中运行返回功能。但它只说:“开始下载”,超时后它消失:异常抛出:Visual Studio 中 System.dll 中的“System.ComponentModel.Win32Exception”

      当我逐步运行调试时,我认为它在那个 Semaphore 方法上停止了,但即使我在 filedownloader 中删除了这个方法,它也不起作用。

      当我从控制台调用函数时,它正常工作的问题是只有当我通过消息框中的那个按钮调用它时。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多