1

I have a main form named: MainForm and a child form named: ChildForm I want to fill ChildForm's textboxes and in MainForm_ButtonClick i want to fire ChildForm_ButtonClick event.

ChildForm:

public partial class ChildForm :Form
  {
        public delegate void delPassData(TextEdit text);

 private void button1_Click(object sender, EventArgs e)
    {
       string depart = "";

       MainForm mfrm = new MainForm();
       delPassData del = new delPassData(frm.funData);
       del(this.Item_CodeTextEdit);
    }
}

MainForm:

public partial class MainForm : Form
 {

 public void funData(TextEdit txtForm1)
    { 
        string ss = "";
        ss = txtForm1.Text;
        MessageBox.Show(ss);
    }

  private void NavigationPanelBtns_ButtonClick(object sender, ButtonEventArgs e)
    {
        switch (e.Button.Properties.Caption)
        {
            case "Save":
             // i want to call funData() here but i get an empty messageBox
            break;
        }
    }

}

helen
  • 23
  • 1
  • 1
  • 5
  • 1
    [Does this help](http://msdn.microsoft.com/en-us/library/edzehd2t%28v=vs.110%29.aspx) – Sriram Sakthivel May 05 '14 at 11:51
  • Looks like very beginner question (like [this](http://stackoverflow.com/q/5743611/1997232) one). Use search harder! – Sinatr May 05 '14 at 11:53
  • @kyr Why dont you post that part in your question itself ? So it will be easy to understand and answer – N.J May 05 '14 at 11:57
  • You should use an MVC (Model View Controller) or MVP (Model View Presenter) approach for this. Here's an example: http://stackoverflow.com/questions/15605161/how-to-make-form1-label-text-change-when-checkbox-on-form2-is-checked/15605436#15605436 – Matthew Watson May 05 '14 at 12:08

2 Answers2

2

Child form

public partial class ChildForm : Form
{
    public ChildForm()
    {
        InitializeComponent();
        MainForm.OnChildTextChanged += MainForm_OnChildTextChanged;
        MainForm.OnButtonClick += MainForm_OnButtonClick;
        bttn1.Visible = false;
    }

    void MainForm_OnButtonClick(object sender, EventArgs e)
    {
        this.bttn1.PerformClick();
    }

    void MainForm_OnChildTextChanged(string value)
    {
        this.textBox1.Text = value;
    }

    private void bttn1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("I am hide. But shows message");
    }

}

public class Bttn : Button
{
    public new void PerformClick()
    {
        this.OnClick(EventArgs.Empty);
    }
}

Create a Parent Form

public partial class MainForm : Form
{
    public delegate void OnMyTextChanged(string value);
    public delegate void ButtonClicked(object sender, EventArgs e);

    public static event OnMyTextChanged OnChildTextChanged;
    public static event ButtonClicked OnButtonClick;

    ChildForm frm = new ChildForm();

    public MainForm()
    {
        InitializeComponent();
        frm.Show();
    }

    public void button1_Click(object sender, EventArgs e)
    {
        OnChildTextChanged("this is new value");
        OnButtonClick(sender, e);
    }
}
Vimal CK
  • 3,543
  • 1
  • 26
  • 47
  • thank you very much for your repsonse. Do you know where can i read some examples of Delegates? – helen May 05 '14 at 12:50
  • I have update the answer with events. Please mark the answer if useful :) – Vimal CK May 05 '14 at 13:20
  • this is exactly what i need. I'm sorry i couldn't explain it better. Thank you very much. – helen May 05 '14 at 13:30
  • i would also like to ask something more. if i set button1.Visible = false; then this.button1.PerformClick(); doesn't work. I don't want button1 to be visible at runtime. – helen May 05 '14 at 13:54
  • This is a custom requirement. So need to create a custom button in order to resolve the issue. Please find the updated answer – Vimal CK May 05 '14 at 16:28
  • this is very helpful! but i can also write my code in: void Root_OnButtonClick(object sender, EventArgs e) { MessageBox.Show("I can get data from here!"); } thanks again! – helen May 06 '14 at 06:43
  • Thank you for posting this example, it helped my understanding of how delegates and static event handlers worked. It was useful for understanding how business logic layer (Parent class above) could trigger control events on the UI (Child class above) in general. – Fuzzy Analysis Apr 27 '19 at 13:20
-2

To access a textbox in another form:

  1. Set Modifier property of the the textbox to public in child form.

  2. In main form, access the textbox by an object of the child form.

    Eg:

    obj.txtBox.Text="MyValue";
    

To access an event in another form:

  1. Set the event handling function to public.

  2. Invoke the function by passing null as parameters by the object of the form.

    Eg:

    obj.MyButton_Click(null, null);
    
Raging Bull
  • 18,593
  • 13
  • 50
  • 55
  • Regarding `MyButton_Click(null, null);`, don't set event `public`, it's bad. Instead set `MyButton` to be public and invoke event like this `MyButton.PerformClick()`. – Sinatr May 05 '14 at 12:12
  • 1
    i' ve tried MyButton.PerformClick(); but it only works on Form_Load event. – helen May 05 '14 at 12:18