简体   繁体   中英

Get selecteditem of listbox to display on another form

I want to get selectedItem of listbox from a form 1 to display in a textbox in form2.

This is how I tried it:

Form 1. MainForm :

public partial class MainForm : Form
    {   

         public string GetListBoxSelectedItem()
        {

            if (Animallst.SelectedItem != null) //Animallst is the listbox
                return Animallst.SelectedItem.ToString();
            return string.Empty;
        }
}

Form 2 FoodRegister :

 public partial class FoodRegister : Form
    {
        private RecipeManager m_foodmanager = new RecipeManager();
        MainForm mainform = null;

        public FoodRegister() 
        {

            InitializeComponent();

            //My initializations
            InitializeGUI();
           MainForm mainform = new MainForm();
           Nametxt.Text = mainform.GetListBoxSelectedItem();
        }

        private void InitializeGUI()
        {
        }

}

I'm not getting any error message. The problem is that it only shows the item that was selected when MainForm is started. If the user changes the item selected, it still gets the one that was selected at first.

Update I tried this:

MainForm:

 public partial class MainForm : Form
 {
   private void Animallst_SelectedIndexChanged(object sender, EventArgs e)
     {

    if (Animallst.SelectedIndex > -1)
    {

        FoodRegister food = new FoodRegister();
        if (food != null)
        {
            food.AddToTextBox(Animallst.SelectedItem.ToString());
        }
    }  

FoodRegister (Form 2):

 public partial class FoodRegister : Form
    {
        public FoodRegister() 
        {         
            InitializeComponent();
        }

        public void AddToTextBox(string selectedItem)
        {
            Nametxt.Text = selectedItem;
        }

But it did not work

You need to add a selectedIndexChanged event to your listbox on your main form. Since your main form instantiates your second form, make a public method in your second form that the main form can call to add the selected item to the textbox.

//  This is in form 1
public partial class MainForm : Form
{
    // You only need to instantiate food once
    private FoodRegister food = new FoodRegister();

    private void MainForm_Load(object sender, EventArgs e)
    {
        if (food != null)
        {
            food.Show();
        }
    }

    private void Animallst_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Animallst.SelectedIndex > -1)
        {
            if (food != null)
            {
                food.AddToTextBox(Animallst.SelectedItem.ToString());
            }
        }
        else
        {
            // This will clear the text box on form 2
            food.AddToTextBox(string.Empty);
        }
    }
}

// This is in form 2
public partial class FoodRegister : Form
{
    public FoodRegister() 
    {         
        InitializeComponent();
    }

    public void AddToTextBox(string selectedItem)
    {
        Nametxt.Text = selectedItem;
    }
}

First you need to add SelectedIndexChanged event, just double clik your listBox in the designer window and VS will generate the method for you. Second, add a static variable to Form1 like this:

public partial class MainForm : Form
{
    static string myListBoxString;
    public Form1()
    {
        InitializeComponent();
    }
    ...
}

Later in your Form1 you'll update the static variable whenever you select an item from the ListBox:

private void Animallst_SelectedIndexChanged(object sender, EventArgs e)
{
    myListBoxString = Animallst.SelectedItem.ToString();
}

Now, to use this variable in your Form2, you can easily access it as follows:

public partial class FoodRegister : Form
{
    private RecipeManager m_foodmanager = new RecipeManager();
    //MainForm mainform = null;

    public FoodRegister()
    {

        InitializeComponent();

        //My initializations
        InitializeGUI();
        //MainForm mainform = new MainForm(); 
        //No need to instantiate the form now, since your variable is static, 
        //which means it depends on the class not an instance of the class.
        Nametxt.Text = MainForm.myListBoxString;
    }

    private void InitializeGUI()
    {
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM