none
How to get the value of strValue and read in Form1_Load?

    Frage

  • Hello everyone, I have one question.

    There have 2 forms, (Form1 & Form2). Form1 and Form2 contains 1 textbox and 1 button... after running the application, by clicking a (buttonForm1), form2 will open. The user writes "Hello" in the (textboxForm2) and after that the user click a (buttonForm2), Form2 will hide and "Hello" is appears in (textboxForm1).

    So my next plan, I want to add label1.Text in Form1. This label1.Text will read the same as (textboxForm1), and label1.Text is  placed in private void Form1_Load(object sender, EventArgs e).

    According to the codes below, strValue = "Hello", How to make label1.Text = strValue in Form1_load? When I run the application. Error will popup: strValue does not exist in the current context.

    //Form1

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                label1.Text = strValue;
            }
            public void PassValue(string strValue)
            {
                textBox1.Text = strValue;
            }

            private void button1_Click(object sender, EventArgs e)
            {
                Form2 objForm2 = new Form2(this);
                objForm2.Show();
            }
        }

    //Form2

    public partial class Form2 : Form
        {
            Form1 ownerForm = null;
            public Form2(Form1 ownerForm)
            {
                InitializeComponent();
                this.ownerForm = ownerForm;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                this.ownerForm.PassValue(textBox1.Text);
                this.Hide();
            }
            private void button2_Click(object sender, EventArgs e)
            {
                this.Hide();
            }
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
         
            }
        }


    • Bearbeitet Pugita Donnerstag, 18. April 2019 11:06
    • Verschoben CoolDadTx Donnerstag, 18. April 2019 13:57 Winforms related
    Donnerstag, 18. April 2019 11:02

Antworten

  • No, this cannot be made to work the way you want it.

    First of all, why do you get an error in label1.Text = strValue? The reason is that strValue is a parameter to method PassValue, and therefore it is not visible outside that method.

    So, how can you fix the error? Well, you could declare a variable at class-level. Assign the value to the variable in PassValue, and then use the class-level variable in form_load.

    This will get rid of the compile-time error. Will it work? No. The reason is that Form_Load is executed BEFORE you call PassValue. Therefore, the variable will always be empty at that time, and you will not see anything in the label. So you will need to reconsider what you are doing. Form_Load is not suitable for duplicating the textbox into the label.

    • Als Antwort markiert Pugita Freitag, 19. April 2019 00:49
    Donnerstag, 18. April 2019 11:33

Alle Antworten

  • No, this cannot be made to work the way you want it.

    First of all, why do you get an error in label1.Text = strValue? The reason is that strValue is a parameter to method PassValue, and therefore it is not visible outside that method.

    So, how can you fix the error? Well, you could declare a variable at class-level. Assign the value to the variable in PassValue, and then use the class-level variable in form_load.

    This will get rid of the compile-time error. Will it work? No. The reason is that Form_Load is executed BEFORE you call PassValue. Therefore, the variable will always be empty at that time, and you will not see anything in the label. So you will need to reconsider what you are doing. Form_Load is not suitable for duplicating the textbox into the label.

    • Als Antwort markiert Pugita Freitag, 19. April 2019 00:49
    Donnerstag, 18. April 2019 11:33
  • Hello Sir,

    Thank you so much for that information. 

    Best regards,

    Pugita

    Freitag, 19. April 2019 00:53