How to pass a text while firing an event? - c#

I need to pass a checkeditem of a checklistbox while firing an event from a dynamic checklistbox. The code snippet is provided below with comments...
I'm facing an issue with the same piece of code. On mouse double click event its throwing an exception saying IndexoutofRange. Its working fine with the index value 0.Please help 2 solve me both.
private void clbTables_MouseDoubleClick(object sender, MouseEventArgs e)
{
int indexofselectedtable;
indexofselectedtable = Convert.ToInt32(clbTables.SelectedIndex);
if (clbTables.CheckedItems.Count != 0)
{
Metadata metadataobj = new Metadata(dbProperties);
DBList = metadataobj.GetColumns(clbTables.CheckedItems[indexofselectedtable].ToString()); // This throws an error on checking an item of index>0.
for (int j = 0; j < DBList.Count; j++)
{
chklistcolumns.Name = "chklist" + j++;
chklistcolumns.Items.Add(DBList.ElementAt(j));
}
this.Controls.Add(chklistcolumns);
chklistcolumns.ItemCheck += new ItemCheckEventHandler(OnCheckListBoxItemCheck);
}
}
private void OnCheckListBoxItemCheck(object sender, ItemCheckEventArgs args) //need to pass the tablename which can be got from the object clbTables
{
Columns columnobj = new Columns();
columnobj.ColumnName = this.Text;
columnobj.Id = this.Name;
columnobj.TableName= // need to get the tablename from the object clbtables
}

I think I see what the issue here is, you are trying to match the selected index of your CheckedListBox with an index in the CheckedItems collection, but it doesn't work that way.
Consider this: you have 10 items in your CheckedListBox, and three of them are checked. That gives you .Items[10] and .CheckedItems[3]. If then you double click on the 7th item in the CheckedListBox, your SelectedIndex will be 6, but there will only be three items in the CheckedItems collection. So when you try to read clbTables.CheckedItems[6] you are going to be outside of the range of that collection.

clbTables.CheckedItems is another collection. You can't use clbTables.SelectedIndex in it.
Why not just to use SelectedValue property?

Related

Datagridview not updating correctly

I'm new-ish to C# and I'm trying to input a list of objects into a datagridview. I constantly add items to this list each time I click a button and the datagridview should refresh by setting it's data source back to the list.
Here is the list:
List<Models.OrderItem> orderitemlist = new List<Models.OrderItem>();
And here is the code that adds to the list and refreshes the list:
private void btnAddToOrder_Click(object sender, EventArgs e)
{
int quantity = Convert.ToInt32(tbAddOrderQuantity.Text);
int stock = Convert.ToInt32(ItemDataGrid.CurrentRow.Cells[6].Value);
int newstock = stock - quantity;
if (newstock < 0)
MessageBox.Show("You do not have enough items in stock for this.");
else
{
ItemDataGrid.CurrentRow.Cells[6].Value = newstock;
int itemID = Convert.ToInt32(ItemDataGrid.CurrentRow.Cells[0].Value);
string itemname = Convert.ToString(ItemDataGrid.CurrentRow.Cells[1].Value);
int sellprice = Convert.ToInt32(ItemDataGrid.CurrentRow.Cells[5].Value);
Models.OrderItem item = new Models.OrderItem(itemID, itemname, sellprice, quantity);
orderitemlist.Add(item);
RefreshItemsOnOrderData();
RefreshPrice();
}
}
private void RefreshItemsOnOrderData()
{
ItemOnOrderDataGrid.DataSource = orderitemlist;
}
The list will update with the first item however when I try to add another item it seems to run the block of code however doesn't actually add it to the datagrid view. Is anyone able to help? Have I made a simple error I just can't see?
As mentioned,
Set the source to null, re-ref the list, then reset bindings
ItemOnOrderDataGrid.DataSource = null;
ItemOnOrderDataGrid.DataSource = orderitemlist;
ItemOnOrderDataGrid.ResetBindings();
You may want to try omitting the null. I can't recall if this works without the null.

C# 2 Comboboxes with identical, not twice-selectable content

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.

Setting multiple items as selected by default list picker- multiple selection mode -windows phone

I need to set some items as selected by default in list view,I am doing this is listpicker loaded event.This works fine,Next when user changes those selections, I am unable to retrieve the results.SelectionChanged event gets fired before listpicker loaded event, If I add event handler in loaded method, removing it from XAML, I m getting the exception
An unhandled exception of type 'System.InvalidOperationException'
occurred in System.Windows.ni.dll
Here is my code..
private void interestms_Loaded(object sender, RoutedEventArgs e)
{
//selectedinterests is a string containing keys of selected interests seperated by commas.
object[] split1 = selectedinterests.Split(',');
//interest is a dictionary with total list of interests
var s = PhoneApplicationService.Current.State["interest"];
List<object> finallist = new List<object>();
var ss = (((System.Collections.Generic.Dictionary<string, string>)(s))).Keys;
List<object> arr = new List<object>((((System.Collections.Generic.Dictionary<string, string>)(s))).Values);
for (int k = 0; k < split1.Length; k++)
{
object getsel = arr[k];
finallist.Add(getsel);
}
interestms.SelectedItems = hello;
}
On selectionChange event , I am getting the items that have been clicked,not those items that have been checked, so When I uncheck a checked item, that item also gets added in selectedItems. In this case I need to create two object arrays,one containing the total set of values and other the selected Items and remove the common items in both.In doing so
selectionChanged method gets called before loaded event.
Please help.In case any other detail is required,I would be glad to provide..
EDIT:
private void interestms_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//edited interst is an array object
editedinterests.Add(e.AddedItems);
var s = PhoneApplicationService.Current.State["interest"];
List<object> arr = new List<object>((((System.Collections.Generic.Dictionary<string, string>)(s))).Values);
var listcommon = arr.Intersect(editedinterests);
}
Try this
private void interestms_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(interestms.SelectedIndex==-1) return;
//Here may be you get all selected items no need to maintain two array if you get all selected items.
var listcommon = (cast as your type)interestms.SelectedItems;
interestms.SelectedIndex=-1;
}

