我有一个控制台应用程序,它调用这样的表单,我把它放在另一个类中只是为了使我的代码井井有条。
我像这样从头开始制作表格代码:
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时,没有任何反应。
有什么想法吗?
感谢所有答案
约翰
答案 0 :(得分:0)
您只需要进行此活动
Synology.Click += (sender, args) =>
{
MessageBox.Show("You clicked Synology");
};
在showDialog()方法之前;
直到showDialog方法返回值
,showDialog事件之后的行才会运行我还认为您需要重新设计该类,以便能够正确处理它。
我希望这对您有用。
答案 1 :(得分:0)
所以我还有另一个问题。
我现在有这样的课程:
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”
当我进行逐步调试时,我认为它已停止使用该信号量方法,但是即使我在filedownloader中删除了该方法,它也无法正常工作。
当我从控制台调用函数时,其正常工作的问题仅在于我通过消息框中的该按钮进行调用。