我正在尝试做一个大型编码项目,但是我碰到了一堵墙。 输入数据后,我需要显示名称和分数。 我尝试使用youtube教程,代码类。但没有这样的运气。 任何帮助都会很棒!
form1
:
private void bNew_Click(object sender, EventArgs e)
{
score link = new score();
link.Show();
SudentBox.Items.Clear();
}
form2
:
public object StudentBox { get; private set; }
private void bCancel_Click(object sender, EventArgs e)
{
this.Close();
try
{
string name = txtName.Text;
int score = Convert.ToInt32(txtScore.Text);
txtStoreScores.Text += score.ToString() + " ";
}
catch (Exception x)
{
MessageBox.Show("Please enter a number");
}
}
private void bClearScores_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtScore.Text = "";
txtStoreScores.Text = "";
}
最终结果表单应该是什么样子的示例。
如果我是对的,你正在尝试编码一种形式的DialogBox
。
比如说,你想从对话框中获得一个名字(例如来自TextBox
的Form2
),你可以拥有这样的模型(当然在Form2
中)。
public string Name
{
//where myTextBox is the design name of your textbox
get => myTextBox.Text;
set => myTextBox.Text=value;
}
简单的Ok按钮
public void OkBtnClick(object sender, EventArgs e)
{
this.Close();
}
现在,您需要实际获取此信息以显示在您的Form1
中。这很容易。
就像你从上面开始:
private void bNew_Click(object sender, EventArgs e)
{
score link = new score();
link.ShowDialog();
//Note that you won't be able to access form1.
SudentBox.Items.Clear();
//You can now get the name
string _nameResult=link.Name;
NameTextbox.Text=_nameResult;
}
我希望这能让你开始!
您可以使用该属性执行此操作。在表单2上添加公共静态属性,并将文本的值分别设置为属性,然后在表单1上访问它们。
在Ok按钮单击事件的表单2上执行此操作
public static string Name { get; set; }
public static string Scores { get; set; }
private void bOk_Click(object sender, EventArgs e)
{
Name = txtName.Text;
Scores = txtStoreScores.TextBox;
}
然后在Form 1 OnLoad事件中访问这些属性并在TextBox中显示它们
private Form1_Load (object sender, EventArgs e)
{
StudentBox.Items.Add(string.Format("{0} {1}", Form2.Name, Form2.Scores);
}