简体   繁体   English

无法将selectedItem获取到文本框

[英]Cannot get selectedItem to textbox

I'm trying to get listbox selected item from one form 1 to display on textbox on form 2. 我正在尝试从一种表单1获取列表框选定的项目,以显示在表单2的文本框中。

So far it's working partly. 到目前为止,它部分起作用。

The problem is that it only gets the selectedItem that was selected at the start of the application. 问题在于它仅获取在应用程序启动时选择的selectedItem。 If the user selects a new item, it still gets the one that was selected as default. 如果用户选择一个新项目,它仍然会获得被选择为默认项目的项目。

Form 1 MainForm : 表格1 MainForm

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

    private void foodbtn_Click(object sender, EventArgs e)
    {
        FoodRegister foodForm = new FoodRegister();
        foodForm.Show();
    }
}

Form 2 FoodRegister : 表格2 FoodRegister

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

    public FoodRegister() 
    {
        InitializeComponent();

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

These two lines are not at all doing what you want them to do. 这两行根本不执行您希望它们执行的操作。 You're creating an entirely new instance of MainForm , which has nothing to do with the original instance. 您正在创建MainForm全新实例,该实例与原始实例无关。 And so GetListBoxSelectedItem() doesn't do what you want either. 因此, GetListBoxSelectedItem()也不执行您想要的操作。

MainForm main = new MainForm();
Nametxt.Text = main.GetListBoxSelectedItem();

Instead, pass a reference to the original Form into the second Form: 而是将对原始表单的引用传递到第二个表单中:

public FoodRegister(MainForm main) 
{
    InitializeComponent();

    Nametxt.Text = main.GetListBoxSelectedItem();
    ...

And then call it like this: 然后这样称呼它:

FoodRegister foodForm = new FoodRegister(this);
foodForm.Show();

A couple of things to mention: 有两件事要提到:

  1. The line return string.Empty is redundant. return string.Empty是多余的。 Because of the line above it, this line becomes unreachable 由于它上面的线,这条线变得无法到达

  2. In your FoodRegister for, you create a new instance of your main form. 在您的FoodRegister中,您将创建主窗体的新实例。 This then wipes anything that the main form was holding - ie Animallst.SelectedItem.ToString(); 然后,这会擦除主窗体中保存的所有内容,即Animallst.SelectedItem.ToString();

An easy way to handle this is to set the value to a static variable - that way you won't have to create a new instance of the form to access it. 处理此问题的一种简单方法是将值设置为静态变量-这样一来,您无需创建表单的新实例即可访问它。

Main form: 主要形式:

public static string GetListBoxSelectedItem()
{
    if (Animallst.SelectedItem != null) //Animallst is the listbox
    {
        return Animallst.SelectedItem.ToString();
    }
    else { return string.Empty(); }
}

Food Register: 食品注册:

public FoodRegister() 
{
    InitializeComponent();

    MainForm.GetListBoxSelectedItem();
    //My initializations
    InitializeGUI();
}

Haven't played with WinForms in awhile but here goes In Form 2 一段时间没有玩WinForms,但是在Form 2中

public partial class FoodRegister : Form
{
    private RecipeManager m_foodmanager = new RecipeManager();
    public FoodRegister() 
    {
        InitializeComponent();
        //My initializations
        InitializeGUI();
    }
    public void SetText(string txt)
    {
         Nametxt.Text = txt;
    }
}

In Form 1 在表格1中

public MainForm()
{
    private readonly FoodRegister foodForm = new FoodRegister();

    private void foodbtn_Click(object sender, EventArgs e)
    {
        foodForm.SetText(Animallst.SelectedItem == null ? "" : Animallst.SelectedItem.ToString());
        foodForm.Show();
    }
}

I replaced 我更换了

GetListBoxSelectedItem()

with

Animallst.SelectedItem == null ? "" : Animallst.SelectedItem.ToString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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