2014-02-14 70 views
1

我有一個調用另一個窗體(子窗體)的窗體(父窗體)。從子窗體winform修改父窗口的控件C#

在子窗體中,我在文本框中做了一些更改,父窗體中有標籤,我想在其中動態顯示這些更改。

我在做什麼現在的問題是:

 NewForm newForm = new NewForm(this); 
     newForm.Parent = this; 
     newForm.ShowDialog(this); 

我不知道這個正確的方式做...

但我無法從this.Parent得到控制孩子的形式。

請幫助我一樣。

在此先感謝...

+0

你可以改變文本框的屬性修改器或標籤公共爲了你訪問它像((MyForm)this.Parent).txtMyTextBox.Text =「」; – Jade

回答

4

首先在你的父窗體添加將更新標籤的屬性。

public string LabelonParent { 
     get { return label1.Text;} 
     set { label1.Text=value;} 
    } 

現在添加事件的方法,將顯示在子窗體子窗體

private void button1_Click(object sender, EventArgs e) 
    { 
     ChildClass form = new ChildClass(); 
     DialogResult result = form.ShowDialog(this); 
    } 

添加TextChanged事件的方法。

private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     ((ParentClass)this.Owner).LabelonParent = textBox1.Text; 
    } 

你甚至可能要添加一個formload事件方法與label1.Text填充textBox1的

private void ChildClass_Load(object sender, EventArgs e) 
    { 
     textBox1.Text = ((ParentClass)this.Owner).LabelonParent; 
    } 
0

這是實現此目的的正確方法。但是,如果您想要獲得特定的自定義屬性,請使用像((ParentForm)newForm.Parent).CustomProp這樣的鑄件,並記住使用Invoke(),因爲每個表單都有自己的線程。

0

您可以在您的子類中公開public string LabelText { get; set; }

當您關閉診斷日誌時,將LabelText從子項更新爲父項。 檢查下面的代碼

class ChildClass 
{ 
    public string LabelText { get; set; } 
    //Update the above property in suitable method. 
    public string ChildMethod() 
    { 
     LabelText = "XXXX"; 
    } 

} 
class ParentClass 
    { 
     public string ParentLabelText { get; set; } 

     public string ParentMethod() 
     { 
      var childObj = new ChildClass(); 
      DialogResult result = ShowDialog(); 

      if (result == DialogResult.Cancel) 
      { 
       //update your parent each time you are closing the showdialog window. 
       this.ParentLabelText = childObj.LabelText; 
      } 
     } 
    } 

感謝 SRIKANTH

1

下面是實現這種功能和子窗體上實時更新的文字,並反映在父窗體

子窗體代碼

的最佳方式
public delegate void PassText(string textValue); 

     public event PassText RaisePassTextEvent; 

private void textBox_TextChanged(object sender, EventArgs e) 
     { 
      if (RaisePassTextEvent != null) 
      { 
       RaisePassTextEvent(textBox.Text); 
      } 

     } 

主/父表格代碼

 Child oChild = new Child(); 
      oChild.RaisePassTextEvent += oChild_RaisePassTextEvent; 
      oChild.MdiParent = this; 
      oChild.Show(); 

    void oChild_RaisePassTextEvent(string textValue) 
     { 
      this.Invoke(new Action(() => lableControl.Text = textValue)); 
     } 
1

有很多方法可以做到這一點。例如,將控制修飾符屬性分配給公共,這可以使該控件在另一個類中可訪問。另一種方法是你可以在子表單中創建一個屬性或公共變量,並在textchange上賦值。這個值可以在ShowDialog()函數調用後返回。即使您可以將父窗體標籤控件修飾符屬性設置爲公開,以便您可以直接在子窗體中訪問該標籤。
但是,分配一個控制修飾符public是不正確的。

另一個邏輯是你可以在子表單中創建一個公共事件,並且將由父表單處理。此事件將在文本更改後執行。

frmChild child = new frmChild(); 
frmChild.Lable_TextChanged += new EventHandler(child_TextChanged); 
frmChild.ShowDialog(); 

//lbl which will be placed in parent form 
private void child_TextChanged(object sender, EventArgs e) 
{ 
    //sender will return textbox from child form 
    lbl.Text = ((TextBox)sender).Text; 
} 

代碼子窗體:

public event EventHandler Lable_TextChanged; 

//txtText which will be placed in child form 
private void txtText_TextChanged(object sender, EventArgs e) 
{ 
    if (Lable_TextChanged != null) 
     Lable_TextChanged(sender, e); 

}