Mike Eghtebas
asked on
prevent lstBox item at index =0 ever to be selected
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.
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.
ASKER
vr6r,
Sorry for the delay. The list boxes are generated on the fly:
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
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);
}
}
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
}
}
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Open in new window
Code
Open in new window