0

I've referred to this link (thread 1) but I get an error. For my example, I only have one list in one class. The class name is Savestate. I have 2 forms.

Form1 contain a textbox where the string that I saved inside will be transferred to the list when I press button1. Button1 will also open

Form2 where there is a label that should reflect the string in the textbox. However, the label will reflect systems.collection...

Below is my code.

Savestate: class name

public static List<string> number = new List<string>();

Form1

private void button1_click(object sender, System.EventArgs e)
 {
  Savestate.number.Add(textbox1.Text);
  Formscollection.Form1.Hide(); //Form 1 and Form 2 saved in another class called formscollection
  Formscollection.Form2.Show(); 
 }

Form2 (Show the systems.collections..)

private void Form2_VisibleChanged(object sender, EventArgs e)
 {
   label1.Text = Savestate. number.ToString();
 }

I tried another code based on other forums but I received an error

Form2 (got error: cannot implicitly convert type void to string)

private void Form2_VisibleChanged(object sender, EventArgs e)
 {
   foreach (string item in Savestate.number)
   {
     label1.Text = Console.WriteLine(item)
   }
 }

Hope to get help. Thanks.

Rajan Mishra
  • 1,178
  • 2
  • 14
  • 30
masyita shariff
  • 110
  • 1
  • 8
  • `Savestate. number` is a list, that's why when you called `ToString()` it will shows `Systems.Collections.GenericList`. You need to get the element inside the list to print the actual string – currarpickt Jan 10 '19 at 06:21

2 Answers2

0

You should display items of the list.

private void Form2_VisibleChanged(object sender, EventArgs e)
{
    label1.Text = string.Join(", ", Savestate.number);
}

Or approach you already tried, but remove Console.WriteLine

private void Form2_VisibleChanged(object sender, EventArgs e)
{
    foreach (string item in Savestate.number)
    {
        label1.Text += item;
    }
}
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • Thanks for this. The first code did not work: The label become empty/data not exported. For the 2nd one, I change to label.Text = item; bcs the above code shows the label name and the textbox value, which wasn't what I want. – masyita shariff Jan 10 '19 at 07:30
0

you're trying to get a vlaue of the list. you can only get the last value of the LIST. and in the second code example you did wrong. Because Console.WriteLine doesnt work in win forms. Try to use this code:

foreach (string item in Savestate.number)
{
   label1.Text += item;
}
Umid Kurbanov
  • 174
  • 1
  • 5
  • Thanks for this. But instead I put label1.Text = item; bcs the above will show the label name + the textbox value, which is not what I want. – masyita shariff Jan 10 '19 at 07:27