Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9110945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:23:20+00:00 2026-06-17T03:23:20+00:00

I have a UserControl in a Form that contains a Listbox. I would like

  • 0

I have a UserControl in a Form that contains a Listbox. I would like to automatically select the first item in the Listbox (assuming there is at least one item) but I cannot get the following code to work:

private void Lightnings_Mode_Load(object sender, EventArgs e)
        {
            this.Size = new Size(416, 506);
            this.Location = new Point(23, 258);
            listBoxIndexs();
            listBoxControl1.MyListBox.SelectedIndex = 0;
            if (this.listBoxControl1.MyListBox.Items.Count > 0)
                this.listBoxControl1.MyListBox.SelectedIndex = 0;
            listBoxControl1.MyListBox.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
            this.listBoxControl1.ItemRemoved += new EventHandler<ItemEventArgs>(listBoxControl1_ItemRemoved);
        }

This line: listBoxControl1.MyListBox.SelectedIndex = 0; will mark the first ListBox item in blue like it is selected. But it’s not realy selecting the item!

So I tried to add this:

if (this.listBoxControl1.MyListBox.Items.Count > 0)
                    this.listBoxControl1.MyListBox.SelectedIndex = 0;

But it’s not working either.

This is the SelectedIndex event:

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            item = listBoxControl1.MyListBox.SelectedItem.ToString();
            this.f1.PlayLightnings();
            f1.pdftoolsmenu();
            int indx = listBoxControl1.MyListBox.SelectedIndex;
            if (listBoxControl1.Indices.Contains(indx))
            {
                if (item != null && !pdf1.Lightnings.Contains(item.ToString()))
                {
                    pdf1.Lightnings.Add(item.ToString());
                }
            }
        }

The name of the event is not right I have to change it since it’s the ListBox over the UserControl but it’s the right one.

When I put a breakpoint in the SelectedIndex event and I click on an item, it stops at the breakpoint. But I want it to go to the selectedIndex event automatically once I show/open the new Form with the UserControl and the ListBox.

So if I put a breakpoint in the SelectedIndex event, when I click the button in Form1 to show/open the new Form it will automatically stop in the breakpoint like I was clicking the first item.

This is in Form1 the code that show the new Form:

if (toolStripComboBox2.SelectedIndex == -1 && toolStripComboBox1.SelectedIndex == -1)
            {
            }
            else
            {
                Lightnings_Extractor.Lightnings_Mode lightningsmode1 = new Lightnings_Extractor.Lightnings_Mode(this);
                lightningsmode1.Show();
            }

Everything is working except for selecting the first item automatically.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:23:22+00:00Added an answer on June 17, 2026 at 3:23 am

    http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.setselected(v=vs.90).aspx

    instead of

    if (this.listBoxControl1.MyListBox.Items.Count > 0)
                        this.listBoxControl1.MyListBox.SelectedIndex = 0;
    

    Try this:

    if (this.listBoxControl1.MyListBox.Items.Count > 0)
                        this.listBoxControl1.MyListBox.SetSelected(0,true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web form that contains a usercontrol and I would like to
In a Windows Form application I have a Form with a UserControl that contains
I have a Windows Form that contains a custom control container as a UserControl.
I have an UserControl that contains a Button : <UserControl> <Button> </UserControl> I want
I have a View that contains a usercontrol. The usercontrol is rendered using: <%
I have one UserControl nested inside the other UserControl, something like that: <uc:MyControl1> <uc:MyControl2
I have a form with two different UserControls - one that contains a Telerik
I have an asp.net usercontrol that contains a jQuery UI Dialog Control. All works
I have an assembly which contains several UserControl objects that I want to be
I have a parent form, which contains panels that calls different user control. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.