I have a listview with subitems. The first 5 sub items are the name, items, total price, address and telephone.
The rest of the subitems contain the past list that I displayed for my order.
It is a pizzeria program and I want it to be able to get the customers info and order.
I can get the info but can't get the rest of the order.
I'm wondering how I can display the rest of my order if that makes sense.
Example Order:
Name: Claud
Items: 3
Total: 10.99
Address: (Blank)
Telephone: (Blank)
Order: Small Pizza
-Bacon
BreadSticks
Right now my messagebox looks like this:
Name: Claud
Items: 3
Total: 10.99
Address: (Blank)
Telephone: (Blank)
Order: Small Pizza
So I just want it to display the -Bacon and BreadSticks.
Source Code:
private void CustomerInfo_Click(object sender, EventArgs e)
{
ListViewItem customers = new ListViewItem(fullName.Text);
customers.SubItems.Add(totalcount.ToString());
customers.SubItems.Add(total.ToString());
customers.SubItems.Add(Address.Text);
customers.SubItems.Add(telephone.Text);
for (int i = 0; i < OrderlistBox.Items.Count; i++)
{
customers.SubItems.Add(OrderlistBox.Items[i].ToString());
}
Customers.Items.Add(customers);
MessageBox.Show("Sent order for " + fullName.Text.ToString() + " to screen.");
//CLEAR ALL FIELDS
OrderlistBox.Items.Clear();
fullName.Text = "";
Address.Text = "";
telephone.Text = "";
totalDue.Text = "";
totalItems.Text = "";
}
private void customerInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Customers.SelectedItems.Count != 0)
{
MessageBox.Show("Name: " + Customers.SelectedItems[0].SubItems[0].Text + "\n" +
"Adress: " + Customers.SelectedItems[0].SubItems[3].Text + "\n" +
"Telephone: " + Customers.SelectedItems[0].SubItems[4].Text + "\n" +
"Order: " +Customers.SelectedItems[0].SubItems[5].Text);
}
}
You can create custom message box by creating new winform that act as your messagebox.
Create public property on it to pass the value of your selecteditems something like:
Then on your form :
private void customerInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Customers.SelectedItems.Count != 0)
{
var myformmessagedialog = new MyFormMessageDialog
{
name = Customers.SelectedItems[0].SubItems[0].Text,
adress=Customers.SelectedItems[0].SubItems[3].Text,
telephone=Customers.SelectedItems[0].SubItems[4].Text
};
myformmessagedialog.ShowDialog();
}
}
Your MessageBoxDialogform:
MyFormMessageDialog : Form
{
public MyFormMessageDialog()
{
InitializeComponent();
}
public string name;
public string adress;
public string telephone;
private void MyFormMessageDialog_Load(object sender, EventArgs e)
{
lblName.Text = name;
lbladdress.Text = adress;
telephone.Text telephone;
//if you are saving ado.net stuff
//query username where name = name then bind it on a list box or a combo box
var Orderdata = //you retrieve info via DataTable;
lstOder.Items.Clear();
foreach (DataRow data in Orderdata.Rows)
{
var lvi = new ListViewItem(data["Order"].ToString());
// Add the list items to the ListView
lstlstOder.Items.Add(lvi);
}
}
}
Hope this help you.
Regards
Related
I'm working on a school project with C# Winforms where I have to create a vehicle sales invoice and ope a new form with information about the vehicle that was selected from a combobox. How do I get the Vehicle object or its properties based on the SelectedItem in the combobox?
The Vehicle objects are in a list which is bound to a BindingSource which is bound to the combobox.
I was able to pass static strings through to a new form in another component of this assignment, but I can't figure out how to retrieve the object information.
My list of vehicles bound to the combobox. DataRetriever is a class we were given to provide us with the Vehicle objects. They have auto-implemented properties (make, model, id, colour, etc.)
List<Vehicle> vehicles = DataRetriever.GetVehicles();
BindingSource vehiclesBindingSource = new BindingSource();
vehiclesBindingSource.DataSource = vehicles;
this.cboVehicle.DataSource = vehiclesBindingSource;
this.cboVehicle.DisplayMember = "stockID";
this.cboVehicle.ValueMember = "basePrice";
I want to be able to pass information to this form and display information about the selected vehicle with labels.
private void vehicleInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
VehicleInformation vehicleInformation = new VehicleInformation();
vehicleInformation.Show();
}
On Form_Load
List<VecDetails> lstMasterDetails = new List<VecDetails>();
private void frmBarcode_Load(object sender, EventArgs e)
{
VechicleDetails();
BindingSource vehiclesBindingSource = new BindingSource();
vehiclesBindingSource.DataSource = lstMasterDetails;
this.comboBox1.DataSource = vehiclesBindingSource;
this.comboBox1.DisplayMember = "stockID";
this.comboBox1.ValueMember = "basePrice";
}
In VechicleDetails() Method i'm just generating the sample value so i can i them to ComboBox
private void VechicleDetails()
{
//Sample Method to Generate Some value and
//load it to List<VecDetails> and then to ComboBox
for (int n = 0; n < 10; n++)
{
VecDetails ve = new VecDetails();
ve.stockID = "Stock ID " + (n + 1).ToString();
ve.basePrice = "Base Price " + (n + 1).ToString();
lstMasterDetails.Add(ve);
}
}
Now on comboBox1_SelectedIndexChanged event i'm getting the value of the item selected
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string strStockId = comboBox1.Text.ToString();
string strBasePrice = (comboBox1.SelectedItem as dynamic).basePrice;
label1.Text = strStockId + " - " + strBasePrice;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
[UPDATE: Added Form1() constructor]
I am trying to match some data from certain clases into combo box values in windows form application.
What I've done until now looks like this:
(This class has some region[] values that I want to store in a combobox, depending on enum Project)
public Form1()
{
formatWorker.DoWork += worker_DoWork;
formatWorker.RunWorkerCompleted += worker_RunWorkerCompleted;
extractWorker.DoWork += extractWorker_DoWork;
extractWorker.ProgressChanged += extractWorker_ProgressChanged;
extractWorker.RunWorkerCompleted += extractWorker_RunWorkerCompleted;
InitializeComponent();
projectBox.DataSource = Constant.projects.ToString();
projectBox.SelectedIndex = (int)Regions.Project.NBTevo;
PopulateUsbDevices();
}
class Regions
{
public enum Project
{
NBT = 0,
NBTevo = 1,
MGU = 2
}
string[] regions = { "ARG", "AUSNZ", "ECE", "IND", "ISR", "LA", "ME", "NA", "NAF", "NANT", "PAL", "SEA", "TC", "ZA" };
public string[] GetRegionsForProject(Project proj)
{
//all directories from /Databases/proj[i]
string[] allDirectories = Constant.ExtractFileNames(Directory.GetDirectories(Constant.path + "//" + Constant.projects[(int)proj]));
string[] availableSubDirectories = Enumerable.Intersect(allDirectories, regions).ToArray();
return availableSubDirectories;
}
}
The next class stores a certain pattern files
class DBVersion
{
public string[] GetVersion(string proj, string region)
{
string pattern = "^" + proj + "_" + region + "_" + #"(\d+\.\d+\.\d+_[a-zA-Z0-9_]+)\.iso$";
string[] files = Directory.GetFiles(Constant.path + "\\" + proj + "\\" + region + "\\images\\", "*.iso", SearchOption.AllDirectories);
return files;
}
}
I am trying to build a dependency, for example, depending on the selected values in projectBox and regionBox some versions will appear in versionBox
private void projectBox_SelectedIndexChanged(object sender, EventArgs e)
{
isoPaths.Clear();
populateRegions((Regions.Project)Enum.Parse(typeof(Regions.Project), projectBox.SelectedValue.ToString()));
regionBox.SelectedIndex = 0;
regionBox_SelectedIndexChanged(null, null);
}
private void regionBox_SelectedIndexChanged(object sender, EventArgs e)
{
string[] versionPaths = version.GetVersion(projectBox.SelectedValue.ToString(), regionBox.SelectedItem.ToString());
isoPaths.Clear();
isoPaths.AddRange(versionPaths);
populateVersions(Constant.ExtractFileNames(versionPaths));
//versionBox.SelectedIndex = 0;
//versionBox_SelectedIndexChanged(null, null);
}
private void versionBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void populateRegions(Regions.Project proj)
{
this.regionBox.DataSource = region.GetRegionsForProject(proj);
}
private void populateVersions(string[] versions)
{
this.versionBox.DataSource = version.GetVersion(
projectBox.SelectedItem.ToString(),
regionBox.SelectedItem.ToString());
}
After running, I don't have nothing stored in ComboBox
If you want to bind a DataSource to a ComboBox you should use :
An object that implements the IList interface or an Array
according to the ComboBox.DataSource Property documentation
So this line in the constructor of Form1:
projectBox.DataSource = Constant.projects.ToString();
has to be changed into this:
projectBox.DataSource = Constant.projects;
The rest of the comboboxes is empty because the problem cascades from one to the next. If one is empty then the rest cannot be filled appropriately
Hello i have got one listview that has txt files names inside one folder in desktop. So as i right data there is delete update and add so when i select one item and right click i want that data name to fill name textbox when its opened so its like basicly if i pick 1.1.2 and select update delete for will open and fixx textbox1 as the selectd datas name
-i tried few things like public statci string but when i use it and run program listview shows empty like there is nothing inside
private void liste_Load(object sender, EventArgs e)
{
#region listview fonksiyonları
listView1.FullRowSelect = true;
listView1.View = View.Details;
listView1.Columns.Add("Versiyon No", 133, HorizontalAlignment.Left);
listView1.Columns.Add("Açıklama", 200, HorizontalAlignment.Left);
listView1.Columns.Add("Tarih", 154, HorizontalAlignment.Left);
#endregion
#region listviewde txt dosyalarını gösterme
string[] dosyalar = System.IO.Directory.GetFiles(masaustu + "\\Versiyonlar");
string k = "";
int deger = 0;
foreach (var item in dosyalar)
{
ListViewItem lili = new ListViewItem();
deger = item.LastIndexOf("\\");
k = item.Remove(0, deger);
k = k.Remove(0, 1);
lili.Text = k;
StreamReader oku = new StreamReader(masaustu + "\\" + "Versiyonlar" + "\\" + k);
string OkunanVeri = oku.ReadToEnd();
string[] dizi = OkunanVeri.Split(new string[] { ";", "$" }, StringSplitOptions.RemoveEmptyEntries);
lili.SubItems.Add(dizi[0]);
lili.SubItems.Add(dizi[1]);
listView1.Items.Add(lili);
}
}
#endregion
#region txt içindekileri textboxda göstermek
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
liste frm = new liste();
try
{
string a = "";
a = "";
a = listView1.SelectedItems[0].SubItems[0].Text;
StreamReader oku = new StreamReader(masaustu + "\\" + "Versiyonlar" + "\\" + a);
string OkunanVeri = oku.ReadToEnd();
string[] dizi = OkunanVeri.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var item in dizi)
{
textBox1.Text = OkunanVeri;
}
oku.Close();
}
catch
{
}
}
this is the listview codes if its going to help you please help me i just started c#and i cant figure out how to do it
i dont know what u looking for , this may help
in list view form:
private void openMenuItem_Click(object sender, EventArgs e)//contentmenu openbtn
{
if (listView1.SelectedIndices.Count > 0)//in listview form//on_click opnbtn
{
string strSlctdtext=Convert.ToString(listView1.Items[listView1.SelectedIndices[0]].SubItems[1].Text);
TextBoxform objTextBoxform = new TextBoxform(strListSelectedtext);
if (objTextBoxform.ShowDialog() == DialogResult.OK)
{
//do somthing if u want some output from textboxform in return
}
}
}
in textbox form
public TextBoxform(string strListviewselected)
{
InitializeComponent();
textBox1.Text = strListviewselected;
}
I wrote a few code line in c# to iterate through a list, but i print in the textbox only the last one. the code that i wrote:
//For instantiation
Account account = new Account(0,"","", 0);
//A list for class Account
List<Account> listAccount = new List<Account>();
//Button for adding new Customer
private void button1_Click(object sender, EventArgs e)
{
account.CustomerID = int.Parse(customerIdTxt.Text);
account.CustomerFullName = customerNameTxt.Text;
account.CustomerAddress = customerAddrTxt.Text;
listAccount.Add(account);
}
//For printing the Customer's detailes in textbox
private void button6_Click(object sender, EventArgs e)
{
string showCustDetailes = "";
for(int i=0;i<listAccount.Count;i++)
{
showCustDetailes+=
"Customer ID : " + listAccount[i].CustomerID + Environment.NewLine +
"Customer Name : " + listAccount[i].CustomerFullName + Environment.NewLine +
"Customer Address : " + listAccount[i].CustomerAddress + Environment.NewLine +
"---------------------------------------------------" + Environment.NewLine;
}
viewDetailesTxt.Text = showCustDetailes;
}
can anyone help me how can i print the whole customers list
There's nothing wrong with the code that loops over the list (apart from not using foreach()). If you really only see one account, the problem is in displaying: make your textbox bigger or give it scrollbars.
Also, you're editing the same instance of account every time, so you're filling your list with multiple references to the same account. You must use new Account to instantiate a new one for every "button 1" click:
private void button1_Click(object sender, EventArgs e)
{
Account account = new Account(0,"","", 0);
// ...
listAccount.Add(account);
}
List<Account> listAccount = new List<Account>();
private void button1_Click(object sender, EventArgs e)
{
var account = new Account {
CustomerID = int.Parse(customerIdTxt.Text),
CustomerFullName = customerNameTxt.Text,
CustomerAddress = customerAddrTxt.Text
};
listAccount.Add(account);
}
private void button6_Click(object sender, EventArgs e)
{
var sb = new StringBuilder();
foreach(var account in listAccount)
{
sb.AppendFormat("Customer ID: {0}\nCustomer Name: {1}\nCustomer Address: {2}\n\n", account.CustomerID, account.CustomerFullName, account.CustomerAddress);
}
viewDetailesTxt.Text = sb.ToString();
}
Working on a project for school that has 2 forms. I want to add items to the listbox in the Display form when I click the show data button. It's showing the form but the form is blank. I think this is because the form object is being created 2x once when I click the add button and another when I click the show data button. How can I create a new object of the display form that can be used in any method in my main form?
Sorry there is a few things in here that I am still working on that were just ideas. I am a beginner so please keep the help in simple terms if at all possible. Thanks :)
private void addEmployee(Employee newEmployee)
{
//Get data from textboxes and use set methods in employee class
newEmployee.Name = EmployeeNameTextBox.Text;
newEmployee.BirthDate = EmployeeBirthDateTextBox.Text;
newEmployee.Dept = EmployeeDeptTextBox.Text;
newEmployee.HireDate = EmployeeHireDateTextBox.Text;
newEmployee.Salary = EmployeeSalaryTextBox.Text;
}
private void AddButton_Click(object sender, EventArgs e)
{
//New list for employee class objects - employeelist
List<Employee> employeeList = new List<Employee>();
//Create new instance of Employee class - newEmployee
Employee newEmployee = new Employee();
bool errorCheck = false;
CheckForms(ref errorCheck);
if (!errorCheck)
{
//Gather input from text boxes and pass newEmployee object
addEmployee(newEmployee);
//Add object to employeeList
employeeList.Add(newEmployee);
Display myDisplay = new Display();
myDisplay.OutputListBox.Items.Add(" Bob");
//" " + newEmployee.BirthDate + " " +
//newEmployee.Dept + " " + newEmployee.HireDate + " " + newEmployee.Salary);
You are creating two separate instances of mydisplay.Create a single instance when the Form Loads and refer to that when you call ShowDataButton_Click
namespace WK4
{
public partial class MainForm : Form
{
Display myDisplay;
public MainForm()
{
InitializeComponent();
}
//Method to clear form input boxes
private void ClearForm()
{
EmployeeNameTextBox.Text = "";
EmployeeBirthDateTextBox.Text = "";
EmployeeDeptTextBox.Text = "";
EmployeeHireDateTextBox.Text = "";
EmployeeSalaryTextBox.Text = "";
FooterLabel.Text = "";
}
//Method to check for blank input on textboxes
private void CheckForms(ref bool error)
{
if (EmployeeNameTextBox.Text == "" || EmployeeBirthDateTextBox.Text == "")
{
MessageBox.Show("Please do not leave any fields blank");
error = true;
}
else if (EmployeeDeptTextBox.Text == "" || EmployeeHireDateTextBox.Text == "")
{
MessageBox.Show("Please do not leave any fields blank");
error = true;
}
else if (EmployeeSalaryTextBox.Text == "")
{
MessageBox.Show("Please do not leave any fields blank");
error = true;
}
else
error = false;
}
private void addEmployee(Employee newEmployee)
{
//Get data from textboxes and use set methods in employee class
newEmployee.Name = EmployeeNameTextBox.Text;
newEmployee.BirthDate = EmployeeBirthDateTextBox.Text;
newEmployee.Dept = EmployeeDeptTextBox.Text;
newEmployee.HireDate = EmployeeHireDateTextBox.Text;
newEmployee.Salary = EmployeeSalaryTextBox.Text;
}
private void AddButton_Click(object sender, EventArgs e)
{
//New list for employee class objects - employeelist
List<Employee> employeeList = new List<Employee>();
//Create new instance of Employee class - newEmployee
Employee newEmployee = new Employee();
bool errorCheck = false;
CheckForms(ref errorCheck);
if (!errorCheck)
{
//Gather input from text boxes and pass newEmployee object
addEmployee(newEmployee);
//Add object to employeeList
employeeList.Add(newEmployee);
Display myDisplay = new Display();
myDisplay.OutputListBox.Items.Add(" Bob");
//" " + newEmployee.BirthDate + " " +
//newEmployee.Dept + " " + newEmployee.HireDate + " " + newEmployee.Salary);
//Clear Form after adding data
ClearForm();
//Print footer employee saved info
FooterLabel.Text = ("Employee " + newEmployee.Name + " saved.");
}
}
//Exit the form/program
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
//Method to clear the form and reset focus
private void ClearButton_Click(object sender, EventArgs e)
{
ClearForm();
EmployeeNameTextBox.Focus();
}
private void ShowDataButton_Click(object sender, EventArgs e)
{
myDisplay.ShowDialog();
}
private void MainForm_Load(object sender, EventArgs e)
{
myDisplay = new Display();
}
}
}
You are making 2 different instances of display class, in first instance you are adding data and you are displaying it using the second instance thats why you are getting a blank form
create a display class object on your MainForm_Load
private void MainForm_Load(object sender, EventArgs e)
{
Display myDisplay = new Display();
}
and the use this object (myDisplay) to add and display data in your AddButton_Click and ShowDataButton_Click methods respectively.