Solved

500pts: ListBox (winform)- set to NOT lose focus when clicking some buttons?

Posted on 2006-06-13
2
362 Views
Last Modified: 2010-07-27
Hi there,

I created a standard listbox (VS2003-winforms) with some buttons at the side saying Up and Down which basically moves the selected item up and down the listbox.

Works great but of course as soon as i hit either up or down i loose focus on the listbox.... is it possible to set it not to lose focus....

As at the moment when i click UP for example that i have rehighlight the item to press Up again

Thanks in advance for any help

Ian
0
Comment
Question by:ianinspain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Assisted Solution

by:existenz2
existenz2 earned 200 total points
ID: 16894236
In the onclick event from the buttons add:

listBox1.Focus();

that should fix it
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 16894366
"As at the moment when i click UP for example that i have rehighlight the item to press Up again"

So you don't really need the ListBox focused again but instead need the item you just moved to be selected again.  This will allow you to continue clicking the up/down button to keep the same item moving...

        private void button1_Click(object sender, EventArgs e)
        {
            // move item up
            int index = listBox1.SelectedIndex;
            if (index > 0)
            {
                object tmp = listBox1.Items[index];
                listBox1.Items.RemoveAt(index);
                listBox1.Items.Insert(--index, tmp);
                listBox1.SelectedIndex = index;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // move item down
            int index = listBox1.SelectedIndex;
            if (index != -1)
            {
                if (index < listBox1.Items.Count - 1)
                {
                    object tmp = listBox1.Items[index];
                    listBox1.Items.RemoveAt(index);
                    listBox1.Items.Insert(++index, tmp);
                    listBox1.SelectedIndex = index;
                }
            }
        }
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question