Search code examples
c#buttonrichtextboxnumericupdown

Passing DomainUpDown.Value from programmatically generated form to main form


I have a form1 with a showForm button that programmatically creates and opens a new form2 with 4 DomainUpDown elements and a OKBtn button. I need to pass the value of DomainUpDown element using my OKBtn from form2 to form1 richtextbox.My only diffulty is that question mark at the end. Here is the code snippet :

public void showForm_Click(object sender,EventArgs e)
{
       Form frm = new Form();
       frm.Size = new Size(264, 183);
       frm.Name = "MarginSelector";
       frm.Text = "Qiymət ver";
       frm.ShowIcon = false;
       frm.Show();

       DomainUpDown marginRightVal = new DomainUpDown();
       marginRightVal.Location = new Point(150, 100);
       marginRightVal.Size = new Size(42, 40);
       frm.Controls.Add(marginRightVal);
       for (int i = 0; i < 100; i++)
       {
            marginRightVal.Items.Add(i + "%");
       }

       Button OKBtn = new Button();
       OKBtn.Visible = true;
       OKBtn.Text = "OK";
       OKBtn.Size = new Size(30, 23);
       OKBtn.Location = new Point(96, 109);
       frm.Controls.Add(OKBtn);
       OKBtn.Click += new System.EventHandler(this.OKBtn_Click);
}

public void OKBtn_Click(object sender, EventArgs e)
{
       textArea.SelectionLength = 0;
       textArea.SelectedText = string.Filter("margin-top: {0} ; \n, ? ");
}

Solution

  • You can follow Hans Passant his suggestion or you can cast the sender of the click event to a control, get a reference to the form and iterate over the Controls collection to find the control you're looking for. Once found, assign it to a variable and use it in your logic. An implentation could look like this:

    public void OKBtn_Click(object sender, EventArgs e)
    {
        // assume a Control is the sender
        var ctrl = (Control)sender;
        // on which form is the control?
        var frm = ctrl.FindForm();
        // iterate over all controls
        DomainUpDown domainUpDown = null;
        foreach(var ctr in frm.Controls)
        {
            // check if this is the correct control
            if (ctr is DomainUpDown)
            {
                // store it's reference
                domainUpDown = (DomainUpDown)ctr;
                break;
            }
        }
        // if we have found the control
        if (domainUpDown != null)
        {
            textArea.SelectionLength = 0;
            Debug.WriteLine(domainUpDown.SelectedIndex);
            Debug.WriteLine(domainUpDown.SelectedItem);
            // use the SelectedItem
            textArea.SelectedText = string.Format("margin-top: {0} ; \n,", domainUpDown.SelectedItem );
        }
    }
    

    If you have multiple controls on your form you better add an unique name to each of them:

    DomainUpDown marginRightVal = new DomainUpDown();
    marginRightVal.Location = new Point(150, 100);
    marginRightVal.Size = new Size(42, 40);
    marginRightVal.Name = "right";
    frm.Controls.Add(marginRightVal);
    

    and when you iterate over the control collection you can check for that name:

    foreach(var ctr in frm.Controls)
    {
        // check if this is the correct control
        if (ctr is DomainUpDown)
        {
            // store it's reference
            domainUpDown = (DomainUpDown)ctr;
            if (domainUpDown.Name == "right")
            {
               // do logic for that value
            }
        }
    }
    

    Or you can use the Find method:

    var found = frm.Controls.Find("right", false);
    if (found.Length>0)
    {
        var rightDomain = (DomainUpDown)found[0];
        // do logic here
    }