c# 从另一个 void/form (eventHandler) 获取控制权
c# get control from another void/form (eventHandler)
在 form1
中,我创建了名为 formhaslo
的简单表单。
我在 formhaslo
控件中创建了名为 listBoxhaslo
现在,我想为 listBoxhaslo
创建 MouseDoubleClick
事件。
我在将 listBoxhaslo
从 formhaslo
变为 form1
时遇到问题。
请看一下这段代码(查看评论):
public partial class Form1 : Form
{
Form formhaslo = new Form();
...
...
...
public void buttonLoadPassForBAKFile_Click(object sender, EventArgs e)
{
int i = 0;
string path = @"Backups";
formhaslo.StartPosition = FormStartPosition.CenterScreen;
ListBox listBoxhaslo = new ListBox();
listBoxhaslo.Location = new System.Drawing.Point(0, 30);
listBoxhaslo.Left = (formhaslo.ClientSize.Width - listBoxhaslo.Width) / 2;
using (FileStream fsSbHaslo = new FileStream(path + @"\BAKPass._pass", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (StreamReader srhaslo = new StreamReader(fsSbHaslo, Encoding.Default))
{
string line;
while ((line = srhaslo.ReadLine()) != null)
{
listBoxhaslo.Items.Add(line);
i++;
}
srhaslo.Close();
}
formhaslo.Controls.Add(listBoxhaslo);
formhaslo.Controls.Add(label1);
listBoxhaslo.MouseDoubleClick += new MouseEventHandler(listBoxhaslo_MouseDoubleClick); // <---here is EventHandler
formhaslo.Show();
}
}
void listBoxhaslo_MouseDoubleClick(object sender, MouseEventArgs e)
{
if ((listBoxhaslo.SelectedItem) != null) // <--listBoxhaslo does not exist in current context
{
PassForBakFile = (listBoxhaslo.SelectedItem.ToString());
formhaslo.Hide();
}
}
我知道这个错误一定存在,因为我做错了,但我不知道该怎么做。
listBox 不存在,因为它是在第一个函数的范围内声明的,这对您的事件的第二个函数不可见 listBoxhaslo_MouseDoubleClick。为了让它工作,你需要在你的函数之外声明 listBoxhaslo 变量。您可以在靠近顶部的 formhaslo 之后声明它。或者另一种实现此目的的方法是在您的事件中将 sender 转换为 ListBox。
void listBoxhaslo_MouseDoubleClick(object sender, MouseEventArgs e)
{
var listBoxhaslo = (sender as ListBox);
if (listBoxhaslo.SelectedItem != null)
{
PassForBakFile = (listBoxhaslo.SelectedItem.ToString());
formhaslo.Hide();
}
}
我还没有尝试过代码,但我认为它可以。
在 form1
中,我创建了名为 formhaslo
的简单表单。
我在 formhaslo
控件中创建了名为 listBoxhaslo
现在,我想为 listBoxhaslo
创建 MouseDoubleClick
事件。
我在将 listBoxhaslo
从 formhaslo
变为 form1
时遇到问题。
请看一下这段代码(查看评论):
public partial class Form1 : Form
{
Form formhaslo = new Form();
...
...
...
public void buttonLoadPassForBAKFile_Click(object sender, EventArgs e)
{
int i = 0;
string path = @"Backups";
formhaslo.StartPosition = FormStartPosition.CenterScreen;
ListBox listBoxhaslo = new ListBox();
listBoxhaslo.Location = new System.Drawing.Point(0, 30);
listBoxhaslo.Left = (formhaslo.ClientSize.Width - listBoxhaslo.Width) / 2;
using (FileStream fsSbHaslo = new FileStream(path + @"\BAKPass._pass", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (StreamReader srhaslo = new StreamReader(fsSbHaslo, Encoding.Default))
{
string line;
while ((line = srhaslo.ReadLine()) != null)
{
listBoxhaslo.Items.Add(line);
i++;
}
srhaslo.Close();
}
formhaslo.Controls.Add(listBoxhaslo);
formhaslo.Controls.Add(label1);
listBoxhaslo.MouseDoubleClick += new MouseEventHandler(listBoxhaslo_MouseDoubleClick); // <---here is EventHandler
formhaslo.Show();
}
}
void listBoxhaslo_MouseDoubleClick(object sender, MouseEventArgs e)
{
if ((listBoxhaslo.SelectedItem) != null) // <--listBoxhaslo does not exist in current context
{
PassForBakFile = (listBoxhaslo.SelectedItem.ToString());
formhaslo.Hide();
}
}
我知道这个错误一定存在,因为我做错了,但我不知道该怎么做。
listBox 不存在,因为它是在第一个函数的范围内声明的,这对您的事件的第二个函数不可见 listBoxhaslo_MouseDoubleClick。为了让它工作,你需要在你的函数之外声明 listBoxhaslo 变量。您可以在靠近顶部的 formhaslo 之后声明它。或者另一种实现此目的的方法是在您的事件中将 sender 转换为 ListBox。
void listBoxhaslo_MouseDoubleClick(object sender, MouseEventArgs e)
{
var listBoxhaslo = (sender as ListBox);
if (listBoxhaslo.SelectedItem != null)
{
PassForBakFile = (listBoxhaslo.SelectedItem.ToString());
formhaslo.Hide();
}
}
我还没有尝试过代码,但我认为它可以。