0

My problem is common which is to print an array elements inside a label in windows form with c# but I wanted to use if statement to print a certain elements not all the elements , the code has two forms :

form 1 :

public static String [] names = new String [3];
private void button (sender and whatever ...){

      if(label1.Text.Equals("meal1")){
         names[0] = "name1";
      }
      if(label1.Text.Equals("meal2")){
         names[1] = "name2";
      }
      if(label1.Text.Equals("meal3")){
         names[2] = "name3";
      }
      this.Hide();
      Form2 frm2 = new Form2();
      frm2.Show();
}

I did this to send the array data to the next form , the elements of the array are stored depending on user choice from a menu

form 2 :

private void Form2_Load(...){

    String [] names2 = {"name1" , "name2" , "name3"};

    for(int i = 0 ; i < Form1.names.Length ; i++){
        if(Form1.names[i].Equals(names2[i])){
             label1.Text = names2[i] + "\n";
        }
    }

}

lets say user chooses "meal1" and "meal3" , now when form 2 is loaded I must see "meal1" and "meal3" in the label but all I see is the last choice of the user , I tried String.join("\n",names2[i]); but it printed all the array elements I tried labe1.Text += names2[i] + "\n"; it also prints the last choice for the user.

thank you ...

4
  • 2
    You need to use += operator instead of = in this line label1.Text += names2[i] + "\n"; Commented Apr 9, 2020 at 10:46
  • printing is, if you send something to a printer. Commented Apr 9, 2020 at 10:52
  • Holger , thanks for the info , maybe because am working with java mostly we use the phrase "print" a lot is there an alternative to this phrase ? Commented Apr 9, 2020 at 19:02
  • @SalahAkbari , it worked well thank you , but null pointer problem appears any ideas ? Commented Apr 9, 2020 at 19:04

1 Answer 1

2

Yah, I got your problem. every time you run your code you will get the last name whose condition will be true. Here is solution to your problem.

if(Form1.names[i].Equals(names2[i])){
         label1.Text = names2[i] + "\n";
    }

In these lines, you are just assigning the name to label1 but every time you assign a new value, the old value is replaced. You should change this assignment line as

if(Form1.names[i].Equals(names2[i])){
         label1.Text += names2[i] + "\n";
    }
Sign up to request clarification or add additional context in comments.

2 Comments

thank you it worked but another problem appeared which is the null pointer ... is there any null pointer here ?
Null pointer exception occurs when you overrun loop. Change your for loop as following: ............................... for(int i = 0 ; i < .names2.Length ; i++){

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.