-2

I want to access labelNummer.Text from my User Control in my form.

Form

if (idPartijen.Contains(labelNummer.Text))
        {
            con.SqlQuery("SELECT * FROM `kandidaat` where `partijnummer` =@nummer ");
            con.Cmd.Parameters.Add("@nummer", MySql.Data.MySqlClient.MySqlDbType.VarString).Value = labelNummer.Text;

User Control

  public string KandidaatNummer
    {
        set
        {
            Nummer = value;
            labelNummer.Text = Nummer;
        }
    }

1 Answers1

0

Your UserControl

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
    }

    private void MyUserControl_Load(object sender, EventArgs e)
    {

    }

    public string KandidaatNummer
    {
        get
        {
            return labelNummer.Text;
        }

        set
        {
            labelNummer.Text = value;
        }
    }
}

Let's say that your user control has a name called: MyUserControl1

 if (idPartijen.Contains(myUserControl1.KandidaatNummer)) { ... }
Flavio Francisco
  • 755
  • 1
  • 8
  • 21
  • Thanks for the help. I changed my code but now it gives me the error an object reference is required for the non-static field method or property. Do you know how to fix this? –  google_was_my_idea May 26 '19 at 20:09
  • Please check if this is the case on your code: https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop – Flavio Francisco May 26 '19 at 20:24