How to assign the name of a component, where it does not exist in the current context

Asked

Viewed 54 times

0

I have a form for client registration and within this form there is a RadioButton that asks if this customer needs to send invoices, if the answer equal to SIM arise two components, a TextBox and a DateTimePicker.

if (rdsim.Checked == true)
{
    metroPanel1.Controls.AddRange(new Control[]
    {            
       new MetroFramework.Controls.MetroTextBox
       {
           Name = "txtconta", //aqui adiciono o nome do componente
           Location = new Point(TextBoxX, 15),
           Size = new Size(185, 30)
       },
       new MetroFramework.Controls.MetroDateTime
       {
           Name = "dtpvencimento", //aqui adiciono o nome do componente
           Format = DateTimePickerFormat.Custom,
           Location = new Point(DateTimeX, 13),
           Size = new Size(124,33)
       },
   });
}

Thus, when the user clicks on the Register button I must pass this data to the database, I am doing so:

Contas contas = new Contas();
contas.Nroconta = txtconta.Text;
contas.Vencimento = dtpvencimento.Text;

But it returns me the error that these Names do not exist in the current context, how can I fix this? There is another way to assign a component, other than by your Name?

2 answers

1


In doing Name = "dtpvencimento", you are just stating a name for the control and not creating a variable for it. Therefore, it makes no sense for you to try to access a variable...

There are several ways to solve this, one of them is to create a variable and update it at the time of component creation.

public class Form1
{
    private MetroTextBox txtconta;
    private MetroDateTime dtpvencimento;

    ...

    if (rdsim.Checked == true)
    {
        txtConta = new MetroFramework.Controls.MetroTextBox
        {
            Name = "txtconta", //aqui adiciono o nome do componente
            Location = new Point(TextBoxX, 15),
            Size = new Size(185, 30)
        };

        dtpvencimento = new { }; // ...

        metroPanel1.Controls.AddRange(new Control[]
        {            
           txtConta,
           dtpvencimento 
       });
    }
}

This way, you can access the elements as you are currently doing.

You can also leave the components created in time to design and just change their visibility. If all components of this form are being created in time design this can be a better solution than the previous.

If you want to keep the component creation code exactly as it is now, you can find the controls by their name using the method Find().

Contas contas = new Contas();
var txtconta = Controls.Find("txtconta");
var dtpvencimento = Controls.Find("dtpvencimento");

contas.Nroconta = txtconta.Text;
contas.Vencimento = dtpvencimento.Text;

Similarly to the above solution, it is possible to use the indexer of ControlCollection.

Contas contas = new Contas();
var txtconta = Controls["txtconta"];
var dtpvencimento = Controls["dtpvencimento"];

contas.Nroconta = txtconta.Text;
contas.Vencimento = dtpvencimento.Text;
  • I understood that’s exactly what I needed, perfect answer. I suspected that just assigning the name in that way was not enough but did not know how to complement. I think creating at design time and changing the visibility will not be possible in my case as it may have more than one account per customer.

0

I would create a method to be able to submit the control parameters for component creation.

Private Sub EnviarFormaPgto(ByRef ucUserControl As UserControl)

            Me.grpDados.Controls.Add(ucUserControl)// grpDados é um GroupBox

            ucUserControl.Location = New System.Drawing.Point(6, 7)
            ucUserControl.Name = "UcForma"
            ucUserControl.Size = New System.Drawing.Size(465, 85)
            ucUserControl.TabIndex = 2

End Sub

Browser other questions tagged

You are not signed in. Login or sign up in order to post.