Using next and previous buttons to navgiate through a list - c#

So I am having some trouble with my C#forms. I have created a list of customers in which they have a there name, suburb, and bank account balances stored. Using a next and previous buttons I need to be able to allow the user to navigate through the current 5 objects in the list. How I was planning on doing so was when the user would click either the button, the text boxes would be filled with the relevant information. The reason why customer and other details are buttons is that later i need to be able to update the information stored in those fields, so i thought a good way to do that would be to erase what was already in the text box, type the new information, then press the button to update.
Anyways, my main issue is i need to use my view buttons to move through my list
This is how my form looks like:
My current form code is this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Customer c1 = new Customer("Sibel Yilmaz", "Wollongong", 2500, 3000, 5000);
Customer c2 = new Customer("John Doe", "Figtree", 2547, 2500, 3655);
Customer c3 = new Customer("Mariah Moore", "Coniston", 2500, 7000, 36000);
Customer c4 = new Customer("Jessica Blackshaw", "Bellambi", 3500, 6000, 4750);
Customer c5 = new Customer("Suzan Yilmaz", "Wollongong", 2500, 2000, 47110);
List<Customer> customers = new List<Customer>();
customers.Add(c1);
customers.Add(c2);
customers.Add(c3);
customers.Add(c4);
customers.Add(c5);
}
private void Form1_Load(object sender, EventArgs e) { }
private void button2_Click(object sender, EventArgs e) { }
private void button6_Click(object sender, EventArgs e) { }
private void textBox4_TextChanged(object sender, EventArgs e) { }
private void Customer_Click(object sender, EventArgs e) { }
}
And my Customer class is:
public class Customer
{
protected string name;
protected string suburb;
protected int postcode;
protected double credit_balance;
protected double saving_balance;
public Customer(string name, string suburb, int postcode, double credit_balance,
double saving_balance)
{
this.name = name;
this.suburb = suburb;
this.postcode = postcode;
this.credit_balance = credit_balance;
this.saving_balance = saving_balance;
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Suburb
{
get { return suburb; }
set { suburb = value; }
}
public int Postcode
{
get { return postcode; }
set { postcode = value; }
}
public double Credit_Balance
{
get { return credit_balance; }
set { credit_balance = value; }
}
public double Savinig_Balance
{
get { return saving_balance; }
set { saving_balance = value; }
}
}
Please help me out, and let me know what the best way to go about this would be.

The first thing that you need to do is make your customers list have class level visibility so that you can access it in your button click events.
public partial class Form1 : Form
{
int index = 0;
List<Customer> customers = new List<Customer>();
public Form1()
{
... The remainder of your Constructor code
Once you do that you should be able to do something like this.
private void next_Click(object sender, EventArgs e)
{
if (index < customers.Count - 1)
{
index += 1;
textBox1.Text = customers[index].Name;
...
}
}
private void previous_Click(object sender, EventArgs e)
{
if (index > 0)
{
index -= 1;
textBox1.Text = customers[index].Name;
...
}
}

Using DataBinding ,and if you need to change the content of some field you could use the button to update or simply set the DataSourceUpdateMode as i do here to OnPropertyChange and every time you change the text in one of the textboxes it will update the datasource(in this case the List).You could define the Binding object seperatly then add it to the textbox.DataBindings so you could format and parse it,but here i just added them directly for demonstration purposes:
List<Customer> customers;
public Form1()
{
InitializeComponent();
//you could add the Customers directly to the add method of the list.
Customer c1 = new Customer("Sibel Yilmaz", "Wollongong", 2500, 3000, 5000);
Customer c2 = new Customer("John Doe", "Figtree", 2547, 2500, 3655);
Customer c3 = new Customer("Mariah Moore", "Coniston", 2500, 7000, 36000);
Customer c4 = new Customer("Jessica Blackshaw", "Bellambi", 3500, 6000, 4750);
Customer c5 = new Customer("Suzan Yilmaz", "Wollongong", 2500, 2000, 47110);
customers = new List<Customer>();
customers.Add(c1);
customers.Add(c2);
customers.Add(c3);
customers.Add(c4);
customers.Add(c5);
textBox1.DataBindings.Add("Text", customers, "Name",true,DataSourceUpdateMode.OnPropertyChanged);
textBox2.DataBindings.Add("Text", customers, "Suburb", true, DataSourceUpdateMode.OnPropertyChanged);
textBox3.DataBindings.Add("Text", customers, "Postcode", true, DataSourceUpdateMode.OnPropertyChanged);
textBox4.DataBindings.Add("Text", customers, "Credit_Balance", true, DataSourceUpdateMode.OnPropertyChanged);
textBox5.DataBindings.Add("Text", customers, "Savinig_Balance", true, DataSourceUpdateMode.OnPropertyChanged);
}
Then in your previous and next buttons:
private void PreviousBtn_Click(object sender, EventArgs e)
{
--this.BindingContext[this.customers].Position;
}
private void NextBtn_Click(object sender, EventArgs e)
{
++this.BindingContext[this.customers].Position;
}

Related

How to send Datagridview Values to Main form

I would like to send my Datagridview values to my Combobox in my mainform.
This is my code in my Datagridview form :
private void EquipmentList_Load(object sender, EventArgs e)
{
DataTable.RowCount = 30;
//Beam Description default value
DataTable.Rows[0].Cells[1].Value = "8.000m x 0.400m x 0.550m";
}
Below code is what i've used but nothing happened :
private void MainForm_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
EquipmentList ELform = new EquipmentList();
//Beam Combo Box default values
var BCBi1 = ELform.DataTable.Rows[0].Cells[1].Value.ToString();
this.BeamCB.Items.Add(BCBi1);
}
When i run it i'm getting this error:
error when running
An advance thank you for all those who will help me.
DataTable.Rows[0].Cells[1].Value is assigned in EquipmentList form when it is loaded.
EquipmentList ELform = new EquipmentList(); - here form is created but not loaded (you need to invoke Show or ShowDialog for that).
I suggest to declare publicly accessible default value (and not rely on existance of DataTable object with 1 row, which is a low-level implementation detail)
public const string DefaultSize = "8.000m x 0.400m x 0.550m";
private void EquipmentList_Load(object sender, EventArgs e)
{
DataTable.RowCount = 30;
DataTable.Rows[0].Cells[1].Value = DefaultSize;
}
and then
private void MainForm_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.BeamCB.Items.Add(EquipmentList.DefaultSize);
}
To the question "So is there any possible to load that values wihtout clicking/showing my equipmentlist form." Absolutely. Start by building a class for your data:
public class gridviewdata
{
public string content { get; set; }
public int row { get; set; }
public int cell { get; set; }
public gridviewdata(string content, int row, int cell)
{
this.content = content;
this.row = row;
this.cell = cell;
}
}
add a mediator class:
public static class mediator
{
public static List<gridviewdata> gridviewdatalist;
}
then add this code to your ELform load event:
private void EquipmentList_Load(object sender, EventArgs e)
{
DataTable.RowCount = 30;
//Beam Description default value
DataTable.Rows[0].Cells[1].Value = "8.000m x 0.400m x 0.550m";
mediator.gridviewdatalist.Add(new gridviewdata("8.000m x 0.400m x 0.550m", 0, 1);
}
then add a new method identifying your specific row to your MainForm:
private gridviewdata selectdata(int row, int cell)
{
foreach(gridviewdata data in mediator.gridviewdatalist)
{
if(data.row == row && data.cell == cell)
{
return data;
}
}
return null;
}
And finally call that code in your MainForm:
private void MainForm_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
//and call it there
var BCBi1 = selectdata(0, 1);
this.BeamCB.Items.Add(new Item(BCDi1, 1)); //Add value and index
}
Hope this helps.

Binding Source Control

I have a binding source control in my form. I utilize the binding source current_changed event in the form for doing some special tasks, the problem that I faces is, in the form_Load event I make a list as the datasource of this binding source and the current_changed event has been called multiple times. Why it's like that?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<Employee> listEmployee = new List<Employee>();
for (int i = 1; i <= 10; i++)
{
Employee emp = new Employee();
emp.EmployeeName = "user" + i;
emp.EmployeeAddress = "Address" + i;
listEmployee.Add(emp);
}
bindingSource1.DataSource = listEmployee;
dataGridView1.DataSource = bindingSource1;
}
private void bindingSource1_CurrentChanged(object sender, EventArgs e)
{
MessageBox.Show("Hai");
}
}
public class Employee
{
private string Name;
private string Address;
public string EmployeeName {
get {return Name;}
set { Name = value; }
}
public string EmployeeAddress
{
get { return Address; }
set { Address = value; }
}
}
For my special case I created a flag on the page load and prevented the execution of currentchanged event more than once. I couldn't find any other way to solve it.

