Im having a problem in my listview because whenever I get value in other form it is not adding on the list but when I put breakpoint it have a value but still not adding on my listview.
here is my function in form1 getting values from datagridview
public void dataGridView1_DoubleClick(object sender, EventArgs e)
{
qtyOfOrders orders = new qtyOfOrders();
if (dataGridView1.SelectedRows.Count > 0)
{
String mealname = dataGridView1.SelectedRows[0].Cells[1].Value + String.Empty;
String price1 = dataGridView1.SelectedRows[0].Cells[2].Value + String.Empty;
pts.meal = mealname;
pts.qtyprice = Int32.Parse(price1);
orders.Show();
}
}
here is my function from form2 and will save data in listview in form1
private void OK_Click(object sender, EventArgs e)
{
cashier c = new cashier();
pricetempstorage pts = new pricetempstorage(); //class
int qty = Int32.Parse(QTYNumber.Text);
int totalPrice = qty * pts.qtyprice;
pts.qtynumber = qty;
pts.TotalPrice = totalPrice;
c.listView1.Items.Add(pts.meal);
c.qtyOrder.ListView.Items[0].SubItems.Add(pts.qtynumber.ToString());
c.price111.ListView.Items[0].SubItems.Add(pts.TotalPrice.ToString());
this.Hide();
}
this is my class
namespace jollibee4
{
class pricetempstorage
{
static int qtyNumber;
static int qtyPrice;
static int ListViewCount;
static String Meal;
static int totalprice;
public int TotalPrice
{
get
{
return totalprice;
}
set
{
totalprice = qtyNumber * qtyPrice;
}
}
public int qtynumber
{
get
{
return qtyNumber;
}
set
{
qtyNumber = value;
}
}
public int qtyprice
{
get
{
return qtyPrice;
}
set
{
qtyPrice = value;
}
}
public int listviewCount
{
get
{
return ListViewCount;
}
set
{
ListViewCount = value;
}
}
public String meal
{
get
{
return Meal;
}
set
{
Meal = value;
}
}
}
}
Try adding this code this.listView1.View = View.Details; After the c.listView1.Items.Add(pts.meal);
form1
public List<pricetempstorage> Items { get; private set; }
private void OK_Click(object sender, EventArgs e)
{
cashier c = new cashier();
pricetempstorage pts = new pricetempstorage(); //class
int qty = Int32.Parse(QTYNumber.Text);
int totalPrice = qty * pts.qtyprice;
pts.qtynumber = qty;
pts.TotalPrice = totalPrice;
Items.Add(pts);
this.Hide();
}
Create a shopping cart class where items can be append in list
I assume pricetempstorage is your class of item, its name can be product
public static ShoppingCart GetInstance()
{
if (cart == null)
{
cart = new ShoppingCart();
}
return cart;
}
protected ShoppingCart()
{
Items = new List<pricetempstorage>();
}
You have many architectural and stylistic issues with your program (use of static, capitalization, etc.)--to correct them all would require a very lengthy response.
Your code isn't working because you're creating a new instance of the cashier class, and then you're updating its listView1 object. What I think you're trying to do is update the listView object of Form2. So what you should be doing is grabbing a reference to Form2 and populating its ListView object in your OK_Click event handler...
Just a tip here: Public properties should have an initial capital letter. Your pricetempstorage class needs some work.
Related
I'm pretty new at this. Using Windows Forms in Visual Studio. I am to hammer out a store that has clothes, with stock that can be transferred in or out of the store.
I've gotten as far as to having a class, a list that contains the clothes and their quantities, and I've managed to get them into comboboxes. What I want to do now is to be able to 'buy' new quantities, changing the value in the list.
I'm stumped as to how to change the actual quantities, I'm sure I am missing stuff here.
This is my class:
public class Store
{
public string Clothing { get; set; }
public int Quantity { get; set; }
public Store(string c, int q)
{
Clothing = c;
Quantity = q;
}
And this is my current code:
}
public partial class Form1 : Form
{
List<Store> stock = new List<Store>
{
new Store ("Jeans size S", 1),
new Store ("Jeans size M", 3),
new Store ("Jeans size L", 5)
};
public Form1()
{
InitializeComponent();
}
private void bShow_Click(object sender, EventArgs e)
{
cbStockType.ValueMember = "Clothing";
cbStockType.DisplayMember = "Clothing";
cbStockType.DataSource = stock;
cbStockQnt.ValueMember = "Quantity";
cbStockQnt.DisplayMember = "Quantity";
cbStockQnt.DataSource = stock;
}
private void lblHighlightAdd_Click(object sender, EventArgs e)
{
}
private void bSlctClothing_Click(object sender, EventArgs e)
{
if (cbStockType.SelectedIndex < 0)
{ lblHighlightAdd.Text = "None"; }
else
lblHighlightAdd.Text = cbStockType.SelectedValue.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
string quantityToAdd = tbQntAdd.Text;
int add = Convert.ToInt32(quantityToAdd);
string addToStock = cbStockQnt.SelectedValue.ToString();
int newAmount = Convert.ToInt32(addToStock);
int result = newAmount + add;
foreach (var item in stock)
{
if (item.Clothing == cbStockType.SelectedValue.ToString())
{
item.Quantity = item.Quantity + result;
MessageBox.Show(cbStockQnt.SelectedValue.ToString());
}
}
}
}
}
If you can read this spaghetti junk, I'm stuck at getting the quantity of the selected piece of clothing to change. How do I get it to change the value both in the list and in the combobox?
I'm working with Windows Forms and 2 out of my 3 DGV's display into the textbox properly but the third won't. I haven't done anything different while setting them up though, thats why I'm kind of confused why it won't work. I'm working on my own practice project to review for our final exam next week, so this isn't an assignment due for any credit.
I have a form with three different DGV's, one for Student, one for Items, and one for Pokemon(our class uses Pokemon a lot). There is also a few textbox to display the selected data.
The first DGV just has students and ID numbers. These display in the correlating textbox. This part works fine.
Student Class:
public class Student
{
private string _studentName;
private int _id;
private List<Items> _items;
private List<Pokemon> _pokemon;
public string StudentName { get => _studentName; set => _studentName = value; }
public int Id { get => _id; set => _id = value; }
public List<Items> Items { get => _items; set => _items = value; }
public List<Pokemon> Pokemon { get => _pokemon; set => _pokemon = value; }
public Student()
{
this._items = new List<Items>();
this._pokemon = new List<Pokemon>();
}
}
Getting student to textbox in Main Form:
private void dgvStudent_SelectionChanged(object sender, EventArgs e)
{
if (dgvStudent.SelectedRows.Count == 1)
{
Student s = (Student)dgvStudent.SelectedRows[0].DataBoundItem;
txtName.Text = s.StudentName;
txtID.Text = s.Id.ToString();
BindItemsGrid(s);
BindPokeGrid(s);
}
else
{
BindItemsGrid(null);
BindPokeGrid(null);
txtName.Text = "";
txtCWID.Text = "";
}
This worked fine so I did something similar for the items part.
Item Class:
public class Items
{
private string _item;
private int _quantity;
public string Item { get => _item; set => _item = value; }
public int Quantity { get => _quantity; set => _quantity = value; }
public Items()
{
}
public Items (string item)
{
this._item = item;
}
public Items(string item, int quantity)
{
this._item = item;
this._quantity = quantity;
}
Items to textbox in Main Form:
private void dgvItems_SelectionChanged(object sender, EventArgs e)
{
if (dgvItems.SelectedRows.Count == 1)
{
Items i = (Items)dgvItems.SelectedRows[0].DataBoundItem;
txtItem.Text = i.Item;
txtQty.Text = i.Quantity.ToString();
}
else
{
txtItem.Text = "";
txtQty.Text = "";
}
}
Here's the problem, I did the same type of code for the third one but no matter what I try, it won't fire properly.
Pokemon class:
public class Pokemon
{
private string _pokename;
private string _nickname;
private string _type;
private int _hp;
private int _level;
public string PokeName { get => _pokename; set => _pokename = value; }
public string Nickname { get => _nickname; set => _nickname = value; }
public string Type { get => _type; set => _type = value; }
public int Hp { get => _hp; set => _hp = value; }
public int Level { get => _level; set => _level = value; }
public Pokemon()
{
}
public Pokemon(string name)
{
this._pokename = name;
}
public Pokemon(string name, string nickname, string type, int hp, int level)
{
this._pokename = name;
this._nickname = nickname;
this._type = type;
this._hp = hp;
this._level = level;
}
then in the Main Form:
private void dgvPokemon_SelectionChanged(object sender, EventArgs e)
{
if (dgvPokemon.SelectedRows.Count > 0)
{
Pokemon p = (Pokemon)dgvPokemon.SelectedRows[0].DataBoundItem;
txtPokeName.Text = p.PokeName;
txtNickname.Text = p.Nickname;
txtType.Text = p.Type;
txtHP.Text = p.Hp.ToString();
txtLevel.Text = p.Level.ToString();
}
else
{
MessageBox.Show("Error?");
txtPokeName.Text = "";
txtNickname.Text = "";
txtType.Text = "";
txtHP.Text = "";
txtLevel.Text = "";
}
}
This is a lengthy post and I apologize, I just want to give as much detail as I can. I've tried putting MessageBox.Show's inside and outside the if statement to see if it would get there and they would never actually display, I've tried removing the if statement, I've tried using CellClick instead of SelectionChanged and that will display the information in the textboxes, but I need it to be there without having to click anything. I use VirtualBox and Visual Studio on a MacBook Pro. I have read similar posts, but the solutions don't change anything. I think I've added all the code needed, but here is the Data Binding methods just in case.
private void BindGrid()
{
dgvStudent.DataSource = typeof(List<Student>);
dgvStudent.DataSource = _roster;
}
private void BindItemsGrid(Student s)
{ dgvItems.DataSource = typeof(List<Items>);
if (s != null)
{
dgvItems.DataSource = s.Items;
}
}
private void BindPokeGrid(Student s)
{
dgvPokemon.DataSource = typeof(List<Pokemon>);
if (s!= null)
{
dgvPokemon.DataSource = s.Pokemon;
}
}
and what it looks like, for reference.
Sample WinForm
Hopefully this is good, thank you for the help.
how to update e specific value on a list..
for example when i click a button it adds the product on the list
name: coffe || quantity:1 || Price:2$
and when i click angain the same product the quantity increases by 1
i used this code but it doesnt change the number of the quantity.
private BindingList<recipt> Lista2 = new BindingList<recipt>();
private void addtolist(object sender, EventArgs e)
{
Button b = (Button)sender;
Product p = (Product)b.Tag;
recipt fat = new recipt ()
{
Name= p.Name,
quantity= 1,
price = p.Cmimi
};
bool found = false;
if (listBox1.Items.Count > 0)
{
foreach (var pr in Lista2)
{
if (pr.Name== p.Name)
{
pr.quantity= pr.quantity+ 1;
found = true;
}
}
if (!found)
{
fat.tot= fat.quantity* fat.price;
fat.Nr_bill = Convert.ToInt32(txtNrbill.Text);
Lista2.Add(fat);
}
}
else
{
fat.tot= fat.quantity* fat.price;
fat.Nr_bill = Convert.ToInt32(txtNrbill.Text);
Lista2.Add(fat);
}
fat.tot= fat.quantity* fat.price;
fat.Nr_bill = Convert.ToInt32(txtNrbill.Text);
Lista2.Add(fat);
pe.Faturs.Add(fat);
pe.SaveChanges();
Total = Total + (int)fat.price;
listBox1.SelectedIndex = listBox1.Items.Count - 1;
}
For updating values in ListBox automatically you need set BindingList of receipts to the ListBox.DataSource and make Receipt class implement INotifyPropertyChanged
public class Receipt : INotifyPropertyChanged
{
public string Name { get; }
public int Quantity { get; private set; }
public decimal Price { get; }
public string BillNumber { get; private set; }
public decimal Total => Price * Quantity;
public string Info => $"{nameof(Name)}: {Name} || {nameof(Quantity)}: {Quantity} || {nameof(Price)}: {Price:C} || {nameof(Total)}: {Total:C}";
public Receipt(string name, decimal price, string billNumber)
{
Name = name;
Price = price;
BillNumber = billNumber;
Quantity = 1;
}
public void AddOne()
{
Quantity += 1;
RaisePropertyChanged(nameof(Info));
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Then in the form
public class YourForm : Form
{
private readonly BindingList<Receipt> _Receipts;
public YourForm()
{
_Receipts = new BindingList<Receipt>();
listBox1.DisplayMember = "Info";
listBox1.DataSource = _Receipts;
}
private void AddToList(object sender, EventArgs e)
{
var button = (Button) sender;
var product = (Product) button.Tag;
var receiptInfo = _Receipts.FirstOrDefault(receipt => receipt.Name.Equals(product.Name));
if (receiptInfo == null)
{
receiptInfo = new Receipt(product.Name, product.Cmimi, txtNrbill.Text);
_Receipts.Add(receiptInfo);
}
else
{
receiptInfo.AddOne();
}
}
}
i'm very new as you can see in my programming, i'm making a simple program excercise. I would like to put for example the Item.price & Item.Name into Listbox2.
is it possible to put the arrayName into a variable and put it in a foreach loop?
Just to prevent a very long IF loop or switch, or a while loop.
For example :
Array variable = Drinks;
foreach(Product item in VARIABLE)
{
listBox2.Items.Add(item.ProductName + item.Price);
}
Ps: i already tryed with a temporary List where you put the drinkList into the Temporary list and then call it the product.Name and/or Product.price.
public partial class Form1 : Form
{
List<Product> Drinks = new List<Product>() {new Product("Coca Cola", 1.2F), new Product("Fanta", 2.0F), new Product("Sprite", 1.5F) };
List<Product> Bread = new List<Product>() { new Product("Brown Bread", 1.2F), new Product("White Bread", 2.0F), new Product("Some otherBread", 1.5F) };
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
if (comboBox1.Items.IndexOf(comboBox1.SelectedItem) == 0)
{
foreach (Product item in Drinks)
{
listBox1.Items.Add(item.ProductName);
}
}
else
{
foreach (Product item in Bread)
{
listBox1.Items.Add(item.ProductName);
}
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
// do something here
}
}
public class Product
{
private string productName;
private float price;
public Product(string productName, float price)
{
this.ProductName = productName;
this.Price = price;
}
public string ProductName
{
get { return productName; }
set { productName = value; }
}
public float Price
{
get { return price; }
set { price = value; }
}
}
I'm not sure what you are looking for exactly but maybe you could put the type of product (drink or bread) in the struct?
public struct Products
{
public string type;
public string name;
public double price;
}
You can then create the list
List<Products>
and use it in your foreach loop as you did in your example
It sounds like what you're looking for is:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
// start with Bread and change if necessary.
List<Product> products = Bread;
if (comboBox1.Items.IndexOf(comboBox1.SelectedItem) == 0)
{
//change the value of "products"
products = Drinks;
}
foreach (Product item in products)
{
listBox1.Items.Add(item.ProductName + item.Price);
}
}
I've got an application I'm building that inputs data into a list, using input textboxes on one tab (say Tab 1). When you hit the command button it adds the data (Book number, author, title, genre, # of pages and Publisher) to a list (books).
It then displays the title of the book in a listbox on tab 2. When you click the item in the listbox on tab 2, I want it to redisplay all the information you just input on tab 1, into textboxes on tab 2. But I can't get information to show up.
Below is my code, including the class I created for the project.
class Book
{
//attributes
private string callNumber;
private string bookTitle;
private string authorName;
private string genre;
private int numberOfPages;
private string publisher;
//constructor
public Book()
{
}
//accessor
public void SetNumber(string aNumber)
{
callNumber = aNumber;
}
public void SetTitle(string aTitle)
{
bookTitle = aTitle;
}
public void SetAuthor(String aName)
{
authorName = aName;
}
public void SetGenre(String aGenre)
{
genre = aGenre;
}
public void SetPages(int aPageNumber)
{
numberOfPages = aPageNumber;
}
public void SetPublisher(String aPublisher)
{
publisher = aPublisher;
}
public string GetNumber()
{
return callNumber;
}
public string GetTitle()
{
return bookTitle;
}
public string GetAuthor()
{
return authorName;
}
public string GetGenre()
{
return genre;
}
public int GetPages()
{
return numberOfPages;
}
public string GetPublisher()
{
return publisher;
}
}
public partial class Form1 : Form
{
List<Book> books;
public Form1()
{
InitializeComponent();
this.books = new List<Book>();
}
private void btnAdd_Click(object sender, EventArgs e)
{
Book aBook = new Book();
aBook.SetNumber(txtCallNumber.Text);
aBook.SetAuthor(txtAuthorName.Text);
aBook.SetTitle(txtBookTitle.Text);
aBook.SetGenre(txtGenre.Text);
aBook.SetPages(int.Parse(txtNumberOfPages.Text));
aBook.SetPublisher(txtPublisher.Text);
foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).Clear();
}
txtCallNumber.Focus();
txtAuthorName.Clear();
txtBookTitle.Clear();
txtCallNumber.Clear();
txtGenre.Clear();
txtNumberOfPages.Clear();
txtPublisher.Clear();
lstLibrary.Items.Add(aBook.GetTitle());
}
}
private void lstLibrary_SelectedIndexChanged(object sender, EventArgs e)
{
int index = 0;
foreach (Book book in books)
{
string tempTitle;
tempTitle = book.GetTitle();
if (tempTitle == (string)lstLibrary.SelectedItem)
break;
else
{
index++;
}
txtNumberRecall.Text = books[index].GetNumber();
txtTitleRecall.Text = books[index].GetTitle();
txtAuthorRecall.Text = books[index].GetAuthor();
txtGenreRecall.Text = books[index].GetGenre();
txtPagesRecall.Text = Convert.ToString(books[index].GetPages());
txtPublisherRecall.Text = books[index].GetPublisher();
break;
}
}
}
}
Once again, I'm trying to get the information from the listbox (in the click event) to show up in the textboxes.
Will something like this work?
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
foreach (string s in listBox1.Items)
{
i++;
if (i == 1)
{
textBox1.Text = s;
}
if (i == 2)
{
textBox2.Text = s;
}
if (i == 3)
{
textBox3.Text = s;
}
}
}
In the btnAdd_Click method, you are never saving the new aBook that you created. You need to add it to you books collection. Adding the title as an entry in lstLibrary.Items does not actually save the newly created object.
Also, you should review your looping structures. In btnAdd_Click() it looks like you will add it to lstLibrary once for each control that exists on your form. In lstLibrary_SelectedIndexChanged(), if you had actually added books to the collection in btnAdd_Click(), you would update the textboxes for the first book in the collection that does not match the selected book.