获取列表框的选择项以在另一个表单上显示

时间:2015-04-08 21:49:44

标签: c# visual-studio-2013

我希望从表单1中获取listbox的selectedItem,以便在form2的文本框中显示。

这是我尝试的方式:

表格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;
        }
}

表格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()
        {
        }

}

我没有收到任何错误消息。问题是它只显示MainForm启动时选择的项目。如果用户更改了所选项目,它仍然会获得最初选择的项目。

更新我试过了:

的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(表格2):

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

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

但它不起作用

2 个答案:

答案 0 :(得分:0)

您需要将selectedIndexChanged事件添加到主窗体上的列表框中。由于您的主表单实例化您的第二个表单,请在您的第二个表单中创建一个公共方法,主表单可以调用该方法将所选项目添加到文本框中。

//  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;
    }
}

答案 1 :(得分:0)

首先你需要添加SelectedIndexChanged事件,只需在设计器窗口中双击clBox,VS就会为你生成方法。 其次,将static变量添加到Form1,如下所示:

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

稍后在Form1中,只要从ListBox中选择一个项目,就会更新静态变量:

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

现在,要在Form2中使用此变量,您可以按如下方式轻松访问它:

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()
    {
    }

}