I have a ListBox that when an item is selected, it is shown in a label as well. However, when I want to remove the selected item, program breaks and shows a NullReferenceException.
My code:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = "Your Selected: " + listBox1.SelectedItem.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
label1.Text = "";
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
It may appear, that there's no selected item in the listbox, so you have to check for that:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = null == listBox1.SelectedItem
? ""
: "Your Selected: " + listBox1.SelectedItem.ToString();
}
private void button2_Click(object sender, EventArgs e) {
// Looks redundant, listBox1_SelectedIndexChanged will do
//label1.Text = "";
// Deselect item, but not remove it
if (listBox1.SelectedIndex >= 0)
listBox1.SelectedIndex = -1;
// In case you want to remove the item (not deselect) - comment out the code below
// if (listBox1.SelectedIndex >= 0)
// listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
Edit: as for counting listbox items, there's no event fo this in the current listbox implementation. So you have to do it manually:
if (listBox1.SelectedIndex >= 0) {
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
lbItemsCount.Text = listBox1.Items.Count.ToString();
}
Another way is to use click event of the list box , if we do not want to double click the one list box item for the deselection of another list items.
ex:
private void ListBox_Right_Click(strong textobject sender, EventArgs e)
{
Btn_Left.Enabled = ListBox_Right.SelectedIndex >= 0;
ListBox_Left.ClearSelected(); // to clear the list selection/highlight
Btn_Right.Enabled = false; // for my specification
}
}
private void ListBox_Left_Click(object sender, EventArgs e)
{
Btn_Right.Enabled = ListBox_Left.SelectedIndex >= 0;
ListBox_Right.ClearSelected(); //to clear the list selection/highlight
Btn_Left.Enabled = false;// for my specification
}
Related
I need to select values from selected item in a drop down list in asp.net. in this code:
protected void EducationFeildsList_SelectedIndexChanged(object sender, EventArgs e)
{
int index = Convert.ToInt32(EducationFeildsList.SelectedIndex);
Label1.Text = index.ToString(CultureInfo.InvariantCulture);
}
But it seems that the value could not be read and so label1.text wasn't changed. how I could get the correct value of a selected item in this situation?
protected void EducationFeildsList_SelectedIndexChanged(object sender, EventArgs e)
{
If (!IsPostback)
{
Label1.Text = Dropdownlist1.Selectedvalue;
}
}
Set AutoPosback prperty of DDL to TRUE
Use the Parse
protected void EducationFeildsList_SelectedIndexChanged(object sender, EventArgs e)
{
int index = int.Parse(EducationFeildsList.SelectedIndex);
Label1.Text = index.ToString(CultureInfo.InvariantCulture);
}
I have a textbox in a Windows form, I have clear button which basically clears the text char by char(that is one by one not all at once), my problem is i want to add another textbox to this form and would like to control both the textboxes with clear button meaning the clear should only clear the textbox which i have selected or clicked on, i tried doing it but either i am able to clear both the textboxes simultaneously or clear only textbox my code for single textbox is
private void clearBtn_Click(object sender, EventArgs e)
{
string s = txtID.Text;
if (s.Length > 0) txtID.Text = s.Substring(0, s.Length - 1);
}
You can set the which control has focus upon focus and then use that to see which one needs to be removed.
private Textbox SelectedTextBox;
protected void Form_Load(object sender, EventArgs e)
{
TextBox1.GotFocus += TextBox_GotFocus;
TextBox2.GotFocus += TextBox_GotFocus;
}
private void clearBtn_Click(object sender, EventArgs e)
{
if(this.SelectedTextBox == null) return;
string s = this.SelectedTextBox.Text;
if (s.Length > 0) this.SelectedTextBox.Text = s.Substring(0, s.Length - 1);
}
private void TextBox_GotFocus(object sender, EventArgs e)
{
this.SelectedTextBox = (Textbox)sender;
}
private void clearBtn_Click(object sender, EventArgs e)
{
sting textbox1text = textbox1.tostring();
string textbox2text = textbox2.tostring();
if (textbox1text.length > 0){
textbox1text = textbox1text.Remove(textbox1text.length - 1)
}
if (textbox2text.length > 0){
textbox2text = textbox2text.Remove(textbox1text.length - 1)
}
}
that will "backspace" one character of each textbox.
if you want to do a clear on the one you last updated textbox, add this to each if statement
if (textbox2text.length > 0 && textbox2text == textbox2text)
that will check if the textbox was updated before it clears, it wont clear if it hasnt been updated
I have these two methods and I have to use them to modify the selected item in a ListBox and after editing it in the same txtBox I use for filling up the ListBox to get it back and replace It with the old one.
private void txtBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
List.Items.Add(Label.Content);
Label.Content = "";
txtBox.Text = "";
}
}
private void ButtonModify_Click(object sender, RoutedEventArgs e)
{
int index = List.SelectedIndex;
object item = List.SelectedItem;
txtBox.Text = (string)item;
txtBox_KeyUp(????????);
}
My intuition says I have to call the EventMethod I've use to fill the ListBox, but there are some parameter which I can't remove cause I need them in the txtBody_KeyUp(). So my question is what I have to write as an arguments to work or is there some other way for doing this?
You Can Use Below Code.
Text Box PreviewKeyDown Event
private int _tmpIndex = -1;
private void TextBox_OnPreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key != Key.Enter || _tmpIndex == -1) return;
ListBox1.Items[_tmpIndex] = TextBox1.Text;
TextBox1.Text = "";
_tmpIndex = -1;
}
ListBox MouseDoubleClick Event
private void ListBox_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if (ListBox1.SelectedIndex <= 0) return;
TextBox1.Text = ListBox1.SelectedItem.ToString();
_tmpIndex = ListBox1.SelectedIndex;
}
Have Fun
Windows Forms application (c#).
I have two ComboBoxes.
If I select an item in one, I want the text in the other one to be blank.
This is what I have:
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox2.Text = "";
}
private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox1.Text = "";
}
... but as you can see, when I make a selection in one, the text in both ComboBoxes get cleared.
How to accomplish this?
Thank you.
Try setting the ComboBox.SelectedIndex to -1
A zero-based index of the currently selected item. A value of negative
one (-1) is returned if no item is selected.
or rather based on your specifications try something lie
private bool changed = false;
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!changed)
{
changed = true;
ComboBox2.Text = "";
changed = false;
}
}
private void ComboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (!changed)
{
changed = true;
ComboBox1.Text = "";
changed = false;
}
}
I have a listBox1 with 4 items inside. I can use the keys to move up-down between the items or click with the mouse once on each item in both cases the selected items will be highlight with blue marked .
I want when I click on an item or when I move the keys up and down over the items it will change the label.Text with the current item name.
For example in on the item moses so label1.Text will contain moses.
Moved to the next item with arrow key up so now label1.Text contain daniel.
Clicked with the mouse on the item number 3 now label1.Text will contain dana.
Tried with this:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
//listBox1.Items.Add(fsi[i].Name + Environment.NewLine);
label2.Text = listBox1.Items[i].ToString();
}
}
But its not worrking.
Works for me.
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("Item1");
listBox1.Items.Add("Item2");
listBox1.Items.Add("Item3");
listBox1.Items.Add("Item4");
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label1.Text = listBox1.SelectedItem.ToString();
}
You really expected your code to work? Why iterate over the whole collection, if you just need to check for the currently selected item?
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
label2.Text = lbi.Content.ToString();
}
or if you're using webforms:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
label2.Text = listBox1.SelectedItem.Text;
}
If you are using List<CustomClass>/ObservableCollection<CustomClass> as ItemSource for ListBox try the following way in listbaox selected index changed event
var listTapped = sender as ListBox;
var selectedUser = listTapped.SelectedItem as CustomClass;
if (selectedUser == null)
return;
label2.Text = selectedUser.Name; //
ListBox contains event SelectedIndexChanged. It raises on such conditions. I think you should use it. Then you should use SelectedValue property to get correct string.