On my windows forms I have a listview control and I have an event handler of
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
the problme is after the first selection I am getting an error that says:
System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '0' is not vaild for 'index'. Parameter name:index'
This is the full code:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(listView1.SelectedItems[0].Text);
}
This code allows for one selection to show up in a message box but the next selection comes up with the above error. Any ideas of how to fix this?
Your datasource or list's length is important in this case, without knowing that, you can check length inside event. The error says that; SelecteItems doesn't have any item inside. When you try to select 0 index of the list, it throws that the list doesn't have any item at index 0.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count > 0){
MessageBox.Show(listView1.SelectedItems[0].Text);
}
}
Hope helps,
Check selected items count before to retrieve the selected item.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count > 0)
MessageBox.Show(listView1.SelectedItems[0].Text);
}
Yes that is the answer thank you!
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count > 0)
MessageBox.Show(listView1.SelectedItems[0].Text);
}
Related
I need to implement a ComboBox, which acts as follows:
When Click on the ComboBox, the client calling API method and updates the combobox items with the response.
My problem is, when I have 0 results - I want the ComboBox not to open (It has 0 items).
Is there a way to do that?
This is my current code:L
private void Combo_DropDown(object sender, EventArgs e)
{
// Private method which addes items to the combo, and returns false if no itmes were added
if (!AddItemsToComboBox())
{
// This is not working
Combo.DroppedDown = false;
}
}
You can make the DropDownHeight as small as possible (1). For example:
int iniHeight;
private void Form1_Load(object sender, EventArgs e)
{
iniHeight = Combo.DropDownHeight;
}
private void Combo_DropDown(object sender, EventArgs e)
{
Combo.DropDownHeight = (AddItemsToComboBox() ? iniHeight : 1);
}
I would like remove item just before because its shows me twice the same item when I click on, but here, item number -1 doesn't exists
And I don't know why.
How I can resolve this ?
Thank you.
private void DEXTarget_CheckedChanged(object sender, EventArgs e)
{
Logs("DEX(TMAPI) Target Checked");
listView1.Items.RemoveAt(-1);
PS3.ChangeAPI(SelectAPI.TargetManager);
Var.API = true;
}
private void CEXTarget_CheckedChanged(object sender, EventArgs e)
{
Logs("CEX(CCAPI) Target Checked");
PS3.ChangeAPI(SelectAPI.ControlConsole);
Var.API = false;
}
Log:
private void Logs(string text)
{
Var.lst = this.listView1.Items.Add(DateTime.Now.ToString("dd/MM/yy HH:mm"));
Var.lst.SubItems.Add(text);
}
ListView Items Index start from 0 and ends with Count-1.
I think you are looking for removing the last Item from the ListView
Try This:
listView1.Items.RemoveAt(listView1.Items.Count - 1);
When I save a new item on datagridview the statement
MessageBox.Show(this.tb_aprovacao_admissaoDataGridView.CurrentRow.Cells[0].Value.ToString());
shows the value -1. How can I change it to show the real number of ID?
Thanks to everyone.
Depends how you want to get the value..
Do you want to get the value after you click on the cell, or on the click of a button or?
If you want to do it on the cell click event, you can do it like this:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
To get it with a button:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}
I am trying to do something like: when a user select an item on a listbox, then the function listboxClicked will be triggered. However, the first click often does not able to trigger the function. It only triggers the function when I click the same item or another item for the second time.
May I know what's wrong with my code? Thank you.
My Code:
private void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
listbox.MouseClick += listboxClicked;
}
private void listboxClicked(object sender, EventArgs e)
{
if (listbox.SelectedIndex != -1)
{
//do something
}
}
Try this one:
Listbox1_SelectedValueChanged(object sender, EventArgs e)
{
Listbox listbox = (Listbox)sender;
MessageBox.Show(listbox.SelectedItem.ToString());
}
I want to add new items to my generic list when user clicks on a button, but each the the list contains only the last introduced item, it seems that during each button click list get reinitialized :(.
This is a part of code:
List<ProdusBon> listaProduseBon = new List<ProdusBon>();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
listaProduseBon.Add(new ProdusBon(-1, Int32.Parse(TextBox2.Text), -1, Int32.Parse (ListBox1.SelectedValue)));
}
I also tried using this code:
List<ProdusBon> listaProduseBon = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
listaProduseBon = new List<ProdusBon>();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
listaProduseBon.Add(new ProdusBon(-1, Int32.Parse(TextBox2.Text), -1, Int32.Parse (ListBox1.SelectedValue)));
}
but in this case a null reference exception was raised.
I must keep all the items in the list and not only the last one, and when click event was raised a new item to be added to the list.
All the controls in Default.aspx got the default values only the ListBox has "Enable AutoPostBack" set to true but i believe that this is not causing this behavior.
I do not how to keep the items in the list in these conditions, please give me a hand if you know how to do this.
Thanks !
Member variables are lost between page loads. You could store the variable in Session if you want it to remain the same value between loads.
List<ProdusBon> listaProduseBon
{
get { return (List<ProdusBon>) Session["ProdusBon"]; }
set { Session["ProdusBon"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (listaProduseBon == null) listaProduseBon = new List<ProdusBon>();
}
protected void Button1_Click(object sender, EventArgs e)
{
listaProduseBon.Add(new ProdusBon(-1, Int32.Parse(TextBox2.Text), -1, Int32.Parse (ListBox1.SelectedValue)));
}
On your button click event first bind the list button and then add the new item from the textbox.
protected void Button1_Click(object sender, EventArgs e)
{
//code to bind your list goes here
listaProduseBon.Add(new ProdusBon(-1, Int32.Parse(TextBox2.Text), -1, Int32.Parse (ListBox1.SelectedValue)));
}