gpt4 book ai didi

c# - 主表单字段仅显示列表框中第一项选择的数据

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:53 25 4
gpt4 key购买 nike

我有两个表单需要相互交互。父表单有 4 个字段和一个添加按钮,用于将每个字段的数据保存到类对象的实例中。将其保存到对象后,该对象将存储在子表单包含的列表框中。我创建了一个自定义事件来处理这些东西,但我肯定做错了什么。

应该发生的是,当两个窗口都打开并且列表框中有数据时,从子表单列表框中选择的任何项目都会使用该对象的数据填充父表单字段。当我测试我的代码时,只有第一项的数据正确填充了正确的字段。如果我在第一次选择后单击任何其他项目,主表单字段根本不会更新。

针对我的问题,子表单具有以下代码:

public EventHandler ListBoxItemClicked;
private void pPotionList_SelectedIndexChanged(object sender, EventArgs e)
{
PotionForm tempMain = new PotionForm(); //this was a test, nothing changed
pPotionList.SelectionMode = SelectionMode.One;
if (ListBoxItemClicked != null)
{
ListBoxItemClicked(this, new EventArgs());
}
tempMain.Refresh(); // this too
}

父表单有这些代码

private void pListDisplay_Click(object sender, EventArgs e)
{
PotionList secForm = new PotionList();

secForm.secFormBox.DataSource = potionBindList;

PotionListChanged += secForm.HandlePotionListChanged;

secForm.ChildPotionListChanged += HandleChildPotionListChanged;

secForm.ListBoxItemClicked += HandleListBoxItemClicked; //this line

secForm.Show();
}

public void HandleListBoxItemClicked(object sender, EventArgs e)
{
pTypeInput.SelectedItem = aPotion._type;
pMagInput.Value = aPotion._magnitude;
pNameInput.Text = aPotion._name;
pBonusInput.Checked = aPotion._bonus;
}

如果相关的话,我目前正在使用 Visual Studio Community 2015。

最佳答案

好的,在看到所有必要的代码后,我会说问题是您永远不会将值从子窗体传递给父窗体。每当事件 HandleListBoxItemClicked 被触发时,只有 aPotion 的初始值被写入控件。

作为一种解决方案,我建议在子窗体中触发 ListBoxItemClicked 事件时将 SelectedItem 作为发送者传递:

child

public EventHandler ListBoxItemClicked;
private void pPotionList_SelectedIndexChanged(object sender, EventArgs e)
{
Potion p = pPotionList.SelectedItem as Potion;

pPotionList.SelectionMode = SelectionMode.One;
if (p != null)
{
if (ListBoxItemClicked != null)
{
ListBoxItemClicked(p, new EventArgs());
}
}
}

您不能在父表单中使用此信息并随意分散信息:

家长

public void HandleListBoxItemClicked(object sender, EventArgs e)
{
Potion p_parent = sender as Potion;

if(p_parent != null)
{
pTypeInput.SelectedItem = p_parent._type;
pMagInput.Value = p_parent._magnitude;
pNameInput.Text = p_parent._name;
pBonusInput.Checked = p_parent._bonus;
}
}

现在不需要刷新或其他任何东西。希望对你有帮助

关于c# - 主表单字段仅显示列表框中第一项选择的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42060020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com