我试图在程序运行期间创建一个列表,我在createList()函数中创建了列表并调用了该函数;但是我无法从之后调用的button1_Click()函数访问该列表。 如何从与其创建的功能不同的功能访问创建的列表。
代码摘录:
public void createlist()
{
ListView MainList = new ListView();
DisplayPanel.Controls.Add(MainList);
MainList.View = View.Details;
MainList.GridLines = true;
MainList.Name = "MainList";
MainList.Size = DisplayPanel.Size;
int s1 = DisplayPanel.Size.Height;
int s2 = DisplayPanel.Size.Width;
MainList.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
MainList.Columns.Add("ProductName", 100);
MainList.Columns.Add("ProductName2", 100);
MainList.Columns.Add("ProductName3", 100);
MainList.Columns.Add("ProductName4", 100);
}
private void button1_Click(object sender, EventArgs e)
{
createlist(); //Calls function and creates the ListView
ListViewItem Source = new ListViewItem("Source", 0);
Source.Checked = true;
Source.SubItems.Add("7");
Source.SubItems.Add("8");
Source.SubItems.Add("9");
MainList.Items.AddRange(new ListViewItem[] {Source});
}
错误: 错误CS0103名称' MainList'在当前上下文中不存在
答案 0 :(得分:1)
发生错误是因为MainList
不在button1_Click
方法的范围中。
有两种方法可以解决这个问题。
将MainList
变量放在所有方法之外。这样,MainList
变量就在整个类的范围内,这意味着类中的每个方法都可以访问它。
ListView MainList;
public void createlist()
{
MainList = new ListView();
DisplayPanel.Controls.Add(MainList);
MainList.View = View.Details;
MainList.GridLines = true;
MainList.Name = "MainList";
MainList.Size = DisplayPanel.Size;
int s1 = DisplayPanel.Size.Height;
int s2 = DisplayPanel.Size.Width;
MainList.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
MainList.Columns.Add("ProductName", 100);
MainList.Columns.Add("ProductName2", 100);
MainList.Columns.Add("ProductName3", 100);
MainList.Columns.Add("ProductName4", 100);
}
// your other method should be here...
从createList
返回一个值。
public ListView createlist()
{
MainList = new ListView();
DisplayPanel.Controls.Add(MainList);
MainList.View = View.Details;
MainList.GridLines = true;
MainList.Name = "MainList";
MainList.Size = DisplayPanel.Size;
int s1 = DisplayPanel.Size.Height;
int s2 = DisplayPanel.Size.Width;
MainList.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
MainList.Columns.Add("ProductName", 100);
MainList.Columns.Add("ProductName2", 100);
MainList.Columns.Add("ProductName3", 100);
MainList.Columns.Add("ProductName4", 100);
return MainList;
}
private void button1_Click(object sender, EventArgs e)
{
var listView = createlist(); //Calls function and creates the ListView
ListViewItem Source = new ListViewItem("Source", 0);
Source.Checked = true;
Source.SubItems.Add("7");
Source.SubItems.Add("8");
Source.SubItems.Add("9");
listView.Items.AddRange(new ListViewItem[] {Source});
}
我推荐第一个解决方案,因为它允许类中的所有方法访问您可能需要的MainList
。如果您完全确定不想以任何其他方式访问MainList
,请使用第二种解决方案。
答案 1 :(得分:0)
MainList(在您的createList()函数中)只是该函数的局部变量,只能在该函数中使用。为了能够在其他函数中访问它,您需要将其作为类变量。
答案 2 :(得分:0)
您需要将此作为该类的成员才能从其他函数访问它。见下文:
class Foo{
ListView MainList = new ListView();
public void createlist()
{
MainList = new ListView();
DisplayPanel.Controls.Add(MainList);
MainList.View = View.Details;
MainList.GridLines = true;
MainList.Name = "MainList";
MainList.Size = DisplayPanel.Size;
int s1 = DisplayPanel.Size.Height;
int s2 = DisplayPanel.Size.Width;
MainList.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
MainList.Columns.Add("ProductName", 100);
MainList.Columns.Add("ProductName2", 100);
MainList.Columns.Add("ProductName3", 100);
MainList.Columns.Add("ProductName4", 100);
}
private void button1_Click(object sender, EventArgs e)
{
createlist(); //Calls function and creates the ListView
ListViewItem Source = new ListViewItem("Source", 0);
Source.Checked = true;
Source.SubItems.Add("7");
Source.SubItems.Add("8");
Source.SubItems.Add("9");
MainList.Items.AddRange(new ListViewItem[] {Source});
}
}