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

  • Home
  • SEARCH
  • 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 8700699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:13:40+00:00 2026-06-13T02:13:40+00:00

In the constructor i did: if (listBox1.Items != null) { listBox1.Focus(); } But when

  • 0

In the constructor i did:

if (listBox1.Items != null)
            {
                listBox1.Focus();
            }

But when im running the program i cant move with the keyboards up down in listBox since the focus is on a button somewhere else in the Form. I need to click with the mouse on the listBox to get the focus.

Another problem i want that when the user add a new item to the listBox the focus will be automatic on the last added item. For this problem this is the code where im adding a new item to the listBox:

private void KeysValuesUpdate()
        {
            using (var w = new StreamWriter(keywords_path_file))
            {
                crawlLocaly1 = new CrawlLocaly();
                crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
                DialogResult dr = crawlLocaly1.ShowDialog(this);
                if (dr == DialogResult.OK)
                {
                    if (LocalyKeyWords.ContainsKey(mainUrl))
                    {
                        LocalyKeyWords[mainUrl].Clear();
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    else
                    {
                        LocalyKeyWords[mainUrl] = new List<string>();
                        LocalyKeyWords[mainUrl].Add(crawlLocaly1.getText());
                    }
                    Write(w);
                    ClearListBox();
                }
                if (dr == DialogResult.Cancel)
                {
                    Write(w);
                }
            }
        }

        private void ClearListBox()
        {
            data.Clear();
            listBox1.DataSource = null;
            string sb;
            foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
            {
                for (int i = 0; i < kvp.Value.Count(); i++)
                {
                    sb = "Url: " + kvp.Key + " --- " + "Local KeyWord: " + kvp.Value[i] + Environment.NewLine;
                    data.Add(sb.ToString());
                }
            }
            listBox1.DataSource = data;
        }

The question is why i cant set the focus in any of the cases on the listBox items ?

In the first case in the constructor the focus i want it to be on the last item in the list and also each time im adding a new item so the focus will be on the last added item.

  • 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-13T02:13:41+00:00Added an answer on June 13, 2026 at 2:13 am

    Most likely, the item is being selected, you just can’t tell because a different control has the focus. There are a couple of different ways that you can solve this, depending on the design of your application.

    1. For the first part of the question, you should set the Focus in the Page/Form Load event, since at the constructor level controls are under initialization process.
      Set the focus to the ListView first whenever your form is displayed. The user typically sets focus to controls by clicking on them. However, you can also specify which controls gets the focus programmatically. One way of doing this is by setting the tab index of the control to 0 (the lowest value indicates the control that will have the initial focus). A second possibility is to use the following line of code in your form’s Load event, or immediately after you set the Selected property:

      listBox1.Select();
      

      The problem with this solution is that the selected item will no longer appear highlighted when the user sets focus to a different control on your form (such as a textbox or a button).

    2. For the second part of the question, selecting last added item in the ListBox, use the following code:

      listBox1.SelectedIndex = listBox1.Items.Count - 1;
      listBox1.SetFocus();
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought the GC would call Dispose eventually if your program did not but
I'm extending a constructor class' functionality via the prototype method, but I'm having trouble
Basically, I want to modify the constructor of the Object class. Since every class
I have very obvious problem, but still did not found solution to it. I
First off, I did look on google and stackoverflow, but nothing seem to really
We're trying to create a ListBox which has a few items inside that the
I did some Google search but could not find anything. So I implemented something
I've searched but I did not find a good answer. I've used an ArrayList
I wanted to make a templated constructor, but I found I had no way
I did look at the related posts that were answered, but still get the

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.