0

I have a Form1 with a align_Click button on it. align_Click button creates a Form2 with a 3 Buttons and 3 TextBoxes. Each of these Buttons creates another Form3 with a ComboBox and a 'Button'. All controls listed above are created dynamically,not in the Design-Mode. How can I pass the value of Comboboxfrom Form3 to Textbox in Form2? I have difficulties in writing an EventHandler for the Button in Form3, aka aOK_Click

Here is code snippet :

public void align_Click(object sender, EventArgs e)
    {
        Button alignContentBtn = new Button();
        alignContentBtn.Location = new System.Drawing.Point(86, 8);
        alignContentBtn.Size = new System.Drawing.Size(47, 23);
        alignContentBtn.Text = "Seç";
        alignContentBtn.Click += new EventHandler(this.alignContentBtn_Click);
        alignForm.Controls.Add(alignContentBtn);

        Button alignItemsBtn = new Button();
        alignItemsBtn.Location = new System.Drawing.Point(86, 43);
        alignItemsBtn.Size = new System.Drawing.Size(47, 23);
        alignItemsBtn.Text = "Seç";
       // alignItemsBtn.Click += new EventHandler(this.alignItemsBtn_Click);
        alignForm.Controls.Add(alignItemsBtn);

        Button alignSelfBtn = new Button();
        alignSelfBtn.Location = new System.Drawing.Point(86, 77);
        alignSelfBtn.Size = new System.Drawing.Size(47, 23);
        alignSelfBtn.Text = "Seç";
        // alignSelfBtn.Click += new EventHandler(this.alignSelfBtn_Click);
        alignForm.Controls.Add(alignSelfBtn);

        TextBox alignContentVal = new TextBox();
        alignContentVal.Location = new System.Drawing.Point(139, 10);
        alignContentVal.Name = "alignContentVal";
        alignContentVal.Size = new System.Drawing.Size(87, 20);
        alignContentVal.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
        alignForm.Controls.Add(alignContentVal);

        TextBox alignItemsVal = new TextBox();
        alignItemsVal.Location = new System.Drawing.Point(139, 45);
        alignItemsVal.Name = "alignItemsVal";
        alignItemsVal.Size = new System.Drawing.Size(87, 20);
        alignItemsVal.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
        alignForm.Controls.Add(alignItemsVal);

        TextBox alignSelfval = new TextBox();
        alignSelfval.Location = new System.Drawing.Point(139, 79);
        alignSelfval.Name = "alignSelfval";
        alignSelfval.Size = new System.Drawing.Size(87, 20);
        alignSelfval.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
        alignForm.Controls.Add(alignSelfval);         
    }

    private void alignContentBtn_Click(object sender, EventArgs e)
        {
            ComboBox c1 = new ComboBox();
            c1.Name = "c1";
            c1.Size = new Size(80, 21);
            c1.Location = new Point(12, 38);
            c1.Items.AddRange(new object[] {
            "flex-start",
            "flex-end",
            "center",
            "space-between",
            "space-around",
            "stretch"});
            c1.SelectedIndexChanged += new      System.EventHandler(this.c1_SelectedIndexChanged);
            alignC.Controls.Add(c1);

            Button aOk = new Button();
            aOk.Location = new System.Drawing.Point(99, 86);
            aOk.Size = new System.Drawing.Size(40, 23);
            aOk.Text = "OK";
            aOk.Click += new EventHandler(this.aOk_Click);
            alignC.Controls.Add(aOk);
        }
  • The typical way to solve this is for each form to have an object representing the state of its data, called a viewmodel. For `Form3` for example, you'd have `class Form3Model { public string Selection { get; set; } }`, and `Form3` would have a property `public Form3Model Model { get; } // initialize in constructor`. Wire up the event handlers in form 3, e.g. the combobox change event or whatever, to update the `Selection` property in `this.Model`. When the button on `Form3` is pressed, `Form2` can look at `form3.Model.Selection`. – Asad Saeeduddin Dec 20 '15 at 18:19
  • Thanks! Will work on it! – Ahmed Cholluyev Dec 20 '15 at 18:26
  • 1
    Possible duplicate of [Passing Values Between Windows Forms](http://stackoverflow.com/questions/17836398/) or [How do I pass a value from a child back to the parent form](http://stackoverflow.com/questions/280579/)? – Dour High Arch Dec 20 '15 at 19:02

1 Answers1

0

A very simple way could be the following. Define the form with a public property for the data, here Index, then in the load event use it to initialise the combobox and in the closing event read it back to the property:

public partial class Form3 : Form
{
    public int Index { get; set; }

    public Form3()
    {
        InitializeComponent();
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        comboBox1.SelectedIndex = Index;
    }

    private void Form3_FormClosing(object sender, FormClosingEventArgs e)
    {
        Index = comboBox1.SelectedIndex;
    }
}

Now you can create this class dynamicaly and use it:

int nIndex = 0;
Form3 oFrm = new Form3();
oFrm.Index = nIndex;
DialogResult oErg = oFrm.ShowDialog();
if (oErg == DialogResult.OK)
{
    nIndex = oFrm.Index;
}

So, i think this is a small and convenient solution for small projects. In greater ones this can be improved easily...

Dieter Meemken
  • 1,937
  • 2
  • 17
  • 22