Trying to get information from my listbox to display in text boxes - c#

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.

Related

How to make a listbox display items from a checked list and radio buttons? - C# Windows Form

I'm doing a project where I am trying to simulate an ice cream parlor. For this specific section, I have the (mutually exclusive) radio buttons representing the dressing the customer can select. There are also a number of checked items (not mutually exclusive) which the customer can select in the checkedListBox. All of the items that a customer selects from the radio buttons and checkedListBox are supposed to appear in a listbox. so that the customer can keep track of all of the ordered items.
Of course, all of the code here is very unfinished and basic. I don't plan on adding any of the calculations for the prices until I make sure that the structure itself is working.
This is what I currently have so far:
private void GetToppings()
{
foreach (ListViewItem li in checkedListBox1.Items)
{
if (li.Selected == true)
{
label1.Text += li + " ";
}
}
if (checkedListBox1.SelectedItem.ToString() == "Sprinkles")
{
}
if (checkedListBox1.SelectedItem.ToString() == "Chocolate Chips")
{
}
if (checkedListBox1.SelectedItem.ToString() == "M&Ms")
{
}
if (checkedListBox1.SelectedItem.ToString() == "Oreos")
{
}
if (checkedListBox1.SelectedItem.ToString() == "Cookie Dough")
{
}
private void GetDressing()
{
if (radioButton1.Checked)
{
sDressing += "Caramel";
}
if (radioButton2.Checked)
{
sDressing += "Hot Fudge";
}
if (radioButton3.Checked)
{
sDressing += "Peanut Butter";
}
if (radioButton4.Checked)
{
sDressing += "Strawberry Syrup";
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i<18; i++)
{
listBox1.Items.Add(i);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Remove(listBox1.SelectedItem);
}
I am still very new to Windows Form programming in C#, so please forgive me if any of these questions/errors seem very basic.
The RadioButton use the following
var radioButton = Controls.OfType<RadioButton>().FirstOrDefault(x => x.Checked);
if (radioButton is not null)
{
// do something
}
For the CheckedListBox consider the following which populates via a model/class which has text and a identifier as in most cases at a later date you work with a data source this is important to keep track of items which you are not there yet but best to do it just the same.
Extension method to get items in the CheckedListBox. Place in a class file.
public static class CheckedListBoxExtensions
{
public static List<T> CheckedList<T>(this CheckedListBox sender)
=> sender.Items.Cast<T>()
.Where((_, index) => sender.GetItemChecked(index))
.Select(item => item)
.ToList();
}
Use a class/model for populating the CheckedListBox, ToString is used to display the item. Place in a class file.
public class Topping
{
public int Id { get; set; }
public string Name { get; set; }
public override string ToString() => Name;
}
Implementation
public partial class StackOverflowForm : Form
{
public StackOverflowForm()
{
InitializeComponent();
List<Topping> toppings = new List<Topping>
{
new Topping() { Id = 1, Name = "Sprinkles" },
new Topping() { Id = 2, Name = "Chocolate Chips" },
new Topping() { Id = 3, Name = "M&Ms" },
new Topping() { Id = 4, Name = "Oreos" },
new Topping() { Id = 5, Name = "Cookie Dough" }
};
checkedListBox1.DataSource = toppings;
}
private void GetToppingsButton_Click(object sender, EventArgs e)
{
List<Topping> toppings = checkedListBox1.CheckedList<Topping>();
if (toppings.Count > 0)
{
listBox1.DataSource = toppings;
}
else
{
listBox1.DataSource = null;
}
}
}

Update a List<t> object and his properties

I have a list MemoryClienti with items based on the ClienteModel class.
The method i use to add a new item to MemoryClienti is:
public bool CreateCliente(ClienteModel model)
{
bool empty = !MemoryClienti.Any();
if (empty)
{
ClienteModel clienteModel = new ClienteModel();
clienteModel.Cognome = model.Cognome;
clienteModel.Nome = model.Nome;
clienteModel.Indirizzo = model.Indirizzo;
clienteModel.IDCliente = StartID;
MemoryClienti.Add(clienteModel);
MessageBox.Show("Cliente aggiunto correttamente.");
}
else
{
int maxID = MemoryClienti.Count;
ClienteModel clienteModel = new ClienteModel();
clienteModel.Cognome = model.Cognome;
clienteModel.Nome = model.Nome;
clienteModel.Indirizzo = model.Indirizzo;
clienteModel.IDCliente = maxID;
MemoryClienti.Add(clienteModel);
MessageBox.Show("Cliente aggiunto correttamente.");
}
return true;
This method makes me able to add a new item, count for the number of items in the list, and set the new item's id as the result of the count, so it happpens for every item i add, and it's working.
Datas for item's "model" comes from form's textboxes:
private void aggiungiClienteButton_Click(object sender, EventArgs e)
{
if (cognomeTextBox.Text == "")
{
MessageBox.Show("Uno o più campi sono vuoti");
}
else if (nomeTextBox.Text=="")
{
MessageBox.Show("Uno o più campi sono vuoti");
}
else if (indirizzoTextbox.Text == "")
{
MessageBox.Show("Uno o più campi sono vuoti");
}
else
{
clienteModel.Cognome = cognomeTextBox.Text;
clienteModel.Nome = nomeTextBox.Text;
clienteModel.Indirizzo = indirizzoTextbox.Text;
dbMemoryManager.CreateCliente(clienteModel);
cognomeTextBox.Text = String.Empty;
nomeTextBox.Text = String.Empty;
indirizzoTextbox.Text = String.Empty;
}
}
My class is:
public class ClienteModel
{
public int IDCliente { get; set; }
public string Cognome { get; set; }
public string Nome { get; set; }
public string Indirizzo { get; set; }
}
The problem is: how can i update one of those items using textboxes without changing the id?
Here's a quick and dirty solution. You don't specify what kind of textboxes you are using. I'm assuming it's Windows Forms.
I modified your ClienteModel so that it looks like this:
public class ClienteModel
{
private static int _currentId = 0;
public int IDCliente { get; set; } = _currentId++;
public string Cognome { get; set; }
public string Nome { get; set; }
public string Indirizzo { get; set; }
public override string ToString()
{
return Nome;
}
}
Note that it manages the IDCliente field now and that it has a ToString member (you can set this to whatever string you want). You may want to show the IDCliente field in a read-only textbox on your form.
Then I created a simple Windows Forms form that has your three text boxes, a ListBox named ModelsListBox and two buttons AddButton (caption: "Add") and UpdateButton ("Update").
In the form class I created a little validation method (since I use it in two places). Note that you will only get one MessageBox even if you have multiple errors:
private bool ValidateFields()
{
var errors = new List<string>();
foreach (var tb in new[] {cognomeTextBox, nomeTextBox, indirizzoTextbox})
{
if (string.IsNullOrWhiteSpace(tb.Text))
{
errors.Add($"{tb.Name} must not be empty");
}
}
if (errors.Count > 0)
{
MessageBox.Show(string.Join(Environment.NewLine, errors), "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
//otherwise
return true;
}
Then I added three event handlers (wiring them up in the normal fashion from within the designer). The first is when the Add button is pressed:
private void AddButton_Click(object sender, EventArgs e)
{
if (!ValidateFields())
{
return;
}
var model = new ClienteModel
{
Cognome = cognomeTextBox.Text,
Nome = nomeTextBox.Text,
Indirizzo = indirizzoTextbox.Text,
};
ModelsListBox.Items.Add(model);
}
It creates a new ClienteModel and adds it to the listbox (assuming validation passes).
Then, I created a handler that updates the text boxes whenever the selection in the listbox changes:
private void ModelsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (ModelsListBox.SelectedItem is ClienteModel model)
{
cognomeTextBox.Text = model.Cognome;
nomeTextBox.Text = model.Nome;
indirizzoTextbox.Text = model.Indirizzo;
}
}
and finally, an update button handler:
private void UpdateButton_Click(object sender, EventArgs e)
{
if (!ValidateFields())
{
return;
}
if (ModelsListBox.SelectedItem is ClienteModel model)
{
model.Cognome = cognomeTextBox.Text;
model.Nome = nomeTextBox.Text;
model.Indirizzo = indirizzoTextbox.Text;
}
}
This isn't perfect. You should disable the Update button until a selection is made (and maybe enable only after a change is made in the text box).
More importantly, the string shown in the listbox for an item is based on the results of a call to ClienteModel.ToString made when the item is first added to the list. If you change the value of a field that is used to compute .ToString, the listbox doesn't update. There are a few ways around this (findable on Stack Overflow), but I thought this would be enough to get you started.

Change value in a list that is binded?

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?

Third DataGridView Selection_Changed Event Not Firing (but other 2 do)

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 save value in listview from other form

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.

Categories