Solved

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

Posted on 2006-06-13
2
359 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
orderby list (from Json) 1 29
"The resource cannot be found" 1 24
create dataset from text files to go on winforms gridview 3 25
Need help with another query 10 37
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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