I'm working on an inventory system within C# as a small personal project.
I've managed to get the basic functions working within the Form, but I'm having trouble working out how to add a button that will either Increase or decrease the products quantity by 1. The inventory is stored in a ListViewItem object.
Or by having the user enter a value into a textbox, with the product selected and it'll change the quantity of that product without affecting the rest.
The code below shows the code I have written for the user to enter a value manually into a textBox, and clicking the button (with the product selected) will update the quantity. There's no crash, it simply doesn't do anything.
private void PlusOne_Click(object sender, EventArgs e)
{
foreach (ListViewItem list in listView1.SelectedItems)
{
list.SubItems.Add(addBox.Text);
}
}
private void addBox_TextChanged(object sender, EventArgs e)
{
int addOne = int.Parse(addBox.Text);
}
This will increment all the selected items in your list view.
I'm assuming that you have one ListViewItem per product and the first SubItem stores the quantity value. Subitems are shown from the second column onwards. First column corresponds with the item itself
private void PlusOne_Click(object sender, EventArgs e)
{
const int quantityColumn = 0;
int increment = int.Parse(PlusOne.Text);
foreach (ListViewItem list in listView1.SelectedItems)
{
int qty = 0;
if (list.SubItems.Count != 0) {
qty = int.Parse(list.SubItems[quantityColumn].Text);
}
list.SubItems[quantityColumn].Text = (qty + increment).ToString();
}
}
Related
I'm making an application that consists of comboBoxes. If the user selected "Chauffeur" in the comboBox the total price goes up by 10% once my btnAddDriver is clicked. However when I select "Chauffeur" the total price does not increase by 10% when I click Add Driver in fact when using brake points it doesn't seem to realise I have selected "Chauffeur" and skips the calculation within the if statement.
My Code is as fallows
int policy = 500;
double Chauffeur = 0.10;
private void cmbOccupation_Loaded(object sender, RoutedEventArgs e)
{
// ... A List.
List<string> occupation = new List<string>();
occupation.Add("Chauffeur ");
occupation.Add("Accountant");
// ... Get the ComboBox reference.
var comboBox = sender as ComboBox;
// ... Assign the ItemsSource to the List.
comboBox.ItemsSource = occupation;
// ... Make the first item selected.
comboBox.SelectedIndex = 0;
}
private void btnAddDriver_Click(object sender, RoutedEventArgs e)
{
txtPolicy.Text = policy.ToString();
if (cmbOccupation.SelectedItem.ToString() == "Chauffeur")
{
txtPolicy.Text = (policy * Chauffeur).ToString();
}
}
"Chauffeur" and "Chauffeur " are two different strings in C#.
That'll be $150, please pay the girl at the desk on your way out.
Change occupation.Add("Chauffeur ");
To occupation.Add("Chauffeur");
I'm currently working on a shopping basket application in C# at the moment.
Is there a way that when I press the add button it can update the item quantity each time that an item is added to the basket?
Thanks. This is my current code:
protected void btnAdd_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox3.Text);
NumericUpDown1.Maximum = 100;
NumericUpDown1.Minimum = 0;
textBox5.Text = listBox1.Items.Count.ToString();
if (NumericUpDown1.Value == 0)
{
NumericUpDown1.Value = +1;
}
}
As I've added two items to the basket the quantity should change to 2 but it doesn't it just stays at 1.
Personally, I would keep a list of objects that store product name/description and value (count) using the product name as the key. Then I'd use databinding to bind the list to the listbox and let databinding do the work of displaying any updates.
The idea being that you can find the object you're interested in by searching your list, then update it's "quantity" property, leaving databinding to update the UI.
Alternatively, you could use the new class you create to add directly to the listbox, but set the ValueMember and DisplayMember properties accordingly.
public class OrderLineViewModel
{
public string Description { get; set; }
public uint Quantity { get; set; }
}
See this answer to this question for more information.
The corrected code, based on the comments:
protected void btnAdd_Click(object sender, EventArgs e) {
listBox1.Items.Add(textBox3.Text);
NumericUpDown1.Maximum = 100;
NumericUpDown1.Minimum = 0;
int previousNoItems;
Int.TryParse(textBox5.Text, out previousNoItems);
textBox5.Text += previousNoItems + NumericUpDown1.Value;
}
I used the following link but it did not work
Combo boxes duplicate entries
The data is:
Printer, Epson, T200
Mainframe, IBM, z15
Mainframe, Del, X99
This information comes from a database but it is all there and tested so I left the irrelevant code out.
So I have two problems. The first one is that the EquipmentType Combo box
repeats values. I have 2 mainframes, so it shows mainframe twice.
The second problem is that if i go through the EquipmentType combo box,
and return to a previous value, the EquipmentModel does not update until i open
the EquipmentName combobox physically click on an entry
private void Client_Maintenance_Load_1(object sender, EventArgs e)
{
cmbicEquipmentType.DataSource = equip;
cmbicEquipmentType.DisplayMember = "EquipmentType";
cmbicEquipmentName.DataSource = equipmentName;
cmbicEquipmentModel.DataSource = equipmentModel;
}
private void cmbicEquipmentType_SelectedIndexChanged(object sender, EventArgs e)
{
nameSource.Clear();
foreach (Equipment item in equip)
{
if (item.EquipmentType == cmbicEquipmentType.Text)
{
nameSource.Add(item.EquipmentName);
cmbicEquipmentName.SelectedIndex = 0;
}
}
nameSource.DataSource = null;
nameSource.DataSource = equipmentName;
cmbicEquipmentName.DataSource = nameSource;
cmbicEquipmentName.Update();
cmbicEquipmentName.Refresh();
}
private void cmbicEquipmentName_SelectedIndexChanged(object sender, EventArgs e)
{
nameSource2.Clear();
foreach (Equipment item in equip)
{
if (item.EquipmentName == cmbicEquipmentName.Text)
{
nameSource2.Add(item.EquipmentModel);
cmbicEquipmentModel.SelectedIndex = 0;
}
}
nameSource2.DataSource = null;
nameSource2.DataSource = equipmentModel;
cmbicEquipmentModel.DataSource = nameSource2;
cmbicEquipmentModel.Update();
cmbicEquipmentModel.Refresh();
}
This is the combo box that shows the duplicates
Equipment model does not update
Thank you in advance
if (!cmbicEquipmentType.Items.Contains(item.EquipmentType.ToString()))
{
cmbicEquipmentType.Items.Add(item.EquipmentType.ToString());
}
This fixed the duplication problem. I adapted my program to make the 2nd problem look like its supposed to happen.
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'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.