gpt4 book ai didi

c# - 触发 SelectedIndexChanged 后列表框在视觉上卡住

转载 作者:行者123 更新时间:2023-12-05 08:03:37 26 4
gpt4 key购买 nike

我有一个结构如下的表单应用程序:

+--------------------------------------------------------------+
| FormMain |
|+------------------------+-----------------------------------+|
|| Panel ObjectCreation | Panel ObjectShow ||
|+------------------------+-----------------------------------+|
|| Button AddObject() | listPhones[selectedItem].Id ||
|| ListBox listBoxPhones | ||
|+------------------------+-----------------------------------+|
+--------------------------------------------------------------+

两个面板都显示两个单独的窗体,FormObjectCreation 和 FormObjectShow。一旦 ListBox 触发 SelectedIndexChanged 事件,Panel ObjectShow 就会更新。

但是,此后列表框不再在视觉上更新。它仍然有效,仍然触发事件。 The listbox just seems to be frozen visually.

这是按钮调用的 AddObject() 函数:

private void buttonAddObject_Click(object sender, EventArgs e)
{
PhoneList.Add(new Phone(PhoneList.Count + 1));

buttonSentTokensToPhone.Visible = true;
listBoxPhones.DataSource = null;
listBoxPhones.DataSource = PhoneList;
}

这里是处理引发事件的函数

private void listBoxPhones_SelectedIndexChanged(object sender, EventArgs e)
{
this.FormMain.ShowCurrentPhone(PhoneList[listBoxPhones.SelectedIndex]);
}

// In FormMain.cs:
public void ShowCurrentPhone(Phone phone)
{
panelPhone.Controls.Clear();
FramePhone = new FrmPhone(this, phone) { Dock = DockStyle.Fill, TopLevel = false, TopMost = true };
FramePhone.Name = "Phone " + phone.Id;
FramePhone.FormBorderStyle = FormBorderStyle.None;
panelPhone.Controls.Add(FramePhone);
panelPhone.BorderStyle = BorderStyle.FixedSingle;
FramePhone.Show();
}

我试过调试它,但是我没有发现任何异常。我在 .NET 4.7.2 上。有没有人也遇到过这个问题?

更新:正在关注Jimis在下面的评论中,我将 PhoneList 从常规列表更改为 BindingList。代码片段现在看起来像这样:

private BindingList<Phone> PhoneList { get; set; }

private void buttonAddPhone_Click(object sender, EventArgs e)
{
PhoneList.Add(new Phone(PhoneList.Count + 1));
buttonSentTokensToPhone.Visible = true;
}

ListBox 现在可以正常工作了。

最佳答案

我用一个表单和一个用户控件(不是表单)做了这个并且它有效:

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

public List<Phone> PhoneList { get; private set; }

private void button1_Click(object sender, EventArgs e)
{
PhoneList = new List<Phone>();

PhoneList.Add(new Phone(PhoneList.Count + 1));


listBox1.DataSource = null;
listBox1.DataSource = PhoneList;
listBox1.DisplayMember = "Id";
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1!=null && listBox1.SelectedIndex>=0)
ShowCurrentPhone(PhoneList[listBox1.SelectedIndex]);
}
public void ShowCurrentPhone(Phone phone)
{
panel2.Controls.Clear();
UserControl1 FramePhone = new UserControl1(phone.Id);
FramePhone.Name = "Phone " + phone.Id;
panel2.Controls.Add(FramePhone);
panel2.BorderStyle = BorderStyle.FixedSingle;
}
}

public class Phone
{
private int id;
public string PonheNumber;

public int Id { get => id; set => id = value; }

public Phone(int id)
{
this.id = id;
this.PonheNumber = "1111";
}
}


public partial class UserControl1 : UserControl
{
private int id;
public UserControl1(int id)
{
InitializeComponent();
this.Id = id;
label1.Text = Id.ToString();
}

public int Id { get => id; set => id = value; }
}

关于c# - 触发 SelectedIndexChanged 后列表框在视觉上卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71968433/

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