Solved

prevent lstBox item at index =0 ever to be selected

Posted on 2014-11-15
4
97 Views
Last Modified: 2014-11-18
There is a list box with some items in it. Upon user click, the item value is printed on the screen.

Question: How can I make item at index 0 not to be selected ever?

What event I should be using. What code I should be using to perform as described below:

1. If the selected index is -1 and user selects item 0, nothing happens.
2. If the selected index is 3, for example, and user selects item 0, item 3 still remains selected preferably without postback.
0
Comment
Question by:Mike Eghtebas
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:vr6r
ID: 40445383
Could do something like this...

On item at listbox index 0 do the following.
1. set the following properties to False:
    IsTabStop
    IsHitTestVisible

IsHitTestVisible prevents mouse selection by preventing hit test results for the element's rendered content.
IsTabStop prevents navigating to the element by tabbing to the control or up-arrowing when the listbox has focus.

2. Handle event "SelectionChanged" on the parent listbox control and set SelectedIndex = -1 if selectedIndex = 0.

This prevents cases where the user starts typing the text of the listbox item when the listbox control has focus.  By default the listbox will autoselect the item that matches the text the user is typing.

Here is the xaml and code behind for reference:

XAML
<ListBox x:Name="mylist" HorizontalAlignment="Left" Width="Auto" SelectionChanged="mylist_SelectionChanged">
    <ListBoxItem IsTabStop="False" IsHitTestVisible="False">option 0</ListBoxItem>
    <ListBoxItem>option 1</ListBoxItem>
    <ListBoxItem>option 2</ListBoxItem>
    <ListBoxItem>option 3</ListBoxItem>
    <ListBoxItem>option 4</ListBoxItem>
</ListBox>

Open in new window


Code
private void mylist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (mylist.SelectedIndex == 0)
        mylist.SelectedIndex = -1;
}

Open in new window

0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40448959
vr6r,

Sorry for the delay. The list boxes are generated on the fly:
  Dictionary<string, bool> listBoxes =   new Dictionary<string, bool>();
        listBoxes.Add("First_Name", false);
        listBoxes.Add("Last_Name", true);
        listBoxes.Add("Address", true);
        listBoxes.Add("City", true);
.
.
   private void AddListBoxes(Dictionary<string, bool> listBoxes)
    {
        Panel1.Controls.Clear();

        foreach (string box in (from listBox in listBoxes where listBox.Value select listBox.Key))
        {
              
            ListBox lb = new ListBox() { ID = "lst" + box };

            colCount += 1;
            if (colCount == colInRow + 1)
            {
                left = 50;
                top = 60 + height +50;
            }

            lb.Style["POSITION"] = "absolute";
            lb.Style["LEFT"] = left + "px";
            lb.Style["TOP"] = top + "px";
            lb.AutoPostBack=true;
            lb.DataTextField = box;
            lb.Style["Width"] = width + "px";
            lb.Style["Height"] = height + "px";
            lb.TextChanged +=new EventHandler(ListBoxSelectedIndexChanged);
            left += gap + width;
            left_old = left;
            Panel1.Controls.Add(lb);
            LoadListBoxes(lb);
        }
    }

Open in new window

I am using your code like this:
  private void ListBoxSelectedIndexChanged(object sender, EventArgs e)
    {
        if (mylist.SelectedIndex == 0)
        {
              mylist.SelectedIndex = -1;
        }
         else
         {
            // some other code to be added later
         }
    }

Open in new window


I have never used XAML. This will be new and exiting to learn how to apply it.

Question 1: Shall I just add an xml file and save it as XAML.xml to hold your code? How this is done?

Question 2: I possibly could include (by guesswork) how to include IsTabStop and IsHitTestVisible properties in AddListBoxes() method; but if possible at all please add them to AddListBoxes() starting on line 8, first box in this post.

Thank you,

Mike
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40449116
Hi vr6r,

I stumbled to something I want to run by you. Say we have the following items in our list box:

Jack    when I a "-" in front of Jack, upon click on this item the ChangeIndex event doesn't fire
Robert
Mike

Like:

-Jack    
Robert
Mike

This basically solves the problem. I wonder if you could take a look at it. I want to know how reliable is this.

Mike
0
 
LVL 8

Accepted Solution

by:
vr6r earned 500 total points
ID: 40451622
Honestly I have never tryied adding a dash in front of a list item to disable it so I couldn't say how reliable it is.

As far as xaml goes, you would generally use it by starting with a WPF c# project.  The xaml defines the UI of your app and you put your c# in the .cs code behind.  A quick google search will tell you much more than I would be able to - but generally speaking it is very flexibe & customizable.  Any desktop app I work on these days is WPF if I can help it at all.

So if you want to try tackling wpf then you can implement that sample as I posted previously.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net How to Exit Sub - Exit Form??? 5 57
C# HTTP GET method sample code 3 57
Connection String 16 56
SQL Exceptions 3 43
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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