Can anyone give me any advice on the following.
Am trying to create code that shows the correct set of details when the Id number is entered. When I access the collectioon arrayList (REGList), the same set of details are always displayed. I cant seem to display the details in the collection arraylist for the relevant Id number. I tried using a foreach loop but this still doesnt work. The click event handler code am using is show below.
private void btnSearch_Click(object sender, System.EventArgs e)
{
string Id = txtStudID.Text;
if(txtStudID.Text == "")
MessageBox.Show("Please enter the 5 digit Student ID number and try again");
foreach(object o in RegList)
{
int Item = 0;
int count = 0;
count++;
if(o.GetType() == typeof(string))
{
if(o.ToString() == Id)
{
Item = count;
txtFirstnm.Text = RegList[Item + 1].ToString();
txtLastnm.Text = RegList[Item + 2].ToString();
txtAddress.Text = RegList[Item + 3].ToString();
txtCity.Text = RegList[Item + 4].ToString();
txtCourse.Text = RegList[Item + 5].ToString();
txtEmail.Text = RegList[Item + 6].ToString();
txtPhone.Text = RegList[Item + 7].ToString();
txtComment.Text = RegList[Item + 8].ToString();
}
}
}
}
Thanks for any help
Craig MurphyPosted Apr 26, 2006, 4:22 AM
This is not the best way of achieving your desired outcome. It would be cleaner and easier to create a Student class with the fields you need (ID, name, etc.) You could then create new instances for each Student and add those to the ArrayList.
If you are using C# 2.0, you might find this helpful:
http://www.c-sharpcorner.com/UploadFile/camurphy/csharpLists03302006170209PM/csharpLists.aspx?ArticleID=3f9c13d2-b85c-4b50-8f4b-2d2c59a17d36
Also:
http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingWithArrays11232005064036AM/WorkingWithArrays.aspx?ArticleID=40dc984d-2a91-4c50-b978-4e4165d83381
And this should make sense of my notion about a Student class:
http://www.c-sharpcorner.com/UploadFile/mahesh/ArrayListBinding10212005102338AM/ArrayListBinding.aspx?ArticleID=ea893798-1d66-4f55-8e4a-4581c3921cb5
Hope this helps
---
public
Form1(){
InitializeComponent();
}
ArrayList RegList = new ArrayList(); Random rndNumber = new Random(); private void Form1_Load(object sender, EventArgs e){
listBox1.Items.Add(AssignList(
"Frank Butcher"));listBox1.Items.Add(AssignList(
"Fred Smith"));}
private string AssignList(string name){
int NewNumber = rndNumber.Next(10000, 99999);RegList.Add(NewNumber.ToString());
RegList.Add(name);
return NewNumber.ToString();}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e){
string Id = listBox1.SelectedItem.ToString(); int Item = 0; foreach (object o in RegList){
// Fine, if your name isn't 5 chars long... if (o.GetType() == typeof(string) && (o.ToString().Length == 5)){
if (o.ToString() == Id){
MessageBox.Show(RegList[Item + 1].ToString());}
}
Item++;
}
}
}
mark jonesPosted Apr 25, 2006, 11:17 AM
private
void btnAddNewStud_Click(object sender, System.EventArgs e){
foreach(System.Windows.Forms.Control aControl in this.Controls){
if(aControl is System.Windows.Forms.TextBox & aControl.Text== "" & aControl .Tag == "ToValidate")
{
MessageBox.Show("Please ensure that the First Name, Last Name, Address and Course details are entered");
aControl.Focus();
return;}
}
FormComplete +=
new FormCompleteDelegate(AssignList);FormComplete();
}
private
void btnSearch_Click(object sender, System.EventArgs e){
string Id = txtStudID.Text; if(txtStudID.Text == "")MessageBox.Show("Please enter the 5 digit Student ID number and try again");
foreach(object o in RegList){
int Item = 0;Item++;
if(o.GetType() == typeof(string)){
if(o.ToString() == Id){
txtFirstnm.Text = RegList[Item + 1].ToString();
txtLastnm.Text = RegList[Item + 2].ToString();
txtAddress.Text = RegList[Item + 3].ToString();
txtCity.Text = RegList[Item + 4].ToString();
txtCourse.Text = RegList[Item + 5].ToString();
txtEmail.Text = RegList[Item + 6].ToString();
txtPhone.Text = RegList[Item + 7].ToString();
txtComment.Text = RegList[Item + 8].ToString();
}
}
}
}
private
void AssignList(){
Random rndNumber =
new Random(); int NewNumber = rndNumber.Next(10000, 99999);txtStudID.Text = NewNumber.ToString();
RegList.Add(txtStudID.Text);
RegList.Add(txtFirstnm.Text);
RegList.Add(txtLastnm.Text);
RegList.Add(txtAddress.Text);
RegList.Add(txtCity.Text);
RegList.Add(txtCourse.Text);
RegList.Add(txtEmail.Text);
RegList.Add(txtPhone.Text);
RegList.Add(txtComment.Text);
}
Craig MurphyPosted Apr 25, 2006, 10:55 AM