我正在创建一个程序,用户可以在一个表单上的文本框中输入值,然后数据将被转移到另一个表单。我认为这样做的最好方法是将用户数据存储在几个数组中,然后将这些数组放在上面,但此刻我真的遇到了麻烦。一旦用户输入了他们需要的文本框,文本框也会被创建,因此文本框最初不会存在于页面上
string q = combobox1.SelectedItem.ToString();
int g = Convert.ToInt32(q);
MessageBox.Show("I have added " +(g-1) +" Films to the list");
TextBox[] FilmTitle1 = new TextBox[int.Parse(q)];
TextBox[] FilmBudget1 = new TextBox[int.Parse(q)];
TextBox[] FilmBoxOffice1 = new TextBox[int.Parse(q)];
TextBox[] FilmDirector1 = new TextBox[int.Parse(q)];
TextBox[] FilmRtScore1 = new TextBox[int.Parse(q)];
TextBox[] FilmGenre1 = new TextBox[int.Parse(q)];
int y = 500;
for (int i = 0; i < g; i++)
{
FilmTitle1[i] = new TextBox();
FilmTitle1[i].Text = "Film Title";
FilmTitle1[i].Size = new Size(162, 20);
FilmTitle1[i].Location = new Point(106, y);
FilmTitle1[i].Tag = 0;
this.Controls.Add(FilmTitle1[i]);
y= y + 40;
private void Createbar_Click(object sender, EventArgs e)
{
BarGraphCreation frm = new BarGraphCreation(FilmTitle.Text, FilmBudget.Text, FilmBoxOffice.Text, FilmDirector.Text, FilmGenre.Text, ft1, FilmBudget1.Text, FilmBoxOffice1.Text, FilmDirector1.Text, FilmGenre1.Text);
frm.Show();
}
但是当我这样做的时候,它表示没有文本框的构造,尽管事实上它允许它在程序中的其他地方。 非常感谢任何类型的帮助
答案 0 :(得分:0)
public class Form
{
// your code ...
string q = combobox1.SelectedItem.ToString();
int g = Convert.ToInt32(q);
MessageBox.Show("I have added " +(g-1) +" Films to the list");
public TextBox[] FilmTitle1 = new TextBox[int.Parse(q)];
public TextBox[] FilmBudget1 = new TextBox[int.Parse(q)];
public TextBox[] FilmBoxOffice1 = new TextBox[int.Parse(q)];
public TextBox[] FilmDirector1 = new TextBox[int.Parse(q)];
public TextBox[] FilmRtScore1 = new TextBox[int.Parse(q)];
public TextBox[] FilmGenre1 = new TextBox[int.Parse(q)];
int y = 500;
for (int i = 0; i < g; i++)
{
FilmTitle1[i] = new TextBox();
FilmTitle1[i].Text = "Film Title";
FilmTitle1[i].Size = new Size(162, 20);
FilmTitle1[i].Location = new Point(106, y);
FilmTitle1[i].Tag = 0;
this.Controls.Add(FilmTitle1[i]);
y= y + 40;
private void Createbar_Click(object sender, EventArgs e)
{
BarGraphCreation frm = new BarGraphCreation(this);
frm.Show();
}
}
public class BarGraphCreation
{
// your code...
Form form;
public BarGraphCreation(Form form)
{
// ...
this.form = form;
}
private void function()
{
// here you can work with your data like
// form.FilmTitle1 and so on
}
}
答案 1 :(得分:0)
您应该避免将TextBox
等整个对象传递给其他控件。一般的想法是,你不要总是只传递数据(信息)。
由于Form
布局是动态创建的,因此有关Controls
的信息应放在某处,可以轻松再次访问。
示例(包括数组和列表作为示例):
public class Form1 : Form
{
private TextBox[] FilmTitles;
private List<TextBox> FilmBudget = new List<Textbox>();
//code removed for brevity
private void Button1_Click( /***/ )
{
FilmBudget.Clear();
int count = Convert.ToInt32(q);
FilmTitles = new TextBox[count];
for (int i = 0; i < count; i++)
{
FilmTitles[i] = new TextBox()
{
Text = "Programmer in one day",
Size = new Size(162, 20)
// all other definitions
};
FilmBudget.Add(new TextBox()
{
Text = "1225",
Size = new Size(162, 20)
// all other definitions
};
this.Controls.Add(FilmTitles[i]);
this.Controls.Add(FilmBudget[i]);
//Now you are holding all the TB & text in global variables (arrays/lists)
}
}
private void Createbar_Click(object sender, EventArgs e)
{
BarGraphCreation frm = new BarGraphCreation(
FilmTitles.Select( a => a.Text).ToArray(),
FilmBudget.Select( a => a.Text).ToArray());
frm.Show();
}
}
其中:
public class BarGraphCreation : Form
{
public BarGraphCreation(string[] Titles, string[] Budgets)
{ }
}
请注意,此部分使用LINQ
选择精确属性(值)为2个数组:
BarGraphCreation frm = new BarGraphCreation(
FilmTitles.Select( a => a.Text).ToArray(),
FilmBudget.Select( a => a.Text).ToArray());