Home > Back-end >  using variables in controls from Form_load
using variables in controls from Form_load

Time:06-20

I declared my variable in Form_load so that it's preloaded and I plan to have these variables used and edited by some events, but I can't call them outside of the controls.

Am I doing it wrong? If yes, how should I do it?

1

CodePudding user response:

You need to make that variable a member of the class. Currently it just lives in the Form1_Load method. Add private int totaldue; to the top of your class and remove it from the form load.

CodePudding user response:

Your variable totaldue is only defined in the scope of the method Form1_Load. You have to define that variable outside to access it from another scope.

Try this:

public partial class Form1 : Form
{

    private int totaldue;

    public Form1()
    {
        InitializeComponent();
    }
    
    public void Form1_Load(object sender, EventArgs e)
    {
        comboBoxl.Items.Add("Movie 1");
        comboBoxl.Items.Add("Movie 5");
        comboBoxl.Items.Add("Movie 4");
        comboBoxl.Items.Add("Movie 3");
        comboBoxl.Items.Add("Movie 2");
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = Convert.ToString(totaldue);
    }
}
  •  Tags:  
  • c#
  • Related