How do you block user input (ie. changing the selected index) on a listbox without changing the text colour, like if you were to just set the enabled property to false on the control?
This is for WinForms.
I assume you want a default selection from the ListBox that the user won't be able to change? My favorite approach to this issue is by Disabling the user from accessing the ListBox. You can do this by adding a GotFocus event on the ListBox and setting the Focus to another control whenever the ListBox gets focus. Something like this:
private void listBox1_GotFocus(Object sender, EventArgs e) {
this.Select();//set the form as the active control or even this.Focus();
}
Best option is to not do that -- instead, in this situation, only populate the one item you want displayed/selected.
Otherwise, you'll need to:
- remember the current selection
- handle OnSelectedIndexChanged
- reset the selection (handling potential recursion)
Related
I'm working on a form and I need to change the behaviour of some controls when some actions are given by the user. Since there are many controls to handle, meaning buttons, comboboxes, and others, do you know how I can handle methods for all those without handling one by one? Like a loop or something.
Like, for example, for comboboxes I need to overwrite the drawItem event with some code to change their appearance.
I can't find anything on google.
I did it, in the end. I set for the many comboboxes the same event handler/method, selecting all the comboboxes and then setting the handler name in the properties/events tab (in my case, in the DrawItem handler).
Then in the code the method comes by default with (object sender, DrawItemEventArgs e) so I set:
var combo = sender as ComboBox;
and with it I can generally get or set what I need
If I put a listview component into a windows form and add bellow code to it's SelectedIndexChanged event:
MessageBox.Show("Fired!");
foreach (int selectedIndex in listView1.SelectedIndices)
{
listView1.Items[selectedIndex].Selected = false;
listView1.Items[selectedIndex].Focused = false;
}
the message box will be shown 4 times! why is that?
Note: I use the loop to clear the selected items in listview
You should not change the selection in a SelectedIndexChanged event. More generally, you should not change a property inside of a notification that the property has been changed.
If you need to change a property in response to a notification, look to handle the corresponding *Changing event. Rather than being a notification that something has changed (which comes after the fact), it is a notification that something is about to change (which comes before the fact). In the SelectedIndexChanging event, you have a couple of different options to alter the course of events:
You can set the e.Cancel property to true, which will do just as it says. It will cancel the event and prevent the selected index from changing.
You can use the e.NewSelectedIndex property to alter the selection. Just set this property to the index of the item that you want to be selected.
And if you want to clear the selected items in a ListView in response to some other event (e.g., a click on a "Clear Selection" button that is not part of the ListView, or a similar context menu item), you don't need a loop at all. Just clear the control's SelectedItems collection: myListView.SelectedItems.Clear(). Again, you can't do this in response the SelectedIndexChanged event, or you'll have the same problem of triggering a bunch of notifications.
Honestly, though, the code you've written here makes no sense. Why would you want to clear all selected items when the user tries to select an item? If you don't want to allow selection, disable the control by setting its Enabled property to false.
I have created a comboBox in my windows form that is auto filled by some database data. When i selected an index there are some label on the win form to capture other details of particular index. but the problem is when i click another control after making a selection , that combobox reset its value to one of my index.
There are two common causes for this:
Does the comboBox have a Leave or LostFocus event that is causing the change?
When you click the other control, is an event fired that will affect the comboBox, either directly or indirectly?
Using this question (Inline editing TextBlock in a ListBox with DataTemplate (WPF) I now have a ListBox that can be doubleclicked to edit the items in it. What I want now is to have a Button on the form, which when clicked will add a new item to the ListBox (this is easy), but then change the ListBoxItem into editmode, so the user can enter the value right away. How would you select the right ListBoxItem, and then find the TextBlock and TextBox inside it and change the visibility of them using the SelectedIndex?
I know this is a really late answer for this, but have you considered adding BeginEdit and EndEdit methods to your items? You could then do something like:
CustomListBoxItem foo = new CustomListBoxItem();
customListBoxInstance.Add(foo);
foo.BeginEdit();
I had to do that with a couple of my custom controls that needed to be created and immediately enter edit mode. You'd end up with something like:
private void TextBlock1_DoubleClick(object sender, RoutedEventArgs e)
{
BeginEdit();
}
public void BeginEdit()
{
// Code to put the item into edit mode.
}
I'd need to see some more code to give a more precise answer, but that's worked really well in my experience for controlling whether or not a control is in edit mode from outside the scope of that control.
I have a ListView control, and I'm trying to figure out the easiest/best way to disallow changing the selected row(s), without hiding the selected row(s).
I know there's a HideSelection property, but that only works when the ListView is still enabled (but not focused). I need the selection to be viewable even when the ListView is disabled.
How can I implement this?
You could also make the ListView ownerdraw. You then have complete control over how the items look whether they are selected or not or whether the ListView itself is enabled or not. The DrawListViewItemEventArgs provides a way to ask the ListView to draw individual parts of the item so you only have to draw the bits you're interested in. For example, you can draw the background of the item but leave it up to the ListView to draw the text.
There are two options, change the selected rows disabled colors. Or change all the other rows to simulate they are disabled except for the selected one. The first option is obviously the easiest, and the second option obviously is going to need some extra protections.
I have actually done the first option before and it works quite well. You just have to remember to change the colors back to the defaults in case another row is selected later on in the process.
Implement SelectedIndexChanged and do this
private void listViewABC_SelectedIndexChanged(object sender, EventArgs e)
{
listViewABC.SelectedItems.Clear();
}