我最近开始学习更多关于事件/代理以及类扩展的知识.
我想通过向Windows Form控件中添加一个扩展方法将我学到的东西付诸实践SetDraggable(),后者又使用a MouseDown和MouseMove事件来移动控件.
一切正常,除了它只适用于特定控件 - 在我的情况下,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) 我想在我的上传过程中显示上传过程的进度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) 我想在我的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个复选框?
我想为图表添加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) 是否可以检测从另一个表单关闭的表单.例如.如果我有一个打开subForm的mainForm,我可以在mainForm中检测到subForm已关闭并执行代码吗?
我知道我可以在subForm中创建一个事件处理程序,但这并不是我真正想要的,因为在subForm关闭之后我将要做的是在mainForm中(对mainForm的更改).
我有一个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)'." 那么我做错了什么或者根本没做什么?
众所周知,有些解决方案与此类似,但是我无法用它们解决问题。我有两个用户控件:
我有一个链接两个控件的主窗体。
这两个控件是在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 以显示它?
我正在关注Udemy系列,它首先要求的是创建一个Windows窗体应用程序(使用C#).我不认为这是我的选择之一.这是因为我使用的是免费版吗?我已经安装了Windows Template Studio.
所以我有以下形式:
我想要实现的目标如下:
输入名称后,我希望只需单击首字母文本框即可生成名称的首字母.
我找到了以下方法来获取字符串的第一个字符:
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)
但这只是填写了完全相同名称的文本框...
我想了解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会先执行吗?