Solved

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

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

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

737 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