Adding items to List<> of object which already contain data, add duplicate item in the List

I am developing a Windows phone app for which I want to get a list of first 15 object from my web service plus an extra item to represent the "Load next 15 items button" and bind it to a listbox. On selection of the last element or index(buttton), I want to remove the last item(button) from the list and call the service again to get the next 15 items plus the button item which will again add up to the current list without clearing the list. And the same process goes on as we select the last item from the listbox.
My problem is I am able to get it correctly for the first round. In the second round, the next 15 element is added to the list twice. I don't know what is the real problem. I am not getting any error or exception also.
Below is what I have done so far :
private void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int LastIndex=app.imgItem.Count - 1;
if (ListBox1.SelectedIndex == LastIndex)
{
app.imgItem.Remove(app.imgItem[app.imgItem.Count-1]);
proxy.SelectNextItemsCompleted += new EventHandler<ServiceReference1.SelectNextItemsCompletedEventArgs>(proxy_SelectNextItemsCompleted);
int id = app.imgItem.Last().Id;
proxy.SelectNextItemsAsync(categoryID, id);
}
}
void proxy_SelectNextItemsCompleted(object sender, ServiceReference1.SelectNextItemsCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("Cannot load data.");
return;
}
else
{
foreach (var item in e.Result)
{
ImgData idata = new ImgData();
idata.CategoryID = item.CategoryID;
idata.ImageID = item.ImageID;
idata.ImageTitle = item.ImageTitle;
idata.Thumbnail = item.Thumbnail;
app.imgItem.Add(idata);
}
}
ImageData btnData = new ImageData();
btnData.CategoryID = 0;
btnData.ImageID = 0;
btnData.ImageTitle = "";
btnData.Thumbnail = "Images/loadButton.jpg";
app.imgItem.Add(btnData);
ListBox1.ItemsSource = app.imgItem;
}
It looks like you're re-registering an event handler in the SelectionChanged event handler, and as a result, the SelectNextItemsCompleted handler is probably getting called twice the second time around:
proxy.SelectNextItemsCompleted += new EventHandler<ServiceReference1.SelectNextItemsCompletedEventArgs>(proxy_SelectNextItemsCompleted);
You should probably be registering that event handler somewhere else where the code only runs once.

Select index from listview

I'm having some problem to get the index of the selected row in a listview. I wonder why this code isn't working? I get a red line below the SelectedIndex
private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e)
{
int index = lvRegAnimals.SelectedIndex;
string specialData = motelManager.GetInfoFromList(index);
UppdateSpecialData(specialData);
}
Help is preciated. Thanks!
EDIT:
For some strange reason I get two messages when I click on one of the lines in the listView!? First I get the previous number and then the number for the last clicked line. What could be wrong?
private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e)
{
int index = lvRegAnimals.FocusedItem.Index;
MessageBox.Show(Convert.ToString(index));
}
It's working now when I added a check like this:
if(lvRegAnimals.SelectedIndices.Count > 0)
Because ListView doesn't contain any SelectedIndex, instead there is a property of SelectedIndices.
var indices = lvRegAnimals.SelectedIndices;
//indices[0] you can use that to access the first selected index
ListView.SelectedIndices
When the MultiSelect property is set to true, this property returns a
collection containing the indexes of all items that are selected in
the ListView. For a single-selection ListView, this property returns a
collection containing a single element containing the index of the
only selected item in the ListView.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
// Acquire SelectedItems reference.
var selectedItems = listView1.SelectedItems;
if (selectedItems.Count > 0)
{
// Display text of first item selected.
this.Text = selectedItems[0].Text;
}
else
{
// Display default string.
this.Text = "Empty";
}
}
Try :
listView1.FocusedItem.Index
This give you the index of the selected row.
There is another thread like this one, but here it goes again.
It can return NULL. Also the SelectedIndexChanged event can be FIRED TWICE. And the first time, there nothing selected yet.
So the only safe way to find it is like this:
private void lv1_SelectedIndexChanged(object sender, EventArgs e)
{
if (lv1.FocusedItem == null) return;
int p = lv1.FocusedItem.Index;
... now int p has the correct value...
The ListView is a darn hassle to work with sometimes.
A simple solution i've used is a for loop that checks for the
selected Item.
I've put my solution in the "When index change trigger" within the ListView.
Example:
int sel_item = 0; //an int to store the selected item index.
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < listView1.Items.Count; i++)
{
if (listView1.Items[i].Selected == true)
{
sel_item = i;
}
}
}
This would ofcourse only work correctly with the "Multiselection" option set as false.

Categories