gpt4 book ai didi

c# - 添加/删除单选按钮

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:31 26 4
gpt4 key购买 nike

在我的程序中,我使用 WPF ListBox 来显示 RadioButton。有两个按钮“添加帖子”和“删除帖子”。

我可以添加项目,但是有两个问题:

1)添加后的光标(点)如何定位到新项上?

2) 如何删除选中的项目并将光标定位到上一个?

enter image description here

// Add element
private void Button_Click_4(object sender, RoutedEventArgs e)
{
RadioButton obj = new RadioButton();
obj.Content = "Group " + ++numberOfGroups;
ListBox1.Items.Add(obj);
}

// Remove element
private void Button_Click_5(object sender, RoutedEventArgs e)
{
//..
}

编辑 1:

感谢您的回答,但您的代码适用于 List 元素。 定位光标 我的意思是,我需要选择一个单选按钮,而不是列表的元素。这张截图可能会更清楚:

enter image description here

最佳答案

如果您的意思是通过定位光标选择一个项目。您可以通过获取 ListBoxItemsCollection 的最后一个索引轻松地做到这一点:

// Add element
private void Button_Click_4(object sender, RoutedEventArgs e)
{
//add new item
RadioButton obj = new RadioButton();
obj.Content = "Group " + ++numberOfGroups;
ListBox1.Items.Add(obj);

//select last item
int LastItemIndex = ListBox1.Items.Count - 1;
ListBox1.SelectedItem = ListBox1.Items.GetItemAt(LastItemIndex);
}

// Remove element
private void Button_Click_5(object sender, RoutedEventArgs e)
{
//delete selected item
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);

//select last item
int LastItemIndex = ListBox1.Items.Count - 1;
ListBox1.SelectedItem = ListBox1.Items.GetItemAt(LastItemIndex);
}

关于c# - 添加/删除单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32033294/

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