Solved

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

Posted on 2006-06-13
2
358 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
ID: 16894236
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
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

861 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