Solved

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

Posted on 2006-06-13
2
355 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
2 Comments
 
LVL 14

Assisted Solution

by:existenz2
existenz2 earned 200 total points
Comment Utility
In the onclick event from the buttons add:

listBox1.Focus();

that should fix it
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
Comment Utility
"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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now