-
January 22nd, 2010, 02:46 PM
#31
Re: Display highlighted data from Listbox in Textboxes
The class I created was striking similar to yours, but I tried to substitute the "text" you have here with variable names, and the names of the textboxes, and I got errors. I don't know how to get the data entered into a textbox into the class. thanks.
-
January 22nd, 2010, 03:11 PM
#32
Re: Display highlighted data from Listbox in Textboxes
And I need to use an array to store the data, also.
I really wish you didn't have to but i guess back to the basics...
Code:
static void Main(string[] args)
{
// Define number of contacts and contact properties (ie: name/address/number).
const int NumContacts = 2;
const int NumContactProperties = 3;
// Create a 2D array of contacts/contact properties.
/*
* contacts
* |
* > contact
* |
* > Name
* > Address
* > Number
* > contact
* |
* > Name
* > Address
* > Number
*
*/
string[][] contacts = new string[NumContacts][]; // 2D array declaration.
contacts[0] = new string[NumContactProperties]; // Initialize first contact array indices.
contacts[0][0] = "Mario Catch";
contacts[0][1] = "123 WillyNilly Ln";
contacts[0][2] = "555-555-5555";
contacts[1] = new string[NumContactProperties]; // Initialize second contact array indices.
contacts[1][0] = "Luigi IHadTo";
contacts[1][1] = "124 WillyNilly Ln";
contacts[1][2] = "555-555-5556";
foreach (string[] contact in contacts)
{
if (contact != null)
{
Console.WriteLine("Contact:");
Console.WriteLine("-Name: {0}\n-Address: {1}\n-PhoneNumber: {2}\n\n",
contact[0],
contact[1],
contact[2]);
}
}
Console.ReadLine();
}
Then you could do:
Code:
contacts[0][0] = textBoxName.Text;
contacts[0][1] = textBoxAddress.Text;
contacts[0][2] = textBoxPhoneNumber.Text;
You gain so much more by using a Collection<T> combined with encapsulation and object oriented programming.
-
January 22nd, 2010, 03:13 PM
#33
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by CarlMartin
The class I created was striking similar to yours, but I tried to substitute the "text" you have here with variable names, and the names of the textboxes, and I got errors. I don't know how to get the data entered into a textbox into the class. thanks.
BigEd went over this briefly.
The class has a constructor that sets its properties to the parameter values of the constructor. The class also has public properties, so you don't have to use the constructor/
So, you could do:
Code:
Contact contact = new Contact();
contact.FirstName = textBoxFirstName.Text;
// and so on...
Or,
Code:
Contact contact = new Contact(textBoxFirstName.Text, textBoxLastName.Text, ...);
-
January 22nd, 2010, 03:18 PM
#34
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by mariocatch
BigEd went over this briefly.
The class has a constructor that sets its properties to the parameter values of the constructor. The class also has public properties, so you don't have to use the constructor/
So, you could do:
Code:
Contact contact = new Contact();
contact.FirstName = textBoxFirstName.Text;
// and so on...
Or,
Code:
Contact contact = new Contact(textBoxFirstName.Text, textBoxLastName.Text, ...);
I will give this a shot. I guess I can then try to store each contact (instance of the class) into an array. Maybe an array of structures would be easier. I don't know. I will see. This problem really sucks because I have to use arrays to store the data, I have to be able to pull the information related to a contact when it is highlighted in the listbox, then send that data to individual textboxes. Also, there can only be a MAX of 20 entries. This is turning out to be a huge pain in the a$$.
-
January 22nd, 2010, 03:24 PM
#35
Re: Display highlighted data from Listbox in Textboxes
You can store any type of 'object' in an array.
Code:
static void Main(string[] args)
{
const int NumContacts = 2;
Contact[] contacts = new Contact[NumContacts];
contacts[0] = new Contact("Mario", "Catch", "123 ASDF", "555-555-5555");
contacts[1] = new Contact("Luigi", "IHadTo", "123 FDSA", "555-555-5556");
foreach (Contact contact in contacts)
{
contact.Show();
}
Console.ReadLine();
}
public class Contact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string PhoneNumber { get; set; }
public Contact() { }
public Contact(string firstName, string lastName, string address, string phoneNumber)
{
FirstName = firstName;
LastName = lastName;
Address = address;
PhoneNumber = phoneNumber;
}
public void Show()
{
Console.WriteLine(
"Person Information:\n-Name: {0}\n-Address: {1}\n-PhoneNumber: {2}\n\n",
FirstName + ' ' + LastName,
Address,
PhoneNumber);
}
}
This is using Contact[], which is an array of Contacts.
You can set the properties of the Contact to the values of your TextBox's instead of hardcoded strings.
Last edited by mariocatch; January 22nd, 2010 at 03:25 PM.
Reason: Description.
-
January 22nd, 2010, 03:42 PM
#36
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by mariocatch
I really wish you didn't have to but i guess back to the basics...
Code:
static void Main(string[] args)
{
// Define number of contacts and contact properties (ie: name/address/number).
const int NumContacts = 2;
const int NumContactProperties = 3;
// Create a 2D array of contacts/contact properties.
/*
* contacts
* |
* > contact
* |
* > Name
* > Address
* > Number
* > contact
* |
* > Name
* > Address
* > Number
*
*/
string[][] contacts = new string[NumContacts][]; // 2D array declaration.
contacts[0] = new string[NumContactProperties]; // Initialize first contact array indices.
contacts[0][0] = "Mario Catch";
contacts[0][1] = "123 WillyNilly Ln";
contacts[0][2] = "555-555-5555";
contacts[1] = new string[NumContactProperties]; // Initialize second contact array indices.
contacts[1][0] = "Luigi IHadTo";
contacts[1][1] = "124 WillyNilly Ln";
contacts[1][2] = "555-555-5556";
foreach (string[] contact in contacts)
{
if (contact != null)
{
Console.WriteLine("Contact:");
Console.WriteLine("-Name: {0}\n-Address: {1}\n-PhoneNumber: {2}\n\n",
contact[0],
contact[1],
contact[2]);
}
}
Console.ReadLine();
}
Then you could do:
Code:
contacts[0][0] = textBoxName.Text;
contacts[0][1] = textBoxAddress.Text;
contacts[0][2] = textBoxPhoneNumber.Text;
You gain so much more by using a Collection<T> combined with encapsulation and object oriented programming.
I have a question on this method you have here. I see where you declare the number of contacts. In this problem, I need to have a MAXIMUM of 20 contacts, but it may be any number between 1 and 20. The user will enter the contact info, then click the "Add" button to add the contact information. How can I tell this to have a maximum of 20 entries without defining each entry from the beginning? This is the stupidest problem I have ever seen. ***.
-
January 22nd, 2010, 03:48 PM
#37
Re: Display highlighted data from Listbox in Textboxes
Change the constant int NumContacts from 2 to 20.
Only initialize the indexes you need. The rest will be null, which is fine.
(You can't expand arrays in C# at runtime... what you set it to is what it's set to... which is why we are all saying use a Collection or List).
If you add a new contact to this array, you set the next index to a new string[NumContactProperties]. This will initialize a new index in your contacts array, with another array of properties for that contact.
You can remove a contact by setting an index to null.
-
January 22nd, 2010, 03:51 PM
#38
Re: Display highlighted data from Listbox in Textboxes
wow, ok, thanks. I will let you know how it goes. This is one I should just skip, lol.
-
January 23rd, 2010, 10:34 PM
#39
Re: Display highlighted data from Listbox in Textboxes
I just started working on this again. I created a structure, as you can see in the code. When I change the Array data type to the structure name, I get like 5 errors in the code, even if I change all the instances of the old Array name to the new Array name. I do not get this at all.
Code:
public partial class frmAddressBook : Form
{
public struct AddressBookEntries
{
public string firstName;
public string lastName;
public string address;
public string city;
public string state;
public string zipCode;
}
// Create Array to hold contact information
string[] Contacts = new string[20];
int i = 0;
public frmAddressBook()
{
InitializeComponent();
}
private void AddressBook_Load(object sender, EventArgs e)
{
}
private void btnExit_Click(object sender, EventArgs e)
{
// Close program
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
// Declare variables
string lastName = tbxLastName.Text;
string firstName = tbxFirstName.Text;
string street = tbxStreet.Text;
string city = tbxCity.Text;
string state = tbxState.Text;
string zip = tbxZip.Text;
// Add contact information for New contact to array
if (i < 20)
{
Contacts[i] = lastName + ", " + firstName;
i++;
}
else
{
MessageBox.Show("The Address Book Can Hold a Maximum of 20 Contacts.", "Address Book Full");
}
//Clear TextBoxes
tbxFirstName.Text = "";
tbxLastName.Text = "";
tbxStreet.Text = "";
tbxCity.Text = "";
tbxState.Text = "";
tbxZip.Text = "";
// Display list of contact names from array in the listbox
lstContacts.Items.Clear();
for (int j = 0; j < 20; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
// Delete selected contact
for (int k = 0; k < Contacts.Length; k++)
{
try
{
if (Contacts[k].Contains(lstContacts.SelectedItem.ToString()))
{
Contacts[k] = null;
}
}
catch
{
}
}
//Display Refreshed Records in ListBox
lstContacts.Items.Clear();
for (int j = 0; j < 10; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void lstContacts_SelectedIndexChanged(object sender, EventArgs e)
{
tbxFirstName.Text = lstContacts.SelectedItem.ToString();
}
}
}
Here is the changed code that gives me all the errors:
Code:
{
public partial class frmAddressBook : Form
{
public struct AddressBookEntries
{
public string firstName;
public string lastName;
public string address;
public string city;
public string state;
public string zipCode;
}
// Create Array to hold contact information
AddressBookEntries[] Contacts = new AddressBookEntries[20];
int i = 0;
public frmAddressBook()
{
InitializeComponent();
}
private void AddressBook_Load(object sender, EventArgs e)
{
}
private void btnExit_Click(object sender, EventArgs e)
{
// Close program
this.Close();
}
private void btnAdd_Click(object sender, EventArgs e)
{
// Declare variables
string lastName = tbxLastName.Text;
string firstName = tbxFirstName.Text;
string street = tbxStreet.Text;
string city = tbxCity.Text;
string state = tbxState.Text;
string zip = tbxZip.Text;
// Add contact information for New contact to array
if (i < 20)
{
Contacts[i] = lastName + firstName + street + city + state + zip;
i++;
}
else
{
MessageBox.Show("The Address Book Can Hold a Maximum of 20 Contacts.", "Address Book Full");
}
//Clear TextBoxes
tbxFirstName.Text = "";
tbxLastName.Text = "";
tbxStreet.Text = "";
tbxCity.Text = "";
tbxState.Text = "";
tbxZip.Text = "";
// Display list of contact names from array in the listbox
lstContacts.Items.Clear();
for (int j = 0; j < 20; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
// Delete selected contact
for (int k = 0; k < Contacts.Length; k++)
{
try
{
if (Contacts[k].Contains(lstContacts.SelectedItem.ToString()))
{
Contacts[k] = null;
}
}
catch
{
}
}
//Display Refreshed Records in ListBox
lstContacts.Items.Clear();
for (int j = 0; j < 10; j++)
{
if (Contacts[j] != null)
{
lstContacts.Items.Add(Contacts[j].ToString().Trim());
}
}
}
private void lstContacts_SelectedIndexChanged(object sender, EventArgs e)
{
tbxFirstName.Text = lstContacts.SelectedItem.ToString();
}
}
-
January 25th, 2010, 09:19 AM
#40
Re: Display highlighted data from Listbox in Textboxes
Could you post the errors you are getting? I assume they are compilation errors...
-
January 25th, 2010, 11:42 AM
#41
Re: Display highlighted data from Listbox in Textboxes
This looks like it would give an error:
Contacts[i] = lastName + firstName + street + city + state + zip;
You can't set the Contacts[i] to a series of strings. You need to set each property from the AddressBookEntries (Contacts[i]) to the values you want.
ie:
Code:
Contacts[i].firstName = firstName;
Contacts[i].lastName = lastName;
...
Also, btw...
You should rename some of your variables, and follow some coding standards/guidelines as well..
Public properties should be PascalCase, not camelCase.
ie:
Code:
public struct AddressBookEntries
{
public string FirstName;
public string LastNAme;
public string Address;
public string City;
public string State;
public string ZipCode;
}
Local variables should be camelCase
Code:
AddressBookEntries[] contacts = new AddressBookEntries[20];
And, you should name collection variables after the name of the Type, with an 's' at the end to show plural.
ie:
Code:
AddressBookEntry[] addressBookEntries = new AddressBookEntry[20];
// or better yet...
Contact[] contacts = new Contact[20];
Also, using int i = 0; as a class member is very bad practice, as 'i' is usually reserved as an iterator for local for loops. Use a class member with a more specific name, such as:
Code:
private int mNumContacts = 0;
Just some advice, because I hate seeing un-standardized/non-uniformed code
-
January 25th, 2010, 02:00 PM
#42
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by nelo
Could you post the errors you are getting? I assume they are compilation errors...
There are lots of errors, too many to post. I am going to start all over on this and use an array of structures, then go from there.
-
January 25th, 2010, 02:26 PM
#43
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by CarlMartin
There are lots of errors, too many to post. I am going to start all over on this and use an array of structures, then go from there.
Why don't you zip up the project and attach it here?
-
January 25th, 2010, 04:28 PM
#44
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by Arjay
Why don't you zip up the project and attach it here?
I guess I could do that.
-
January 25th, 2010, 06:06 PM
#45
Re: Display highlighted data from Listbox in Textboxes
Originally Posted by CarlMartin
There are lots of errors, too many to post. I am going to start all over on this and use an array of structures, then go from there.
Are you new to the C#/Visual Studio compiler? What tends to happen is that there are errors that occur because of other errors. If you tackle them one by one from the beginning you might be able to sort them out. The ones you're not able to resolve you can post them here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|