int itemCount;
public struct addressBook
{
public string lastName;
public string firstName;
public string street;
public string city;
public string state;
public int zip;
}
addressBook[] book = new addressBook[20];
public MainForm()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
string wholeName = "";
try
{
// if (lstDisplay.SelectedIndex != 0)
// {
book[itemCount].lastName = txtLast.Text;
book[itemCount].firstName = txtFirst.Text;
book[itemCount].street = txtStreet.Text;
book[itemCount].city = txtCity.Text;
book[itemCount].state = txtState.Text;
book[itemCount].zip = int.Parse(mTxtZip.Text);
itemCount++;
clear();
// lbltest.Text = book[itemCount].ToString();
//}
// else
// {
// MessageBox.Show("ERROR!!\n" + "Check your input.");
// }
}
catch (FormatException)
{
MessageBox.Show("ERROR!!\n" + "Check your input.");
}
for (int i = 0; i < book.Length; i++)
{
wholeName = book[i] + "\n" + wholeName ;
}
lstDisplay.Text = wholeName; //book[itemCount].lastName + " , " + book[itemCount].firstName;
lbltest.Text = wholeName;
}
what i want is when the user enters the info in text boxes for that info to be stored into an array then the last name, first name to be shown up in the listtextbox but all i get now is nothing. Any help would be appreciated. lstDisplay.Text is my list text box
1. What does the "clear" method do?
2. The loop that constructs "wholeName" is odd. You add "book[i]", which is a struct, to the front of it. That will call "book[i].ToString()" and will not return what you want. You then append an i9ncorrect newline. Newlines in Windows are \r\n, not \n, and you should be using Environment.NewLine instead anyway. You then proceed to append "wholeName" to itself at the end. I don't see how that could ever produce something useful. It is going to be a huge string full of garbage. Try this:
clear clears my text boxes-- i just didn't include that in my showcase for you
i tried using your code and nothing happens
i am also adding to a list box not a text box
Last edited by Darenr; January 11th, 2010 at 07:34 PM.
I don't know what you mean by "nothing happens". Why don't you use the debugger to see what is actually going on, i.e., what is the real content of "wholeName"?
I agree with BigEd781. You don't need the "for" loop to construct your wholeName variable. You don't want to loop over your entire address book each time a user enters their information. You just want to take the last name and first name that the user entered, construct a string using the last name and first name, and add that string as a list box item to the list box.
The following code worked for me:
Code:
int itemCount;
public struct addressBook
{
public string lastName;
public string firstName;
public string street;
public string city;
public string state;
public int zip;
}
addressBook[] book = new addressBook[20];
public Form1()
{
InitializeComponent();
itemCount = 0;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
book[itemCount].lastName = txtLast.Text;
book[itemCount].firstName = txtFirst.Text;
book[itemCount].street = txtStreet.Text;
book[itemCount].city = txtCity.Text;
book[itemCount].state = txtState.Text;
book[itemCount].zip = int.Parse(mTxtZip.Text);
lstDisplay.Items.Add(txtLast.Text + " , " + txtFirst.Text);
itemCount++;
}
catch (FormatException)
{
MessageBox.Show("ERROR!!\n" + "Check your input.");
}
}
ok i have most of it working now thanx to you peeps. Now how about if i have lets say a , b , c ,f in the list box and then someone inputs d . When i tried it that way as a sorted by last name array the new input goes on the end instead of the sorted location. How can i make it go to the sorted location as well as delete any items that i choose from the list box and have it sort back .
Code:
namespace Unit_10_Project
{
public partial class MainForm : Form
{
int itemCount;
public struct addressBook
{
public string lastName;
public string firstName;
public string street;
public string city;
public string state;
public int zip;
}
addressBook[] book = new addressBook[20];
public MainForm()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void clearBoxes()
{
txtCity.Clear();
txtFirst.Clear();
txtLast.Clear();
txtState.Clear();
txtStreet.Clear();
mTxtZip.Clear();
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
book[itemCount].lastName = txtLast.Text ;
book[itemCount].firstName = txtFirst.Text ;
book[itemCount].street = txtStreet.Text;
book[itemCount].city = txtCity.Text;
book[itemCount].state = txtState.Text;
book[itemCount].zip = int.Parse(mTxtZip.Text);
lBxDisplay.Items.Add(book[itemCount].lastName + " , " + book[itemCount].firstName);
itemCount++;
clearBoxes();
txtFirst.Focus();
}
catch (FormatException)
{
MessageBox.Show("ERROR!!\n" + "Check your input.");
}
}
private void btnDel_Click(object sender, EventArgs e)
{
if (lBxDisplay.SelectedIndex == -1)
{
MessageBox.Show("Please select an item first.", "No item selected");
}
else
{
lBxDisplay.Items.RemoveAt(lBxDisplay.SelectedIndex);
}
}
private void lBxDisplay_SelectedIndexChanged(object sender, EventArgs e)
{
int listIndex = lBxDisplay.SelectedIndex;
if(lBxDisplay.SelectedItem != null)
{
txtLast.Text = book[listIndex].lastName.ToString();
txtFirst.Text = book[listIndex].firstName.ToString();
txtCity.Text = book[listIndex].city.ToString();
txtState.Text = book[listIndex].state.ToString();
txtStreet.Text = book[listIndex].street.ToString();
mTxtZip.Text = book[listIndex].zip.ToString();
}
listIndex++;
}
}
}
Bookmarks