I am populating a list-view with passwords.
Then I want to take the selected items text and pass it to a text box when it is clicked.
So far I have:
private void passwordListView_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem listViewItem = new ListViewItem();
listViewItem = passwordListView.SelectedItems[0];
passwordTextBox.Text = listViewItem.Text;
}
It works the first time I press it and it populates the textbox but then if I click a different password in the list-view it throws an exception.
Have I left out something blatantly obvious?
When the Selected Index is changed in a winforms ListView the first event is for the item that has now been deselected. So at that point SelectedItems is empty.
Check for this through if (passwordListView.SelectedItems.Count == 0) return;
After this you will get a second event, which will be for the new selection, and you can act on this.
Btw, you don't need to make a new ListViewItem as you are in your snippet, this will save the extra unnecessary creation:
ListViewItem listViewItem = passwordListView.SelectedItems[0];
Maybe you could try this :
private void passwordListView_SelectedIndexChanged(object sender, EventArgs e)
{
if(passwordListView.SelectedItems.Count > 0)
passwordTextBox.Text = passwordListView.SelectedItems.First().Text;
}
Related
I have a data grid view which is used to bind values.
I have a ComboBox inside this DatagridView; I want to implement an auto complete property in this ComboBox. It should not only search for the first letter but the whole item...
This can be done by
Grabbing the ComboBox
Manipulating its Items
Let's assume you have only one ComboBoxColumn; then you can grab an instance of the current one like this:
ComboBox editCombo = null; // class level variable
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
editCombo = e.Control as ComboBox;
if (editCombo != null)
{
// here we can set its style..
editCombo.DropDownStyle = ComboBoxStyle.DropDown;
editCombo.AutoCompleteMode = AutoCompleteMode.Suggest;
// sigh..:
editCombo.TextChanged -= editCombo_TextChanged;
editCombo.TextChanged += editCombo_TextChanged;
}
}
Let's assume you have the valid list of values in a List<string>
List<string>() allChoices = new List<string>();
Then we can adapt the Items to be shown in the TextChanged event:
void editCombo_TextChanged(object sender, EventArgs e)
{
List<String> items = allChoices.Select(x=>x)
.Where(x=>x.Contains(editCombo.Text)).ToList();
if (items.Count > 0)
{
editCombo.Items.Clear();
editCombo.Items.AddRange(items.ToArray());
}
editCombo.Select(editCombo.Text.Length, 0); //clear the selection
}
Note that this x=>x.Contains(editCombo.Text) searches for items that contain the full text entered. I hope that is what you mean; searching for items that are identical to the entered text makes no sense, as then you do not need to AutoComplete them anyway..
I have a dialog form where the user has to selected which colums from a textfile he wants to use for drawing a graph.
If someone doesn't quite understand what I mean, please look at the following example:
The dialog opens
The user selects e.g. that the x-values of his graph shall be from the second column of the textfile
The user selects e.g. that the y-values of his graph shall be from the third column of the textfile
The user clicks "OK"
The problem I have is the following:
I want to prevent the user from selecting the same column for x and y values, which would result in a line in an angle of probably 45 degrees and make the graph useless.
Both comboboxes are filled with the same array of strings, which contains the headlines of the columns in the textfile. Getting those strings into the comboboxes works great, but:
I tried removing the item selected in one combobox from the other combobox and otherwise.
Before that, the currently selected item is stored in a variable and the items are reset to the default state, which means all headlines from the textfile.
But, as I programmatically set the index to where it was before, so that the user doesn't have to, the SelectedIndexChanged event fires and traps my code in an infinite loop.
public void setComboboxText()
{
cbX.Items.Clear();
cbY.Items.Clear();
cbX.Items.AddRange(cbText);
cbY.Items.AddRange(cbText);
}
void CbXSelectedIndexChanged(object sender, EventArgs e)
{
var item = cbX.SelectedItem;
setComboboxText();
cbX.SelectedItem = item;
cbY.Items.Remove(cbX.SelectedItem);
}
void CbYSelectedIndexChanged(object sender, EventArgs e)
{
var item = cbY.SelectedItem;
setComboboxText();
cbY.SelectedItem = item;
cbX.Items.Remove(cbY.SelectedItem);
}
The code does the following:
The currently selected item is temporarily stored
The items of the combobox are reset
The currently selected item is set to be the item stores before
The item selected in the changed box disappears from the other combobox
Any help appreciated, especially if someone could tell me if I can do what I want with another event or even without events.
Thanks in advance
I think this is what you are trying to achieve.
public partial class Form1 : Form
{
List<string> source1 = new List<string>();
List<string> source2 = new List<string>();
public Form1()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
source1.Add("item" + i);
source2.Add("item" + i);
}
comboBox1.Items.AddRange(source1.ToArray());
comboBox2.Items.AddRange(source2.ToArray());
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.Items.Contains(comboBox1.SelectedItem))
{
comboBox2.Items.Clear();
List<string> updatedList = new List<string>();
updatedList = (from x in source2
where !x.Equals(comboBox1.SelectedItem)
select x).ToList<string>();
comboBox2.Items.AddRange(updatedList.ToArray());
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Items.Contains(comboBox2.SelectedItem))
{
comboBox1.Items.Clear();
List<string> updatedList = new List<string>();
updatedList = (from x in source1
where !x.Equals(comboBox2.SelectedItem)
select x).ToList<string>();
comboBox1.Items.AddRange(updatedList.ToArray());
}
}
}
Make the source collections available avaiable to each combobox SelectedIndexChanged handlers
On each selection change update the source of the other combobox only if the newly selected item exists in the other combobox Items.
I have a problem. I have three buttons of main categories and when you click one of these buttons some things appear in ListBox and the buttons with subcategories appear. If you click on subcategory, consequently different things appear in ListBox.
I have methods like that:
private void DisplayPeople(string category); //I use it ParentClicked and SubClicked
private void ParentClicked(object sender, EventArgs e); //for parent category
private void SubCatClicked(object sender, EventArgs e); //for subcategory
myListBox.SelectedIndexChanged+= new EventHandler(selectedIndexChange);
When you select sth in listbox then it should appear in my DataGridView and it works perfectly. However, when I click on the button and things appear in listbox, and I put sth like myListBox.ClearSelected(); or my.SelectedItem = null; I see NOTHING is selected at the begining but still SelectedIndexChange event works because it adds first row to my DataGridView. I have no idea why, could u help me?
MUCH MORE SHORTER:
In my program when you select sth in ListBox, it appears in DataGridView. When i set myListBox.ClearSelected(); or my.SelectedItem = null;, nothing is selected in the begining but SelectedIndexChange event works and first thing in listbox is added to DataGridView. I don't want that, I want it to appear in datagridview only when it is selected by the user.
NOW MY EVENT HANDLER LOOKS LIKE THAT:
private void selectedIndexChange(object sender, EventArgs e)
{
Person person = (Person)MyListBox.SelectedItem;
if (MyListBox.Items.Count > 0 && MyListBox.SelectedItems.Count > 0)
{
Basket.Add(person);
dataGridView1.DataSource = Basket;
}
}
PS.
I SOLVED THE PROBLEM. I did it that the thing from listbox is added to DataGridView when user clicks on ListBox and SelectedIndexChange event appears. But maybe there is simpler and prettier solution?
That's exactly how SelectedIndexChanged is supposed to work.
What you have to do is compare myListBox.SelectedIndex to -1 or myListBox.SelectedItem to null to see if something is actually selected in the ListBox.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged(v=vs.110).aspx
try like this
private void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (myListBox.Items.Count > 0 && myListBox.SelectedItems.Count > 0)
{
//Do something in DatagridView
}
else
{
//clear the gridview
}
}
I would like to update the data contained in a ListviewItem contained itself in a ListView.
The idea is when a row of the listview is selected, I click on a button and the data is updated .
I have this code :
ListView listView1 = new System.Windows.Forms.ListView();
ListViewItem lv1 = new ListViewItem("me");
lv1.SubItems.Add("my brother");
listView1.Items.Add(lv1);
Button myB = new System.Windows.Forms.Button();
private void myB_Click(object sender, EventArgs e)
{
listView1.SelectedItems[0] ....... ;
}
I know how to go any further to acces to modify the value of "my brother" to "my sister".
Thanks in advance.
Check if something is selected then access the first ListViewItem in the SelectedItems
if(listView1.SelectedItems != null)
{
ListViewItem item = listView1.SelectedItems[0];
item.SubItems[0].Text = "Sister";
}
this is the reference on MSDN for ListViewSubItem class
I want to use a ComboBox with the DropDownList style (the one that makes it look like a button so you can't enter a value) to insert a value into a text box. I want the combobox to have a text label called 'Wildcards' and as I select a wildcard from the list the selected value is inserted in to a text box and the combobox text remains 'Wildcard'. My first problem is I can't seem to set a text value when the combobox is in DropDownList style. Using the properties pallet doesn't work the text value is simply cleared when you click off, adding comboBox.Text = "Wildcards"; to form_load doesn't work either. Can anyone help?
The code you specify:
comboBox.Text = "Wildcards";
...should work. The only reason it would not is that the text you specify is not an item within the comboBox's item list. When using the DropDownList style, you can only set Text to values that actually appear in the list.
If it is the case that you are trying to set the text to Wildcards and that item does not appear in the list, and an alternative solution is not acceptable, you may have to be a bit dirty with the code and add an item temporarily that is removed when the drop-down list is expanded.
For example, if you have a form containing a combobox named "comboBox1" with some items and a button named "button1" you could do something like this:
private void button1_Click(object sender, EventArgs e)
{
if (!comboBox1.Items.Contains("Wildcards"))
{
comboBox1.Items.Add("Wildcards");
}
comboBox1.Text = "Wildcards";
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
if (comboBox1.Items.Contains("Wildcards"))
comboBox1.Items.Remove("Wildcards");
}
That's pretty quick and dirty but by capturing the DropDownClosed event too you could clean it up a bit, adding the "Wildcards" item back as needed.
You can select one of items on formload or in form constructor:
public MyForm()
{
InitializeComponent();
comboBox.SelectedIndex = 0;
}
or
private void MyForm_Load(object sender, EventArgs e)
{
comboBox.SelectedIndex = 0;
}
Try this
comboBox1.SelectedValue = "Wildcards";
This may be a possible solution:
comboBox1.SelectedValue = comboBox1.Items.FindByText("Wildcards").Value;