C# 列表框设置选中项

发布于 2024-10-07 15:03:23 字数 133 浏览 0 评论 0原文

Profile 1
Profile 2
Profile 3

我有一个 C# 列表框,其中包含我希望在加载表单时选择配置文件 2 的 。我该怎么做?

i have a C# listbox with the values

Profile 1
Profile 2
Profile 3

I want to have Profile 2 selected when the form loads. How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昔日梦未散 2024-10-14 15:03:24

设置 ListBox.SelectedIndex 中的属性Form.Shown 事件。
例如:

public Form1()
{
    InitializeComponent();

    // Adding the event handler in the constructor
    this.Shown += new EventHandler(Form1_Shown);
}    

private void Form1_Shown(object sender, EventArgs e)
{
    myListBox.SelectedIndex = 1;
}

Set the ListBox.SelectedIndex property in the Form.Shown event.
For example:

public Form1()
{
    InitializeComponent();

    // Adding the event handler in the constructor
    this.Shown += new EventHandler(Form1_Shown);
}    

private void Form1_Shown(object sender, EventArgs e)
{
    myListBox.SelectedIndex = 1;
}
﹏半生如梦愿梦如真 2024-10-14 15:03:24

将以下代码放入 Form.Loaded 事件中:

listBox1.SelectedItem = "Profile 2";

Put following code in Form.Loaded event:

listBox1.SelectedItem = "Profile 2";
九局 2024-10-14 15:03:24
listBox1.Items.Add("Profile 1");

listBox1.Items.Add("Profile 2");

listBox1.Items.Add("Profile 3");

listBox1.SelectedIndex = 1;
listBox1.Items.Add("Profile 1");

listBox1.Items.Add("Profile 2");

listBox1.Items.Add("Profile 3");

listBox1.SelectedIndex = 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文