C # Passing the value of a new form

I got a Windows form that is trying to transfer the value from this form to a new one when the button is clicked. via:

private void Edit_button_Click(object sender, EventArgs e)
        {
             for (int i = 0; i < listBox1.SelectedItems.Count; i++)
            {

             Edit item = new Edit(int.Parse(listBox1.SelectedIndex.ToString()));
            item.ShowDialog();

            }
        }

When I run the program, it does not show the form I created, it shows it instead problem

But when I change the code to this:

 Edit item = new Edit();
   item.ShowDialog();

run it, it will display the correct thing, but does not pass the value to the second form. enter image description here

Is there a way to pass the value to another form?

+5
source share
3 answers

Add Property

Edit item = new Edit();
item.Value = 5;
item.ShowDialog();

EDIT:

You must define this property in order to use it. Extend the class Editas follows:

class Edit {
    ...
    public int Value { get; set; }
    ...
}
+7
source

, InitiaizeComponent() . , , , .

, .

.

public class Edit
{
    public int val = 0;

    public Edit()
    { 
        InitializeComponent();
    }

    public Edit(int a)
    {
        val = a; 
        InitializeComponent();
    }

    public void Edit_Load()
    {
      txtbox.Text = val.ToString();
    }
}

,

Edit item = new Edit(5);
+1

Just guess: in your own constructor, you forgot to call IntializeComponents ().

+1
source

All Articles