Edit listview item based on click_event c# - c#

i have created a listview
ListView ListView1 = new ListView();
ListView1.Location = new System.Drawing.Point(12, 12);
ListView1.Name = "ListView1";
ListView1.Size = new System.Drawing.Size(280, 300);
ListView1.BackColor = System.Drawing.Color.White;
ListView1.ForeColor = System.Drawing.Color.Black;
ListView1.View = View.Details;
ListView1.GridLines = true;
ListView1.FullRowSelect = true;
ListView1.Columns.Add("ProductName", 100);
ListView1.Columns.Add("Quantity", 100);
ListView1.Columns.Add("Price", 100);
and i add items to it using the following code :
b.Click += (s, e) => {
string[] arr = new string[4];
ListViewItem itm;
arr[0] = b.Text;
arr[1] = x.ToString();
arr[2] = price;
itm = new ListViewItem(arr);
ListView1.Items.Add(itm);
x++;
};
b is an auto generated button, what i want to achieve is simple the variable x will increment with value 1 everytime i click on the button b, and x indicate the Quantity.
What i want:
when ever i click on the button b, the Quantity will change for the current item that has the Column["Productname"]=b.Text
What im getting:
the Quantity change but the item gets reinserted so i want to check if the item does exist first(based on Column["Productname"]) and if it does w the Quantity gets incremented by 1.
image_to_help_understand
More details: im sorry if this is getting too long, but im simply having a number of auto generated buttons and every button represents a product, when the user click on a product it gets added to the list ( to buy it later) and if the client clicks the same product n times, the Quantity should became Quantity=n without the item being added another time. thanks all and sorry for the long post.

Poking values into the UI is not a good design. I have these changes from my previous comments. Use a BindingList, public class Product : INotifyPropertyChanged, and replace the ListView with a DataGridView. This just makes life a whole lot easier and the code so simple. Creating ListBoxItems on your own is NOT a good way of doing things.
BTW: to find a product you just use a LINQ query on the ProductCollection. No searching the UI.

Related

C# Why does the column index change at random on my DataGridView within tab control

I have a really strange problem. I have a tab control on a Windows Form with 3 tabs. Each tab has an identical DataGridView. I have a button in column 5 on each of the DataGridViews. So identical tabs, identical GridViews, vertually identical code to populate each, which looks like that below.
However, the column index works exactly as expected for the first tab i.e zero indexed. But for the second and third tab, it appears to see the first column as index , err, -1 i guess as it doesn't see it. But here's the really strange, and annoying part....it seems to change at random. Sometimes when i run the program it works as expected and takes the first column as index 0, and sometimes it displays the strange behaviour as described by completely ignoring the first column and counts the second column as Index 0. This is a huge problem as i take the values from the cells in the columns to populate another Form - which the button in column 5 opens.
private void populateToolsDataGrid()
{
doc = XDocument.Load(XMLfilePath + "\\DataFile\\ILS_Support_TOOLS.xml"); // this works but does not allow sorting
var xmlData = from supeq in doc.Elements("data").Elements("supequi").Elements("tool")
select new
{
Name = supeq.Element("toolName").Value,
NSN = supeq.Element("toolNSN").Value,
PN = supeq.Element("toolPN").Value,
Cage = supeq.Element("toolCage").Value,
ID = supeq.Attribute("id").Value
};
GridViewTools.DataSource = xmlData.ToList();
GridViewTools.Columns["ID"].Visible = false;
if (GridViewTools.Columns.Contains("detailsButton") == false)
{
DataGridViewButtonColumn button = new DataGridViewButtonColumn();
{
button.Name = "detailsButton";
button.HeaderText = "Details";
button.Text = "Details";
button.UseColumnTextForButtonValue = true;
GridViewTools.Columns.Add(button);
}
}
if (toolDetailsButtonInitialised == false)
{
GridViewTools.CellClick += dataGridViewTools_CellClick; // click event for the button click
toolDetailsButtonInitialised = true;
}
GridViewTools.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
}

Add more items to same Row and same column in listview

