从文本框文本中以其他形式设置标签 [英] setting label from textbox text in other form

查看:74
本文介绍了从文本框文本中以其他形式设置标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该场景是:我单击一个标签(位于form1),一个包含文本框,标签和按钮的form(form2)正在打开.现在,无论用户在form2的texttbox中键入什么内容,都应该出现在form1的标签上.这应该在单击按钮时完成..

紧急...

The scenario is: im clicking on a label (of form1),a form(form2)is opening with a textbox,label and a button. now whatever user types in texttbox of form2 should appear on label of form1.this should be done on button click..

its urgent...

推荐答案

创建事件,例如从form1打开的form2中的UpdateText.

在form2中单击按钮时,引发事件

在form1(form2_UpdateText)中的事件处理程序中,您可以在form1上更新标签.
Create an event, e.g. UpdateText in form2 that is opened from form1.

When the button is clicked in form2, raise the event

in the eventhandler in form1 (form2_UpdateText), you update the label on form1.


您确实不需要使子窗体知道父窗体.而且,让孩子对父母进行修改通常是一种不好的做法.

通常,您希望父母根据孩子的反馈改变自己.正如约翰尼(Johnny)所建议的,事件是做到这一点的一种方法.
但是,如果您还在单击按钮时关闭了表单...说表单仅在此处用于更改标签,那么您根本就不必担心事件.

在form2上(我非常希望您在代码中使用一个更好的名称),创建一个名为NewText或类似名称的属性.该属性应该是只读的,并且应该只返回文本框中的文本.然后,当单击按钮时,调用this.DialogResult = DialogResult.Ok并隐藏该表单.

在form1上(再次,我希望您对此命名更好),使用showdialog显示form2,如果它返回OK值,则可以使用form2上的属性来更改标签.

像这样:

You really don''t need to make the child form aware of the parent form. And it''s generally bad practice to allow the child to make modifications to the parent.

Generally, you want the parent to change itself based on feedback from the child. As Johnny suggested, events are one way to do this.

If, however, you are also closing your form on the button click...say the form is only there to change the label, then you don''t need to worry about events at all.

On form2 (which I seriously hope you have a better name for in your code), create a property called NewText or something similar. That property should be read-only and should just return the text within your textbox. Then, when the button is clicked, call this.DialogResult = DialogResult.Ok and hide that form.

On form1 (again, I hope you''ve named it better than that), show form2 using showdialog and if it returns an OK value, then you use the property on form2 to change the label.

Like so:

//Form1 method
private void label1_Click(object sender, EventArgs e)
{
  Form2 newForm = new Form2();
  if (newForm.ShowDialog() == DialogResult.OK)
  {
    label1.Text = newForm.NewText;
  }
}

//Form2 methods
public Form2()
{
  InitializeComponent();

  this.DialogResult = DialogResult.Cancel;
}

public void button1_Click(object sender, EventArgs e)
{
  this.DialogResult = DialogResult.OK;
  this.Hide();
}
public string NewText {
  get { return textBox1.Text; }
}



这不需要事件,也不需要子窗体知道其父窗体.但是,仅当您还单击按钮时隐藏了表单时,此方法才起作用.



This doesn''t require events and it doesn''t require the child form to know about its parent. But, it only works if you''re also hiding the form on the button click.


在form1上公开一个属性,该属性会更改标签文本,并且每次单击form2中的按钮时,都要进行更新此属性具有新值.
Expose a property on form1 that changes the label text and everytime a button in form2 is clicked, update this property with the new value.


这篇关于从文本框文本中以其他形式设置标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