C# Why wouldn't it display the values I set in the List?

Hi I really need help on this inheritance project. I need my form to display all the data in each label when the user selected a value in the combobox. I got all the data declared in the list but I dont know why the label still shows ZEROs when I select value.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void accountNumComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (accountNumComboBox.SelectedIndex == 0)
{
SavingsAccount savings = new SavingsAccount();
ownerIdLabel.Text = savings.OwnerId;
balanceLabel.Text = savings.Balance.ToString("c");
interestLabel.Text = (string.Format("{0}%", savings.Interest));
}
}
private void Form1_Load(object sender, EventArgs e)
{
List<SavingsAccount> savings = new List<SavingsAccount>();
SavingsAccount savings1 = new SavingsAccount();
savings1.OwnerId = "0001";
savings1.AccountNumber = "31-1000";
savings1.Balance = 100m;
savings1.Interest = 0.01;
SavingsAccount savings2 = new SavingsAccount();
savings2.OwnerId = "0002";
savings2.AccountNumber = "31-1001";
savings2.Balance = 1000m;
savings2.Interest = 0.0125;
And here is my SavingsAccount class
class SavingsAccount : BankAccount
{
//field
private double _interest;
//constructor
public SavingsAccount(string ownerID, string acctNumber, decimal balance, double interest)
: base(ownerID, acctNumber, balance)
{
_interest = interest;
}
//interest property
public double Interest
{
get { return _interest; }
set { _interest = value; }
}
public SavingsAccount()
{
}
}
I'll appreciate your help. Thank you!
Although you declare values in the code below, it doesn't appear that you are actually using it.
private void Form1_Load(object sender, EventArgs e)
{
List<SavingsAccount> savings = new List<SavingsAccount>();
SavingsAccount savings1 = new SavingsAccount();
savings1.OwnerId = "0001";
savings1.AccountNumber = "31-1000";
When you write the code below, you're creating a new localized object inside of the event; you're not using the values you tried to give it before:
private void accountNumComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (accountNumComboBox.SelectedIndex == 0)
{
SavingsAccount savings = new SavingsAccount();
ownerIdLabel.Text = savings.OwnerId;
balanceLabel.Text = savings.Balance.ToString("c");
My suggestion would be to move this piece of code: List<SavingsAccount> savings = new List<SavingsAccount>();just underneath of your private double _interest declaration. That would make it accessible by your other methods and events.

using a listbox to display items from a class

My program compiles and to me it makes sense.
I want to know how to get 'name' to list in my listbox.
I'm trying to use an array of classes so I can add salesmen. A new class will be created every time a person is to be added.
This way the name is a way of calling all the data in that class.
When I execute the program everything looks like its doing what it's suppose to do but it just lists 'form1' in the list box when i press the list names button
This is what i mean:
Where am I going wrong?
SalesmanClass
namespace WindowsFormsApplication1
{
class SalesmanClass
{
private string name;
public string cNum;
public string Email;
public string address;
public string gArea;
public int tSales;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
Form 1
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form2 w2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (w2 == null)
{
w2 = new Form2();
w2.Show();
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Object names;
names = Name;
listBox1.Items.Add(Name);
}
}
}
Form 2
//form2
namespace WindowsFormsApplication1
public partial class Form2 : Form
{
SalesmanClass[] salesman = new SalesmanClass[] { };
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length != 0)
{
for (int i = 0; i > salesman.Length; i++)
{
if (salesman[i] == null)
{
salesman[i].Name = textBox1.Text;
break;
}
}
this.Close();
}
else
{
MessageBox.Show("Please Input a Name");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
In this method:
private void button2_Click(object sender, EventArgs e)
{
Object names;
names = Name; // <--- Using this.Name, i.e. Form.Name, NOT SalesmanClass.Name
listBox1.Items.Add(Name);
}
You have accidentally used the Name property of the Form itself (which naturally is "form1").
You need to have a SalesmanClass object at this point, and use the Name property of that instead.
You don't currently have a list of salesmen in your Form1, so you will need to add one and use that.
Also, if you have a list or array of SalesmanClass objects, you should create a List<string> from them and use that to initialise the listbox, something like:
SalesmanClass[] salesmen = new SalesmanClass[] {};
// ...
List<string> names = new List<string>();
foreach (var salesman in salesmen)
names.Add(salesman.Name);
listBox1.Items.AddRange(names);
You can do this using Linq too, but I don't want to confuse you by introducing that into the mix!
In your button2_Click, you have :
names = Name;
What does this Name belong to ? I suspect it belongs to Form1, that's why it's been displaying "form1". If that's the case, you just need to get your SalesmanClass object and get the Name from it.

Refresh a listbox's datasource

I have a form for my update method, the form is in detail view. Next to the textboxes I have a listbox which shows the names of all the names in the database table. Under the listbox I also have an extra textbox to quick search the name in case user would like to type it in.
When I go to update one of the names, such as changing John to Jonathan, the database updates with the new name as I have checked on sql server, but the name in the listbox does not change! There's a dirty way to fix this by moving the position the listbox is selected on currently to movefirst(). However, under the listbox I have the textbox which is a quick search as I've mentioned, so I go to type Jonathan in the search text box, but nothing appears. However, if I type the former name John, then I get the details of this row in the table.
Is there a way I can fix this?
UPDATE 1:
Ive tried making the listbox datasource null then reassigning it again but it doesen't work. Ive put my code for my update form below.
namespace WindowsFormsApplication1
{
public partial class updateContact : Form
{
public updateContact()
{
InitializeComponent();
}
private void updateContact_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'tblcontactsupdate.tblContacts' table. You can move, or remove it, as needed.
this.tblContactsTableAdapter.Fill(this.tblcontactsupdate.tblContacts);
}
private void btnUpdateContact_Click(object sender, EventArgs e)
{
int x;
Program.da.UpdateCommand = new SqlCommand("Update tblContacts SET FIRSTNAME = #FIRSTNAME, LASTNME = #LASTNME WHERE ID = #ID", Program.cs);
Program.da.UpdateCommand.Parameters.Add("#FIRSTNAME", SqlDbType.VarChar).Value = fIRSTNAMETextBox.Text;
Program.da.UpdateCommand.Parameters.Add("#LASTNME", SqlDbType.VarChar).Value = lASTNMETextBox.Text;
Program.da.UpdateCommand.Parameters.Add("#ID", SqlDbType.VarChar).Value = iDTextBox.Text;
Program.cs.Open();
x = Program.da.UpdateCommand.ExecuteNonQuery();
Program.cs.Close();
if (x >= 1)
{
MessageBox.Show("Record(s) has been updated");
Program.ds.Clear();
Program.da.Fill(Program.ds);
txtfindUpdatecontact.Text = "";
//lbupdateContact.DataSource = null;
//lbupdateContact.DataSource = this.tblcontactsupdate.tblContacts;
}
}
private void txtfindUpdatecontact_TextChanged(object sender, EventArgs e)
{
if (!txtfindUpdatecontact.Text.Equals(""))
{
this.tblContactsBindingSource.Filter = "FIRSTNAME = '" + txtfindUpdatecontact.Text + "'";
}
else
{
this.tblContactsBindingSource.RemoveFilter();
}
}
private void lbupdateContact_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void iDTextBox_TextChanged(object sender, EventArgs e)
{
}
private void fIRSTNAMETextBox_TextChanged(object sender, EventArgs e)
{
}
private void lASTNMETextBox_TextChanged(object sender, EventArgs e)
{
}
}
}
You will have to set the DataSource of your listbox one more time after updating the source.
Something like below: It's my data:
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
public class MyDataSource
{
public static List<Person> Persons = new List<Person>
{
new Person{Age=30,Name="Ram"},
new Person{Age=33,Name="Rahim"},
};
}
then in the form's constructor you can do:
listBox1.DataSource = MyDataSource.Persons;
listBox1.DisplayMember = "Age";
then for updation, something like below:
private void button1_Click(object sender, EventArgs e)
{
MyDataSource.Persons[0].Age = 45;
listBox1.DataSource = null;
listBox1.DataSource = MyDataSource.Persons;
listBox1.DisplayMember = "Age";
}
This is just an example change code according to your need.
If your DataSource is a DataTable, all you have to do is calling AcceptChanges(), like this:
listBox.DataSource = null;
((DataTable)listBox.DataSource).AcceptChanges();

Categories

Resources