I know how to add/delete items but I don't know how to add more items to the same field (same row and same column). I want whenever I click a button, an item is added to same selected row but not to new row in the listView.
I uploaded a photo you can check to see what I exactly mean.
Consider looking at ObjectListView or DataGridView instead of what you are currently. It may be more flexible to your needs.
Your question is somewhat unclear. Clearly you are using listView and you have columns and rows resulting in a cell / box / grid location. I gather that, after its initial creation, you wish to append or alter the data at that location.
To get to the point: Multi-line text within a given 'cell' is not supported (as best I can tell). The picture you have shown is likely a custom object or something similar to a listView, but different (such as a ObjectListView). Or perhaps a picture.
listView2.Items[0].SubItems[4].Text = "123\nabc"; //Doesn't add a proper newline like a normal string
listView2.Items[0].SubItems[4].Text = "123\rabc"; //Doesn't add a proper return carriage like a normal string
listView2.Items[0].SubItems[4].Text = "123\r\nabc"; //Doesn't add a proper newline like a normal string
I am assuming you are using the details view
listView1.View = View.Details;
First adding your headers, listView1.Columns.Add(text, width);
listView1.Columns.Add(First Name", 50);
listView1.Columns.Add("Middle Name", 100);
listView1.Columns.Add("Last Name", 100);
You then add data to the listView. However, this is not done directly. You build a listViewITEM then add that item to the list view.
string[] row = new string[3];
row[0] = "john";
row[1] = "someone";
row[2] = "doe";
ListViewItem lvi = new ListViewItem(row);
listView1.Items.Add(item);
listView1.SelectedItems[#].SubItems[#].Text = "string" + "\n" + "string2";
CrazyPaste suggested adding a row, which could be practical and is something you often see with listViews.
However, If you choose to add or "redo" the rows, be sure to remove any old information before inputting new information to avoid duplicates.
Taken from the popup within visual studio 2013 pro
listView1.Items.RemoveAt(int index)
listView1.Items.Insert(int index, string key, string text, int imageIndex)
OR
listView1.Items.Clear(); //Clears all items
then
//Add populate logic here
Two arrays or a multidimensional array in a loop would be effective if you wish to populate the listview in that manner.
To achieve this programmatically, you could...
listView2 = new ListView();
listView2.View = View.Details;
listView2.Location = new Point(50, 50);
listView2.Size = new Size(400, 100);
this.Controls.Add(listView2);
listView2.Columns.Add("AAA");
listView2.Columns.Add("BBB");
listView2.Columns.Add("CCC");
listView2.Columns.Add("DDD");
listView2.Columns.Add("EEE");
ListViewItem item1 = new ListViewItem();
item1.Text = "0"; //The way to properly set the first piece of a data in a row is with .Text
item1.SubItems.Add("1"); //all other row items are then done with .SubItems
item1.SubItems.Add("2");
item1.SubItems.Add("3");
item1.SubItems.Add("");
item1.SubItems.Add("");
ListViewItem item2 = new ListViewItem();
item2.Text = "00";
item2.SubItems.Add("11");
item2.SubItems.Add("22");
item2.SubItems.Add("33");
item2.SubItems.Add("");
item2.SubItems.Add("");
ListViewItem item3 = new ListViewItem();
item3.Text = "000";
item3.SubItems.Add("111");
item3.SubItems.Add("222");
item3.SubItems.Add("333");
item3.SubItems.Add("");
item3.SubItems.Add("");
//item1.SubItems.Clear();
//item1.SubItems.RemoveAt(1);
listView2.Items.Add(item1);
listView2.Items.Add(item2);
listView2.Items.Add(item3);
//listView2.Items.Insert(2, item1); //0 here is the row. Increasing the number, changes which row you are writing data across
listView2.Items[0].SubItems[4].Text = "123\rabc";
To 'update' the information:
listView1.Items.Clear();
listView1.Items.Add(item1);
listView1.Items.Add(item2);
...etc
NOTES:
I was not able to get .Insert to work with subitems.
If you already inserted a listViewItem, You cannot insert an item
without first removing it
SubItems are not automatically created to fill empty space. Commands like 'listView2.Items[0].SubItems[4].Text' will not work with null/non-existent SubItems
I don't have much to go on. But this adds a new row:
string[] row = { "1", "snack", "2.50" };
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);
Here's a post discussing how to update an existing listitem:
C#: How do you edit items and subitems in a listview?
Ok. after I searched the internet for ages, it turned out that listView does not support text wrap. so instead I used DataGridView. thank you for your help

How to check only one radio button at a time (others disable)

I am working in silverlight c# and i have situation where i create radio buttons programatically. My code to do so is this:
Grid childGrid = CreateChildGrid();
int NumberOfRadioButton =0;
RadioButton[] RadioBut= new RadioButton[5];
int count = 0;
foreach (var item in param.Component.Attributes.Items)// this param.Component.Attributes.Items value is 4 in fact.
{
NumberOfRadioButton++;
RadioBut[count] = new RadioButton();
RadioBut[count].GroupName = item;
RadioBut[count].Content = item;
sp.Children.Add(RadioBut[count]);
count++;
}
Problem: The problem is it checks all the button where as i want only one button checked at a time . I mean if one checked the others must be disable.
Could some one please help me to achieve my target ? Thanks a lot.
Note: I am using silverlight to do so.
You should set the GroupName property of all RadioButtons to the same, so they will be "grouped", meaning only one of them can be selected at a time.
So this line:
rbs[count].GroupName = item;
Should be something like this:
rbs[count].GroupName = "MyRadioButtonGroup";
Of course the string can be anything, as long as it is the same for all RadioButtons.
You'll have to assign these radio buttons to a common group. Check Radio Button Group Name
RadioButton[] rbs = new RadioButton[5];
rbs.GroupName = "Add your common groupname here"; // added code
int count = 0;

Add ToolStripMenuItem in new row when one row is filled inside ToolStrip

I have one form and inside form i have one toolstrip control, and i am adding ToolStripMenuItem dynamically.
What i want is when one is filled up, items should list in next row.
I tried this for increasing the height and width of form but adding items in new row not happening.
ToolStripItemCollection t_col = toolStripTaskBar.Items;
int _howMany = t_col.Count;
Rectangle mi_bounds = t_col[_howMany - 1].Bounds;
if (this.Width < (mi_bounds.X + mi_bounds.Width))
{
int minimumFormHeight = 80;
this.MinimumSize = new Size((mi_bounds.X + mi_bounds.Width), minimumFormHeight);
}
Let me know if you not understand what i want.
Any suggestion how can i achieve this.
Thank You.
You can use LayoutStyle property of ToolStrip. You need to set up it to Table and modify layout settings (specify rows and columns count).
You can do it like this:
this.toolStrip1.LayoutStyle = ToolStripLayoutStyle.Table;
var layoutSettings = (this.toolStrip1.LayoutSettings as TableLayoutSettings);
layoutSettings.ColumnCount = 3;
layoutSettings.RowCount = 3;
And the you can add new items to toolstrip:
var item = new ToolStripMenuItem(string.Format("item{0}", this.toolStrip1.Items.Count + 1));
this.toolStrip1.Items.Add(item);

Listviews in C#

string title = HardwareInfo.GetComputerName().ToString();
TabPage myTabPage = new TabPage(title);
// tabControl1.TabPages.Add(myTabPage);
// Create Column Headers
ListView listView2 = new ListView();
ColumnHeader columnA = new ColumnHeader();
columnA.Text = "adsasd";
columnA.Width = 185;
columnA.TextAlign = HorizontalAlignment.Left;
ColumnHeader columnB = new ColumnHeader();
columnB.Text = "asd";
columnB.Width = 185;
columnB.TextAlign = HorizontalAlignment.Left;
ColumnHeader columnC = new ColumnHeader();
columnC.Text = "asdasd";
columnC.Width = 185;
columnC.TextAlign = HorizontalAlignment.Left;
ColumnHeader columnD = new ColumnHeader();
columnD.Text = "xx";
columnD.Width = 185;
columnD.TextAlign = HorizontalAlignment.Left;
// Add columns to the ListView:
listView2.Columns.Add(columnA);
listView2.Columns.Add(columnB);
listView2.Columns.Add(columnC);
listView2.Columns.Add(columnD);
listView2.Size = new Size(800, 300);
listView2.Location = new Point(0, 0);
listView2.GridLines = true;
listView2.View = View.Details;
Here I have a copy of some of my Code, and what I am looking to do is get a list of computers on my next work, then create tabs for each computer. I have that part done perfectly fine, but the issue I am having is that, it creates the listviews with the same NAME and that is causing an obvious problem when I try and add information to those specific list views. I was wondering, how would I go about giving each listview a name of the computer for example. As you can see for my tabs I can do that, but when it comes to the list views, if i try and do the same type of assign a string title to where it says Listview listview2 It wont let me compile. I'm new to programming and I apologize if this is obvious. Thank you.
It sounds like you want to create a List<ListView> and add your listviews to it.
Depending on how you use it, you may want a dictionary instead.
If i understand the question what you want is the name variable, in this case
listView2.name = <name of listview2>
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview_members(v=vs.71)
But i think you should look into using functions with a returntype of columns for those column constructor parts.
You want the to make the variable that stores the listview part of a dictionary, this way you can look up the different computers by their name, or whatever string you desire
Dictionary<string, ListView>
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
You could use a List if you don't need the lookup portion of the dictionary, but is fine with using integers as with an array
List<ListView>
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

Categories

Resources