I try to display "FirstName" and "LastName" attributes in my ListBox component on the same row like this "SMITH Robert" but when I launch the program, I have the Id attribute. The problem is probably that the program doesn't find the attributes "LastName" and "FirstName" ...
My Customer class :
public class Customer
{
private int id;
private string lastName;
private string firstName;
public Customer(int id, string lastName, string firstName)
{
this.id = id;
this.lastName = lastName.ToUpper();
this.firstName = firstName;
}
#region Accessors
public int GetId() { return id; }
public string GetLastName() { return lastName; }
public string GetFirstName() { return firstName; }
public void SetId(int id) { this.id = id; }
public void SetLastName(string lastName) { this.lastName = lastName; }
public void SetFirstName(string firstName) { this.firstName = firstName; }
#endregion
#region Properties
public int Id { get { return id; } set { id = value; } }
public string LastName { get { return lastName; } set { lastName = value; } }
public string FirstName { get { return firstName; } set { firstName = value; } }
#endregion
}
My function which I would to use for this manipulation (customers is a List of customers load from an Access database (this part work)):
// Define where is from data.
lbxCustomers.DataSource = customers;
// Value show in listbox.
lbxCustomers.DisplayMember = "LastNameFirstName";
// Value when row is selected in listbox.
lbxCustomers.ValueMember = "Id";
You need the DisplayMember information to be an existing property in the referencing class:
public string LastNameFirstName {
get {
return lastName + ", " + firstName;
}
}
Related
I have a class and I am using it inside a LIST
List<user> listWithCustomClass = List<user>();
myClass.cs
public class user
{
public user(string fullname, string city, string state, int age, int type)
{
name = fullname;
citi = city;
estate = state;
tipe = type;
}
private string name = string.Empty;
private string citi = string.Empty;
private string estate = string.Empty;
private int tipe = 0;
public string getFullname
{
get { return name; }
set { name = value;}
}
public string getCity
{
get { return citi; }
set { citi = value;}
}
public string getState
{
get { return state; }
set { state = value;}
}
public int getType
{
get { return type; }
set { type = value;}
}
}
How can I add a custom toString() without having to override generic toString(). I would like to add something like showDate().
For example, in a combobox I would like the output of the inserted information to be:
--> Hello, your name is {name} and your age is {age}
Like this:
foreach(var item in user)
{
user.ShowData();
}
Add this in your class:
public string ShowData()
{
return "Hello, your name is " + name + " and your age is " + age.ToString();
}
but you must also define age first. Which, following your style, would be:
private int age = 0;
and then in the constructor add:
this.age = age;
EDIT
foreach(var item in listWithCustomClass)
{
item.ShowData();
}
I made a custom usercontrol which contains a label.
I have 3 string properties : firstName, lastName, fullName.
How can I set the label's text = FullName ?
public string firstName
{
get; set;
}
public string lastName
{
get; set;
}
public string fullName //this fails
{
get { return string.Format("{0} {1}", firstName, lastName); }
set { labelFullName.Text = value; }
}
Looks like Windows Form to me. In WPF you would be using labelFullName.Content property. Assuming you want to set the label as the fullname each time first name or last name changes, then one option would be to do this within your UserControl class:
private String _sFirstName = "";
private String _sLastName = "";
public String FirstName {
get { return _sFirstName; }
set { _sFirstName = value; UpdateLabel(); }
}
public String LastName {
get { return _sLastName; }
set { _sLastName = value; UpdateLabel(); }
}
public String FullName {
get { return _sFirstName + " " + _sLastName; }
}
private void UpdateLabel() {
// do within a UI thread to prevent threading issues
this.BeginInvoke((Action)(() => {
labelFullName.Text = this.FullName.Trim();
}));
}
I am new in c# (for Windows App) and I am trying to understand an example that I found in the web.
There is a class:
public class Person
{
string firstname;
string lastname;
int age;
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
public string LastName
{
get { return lastname; }
set { lastname = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
And a Code that reads data from XML and binds it to a listbox:
string peopleXMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "Assets/PeopleData.xml");
XDocument loadedData = XDocument.Load(peopleXMLPath);
var data = from query in loadedData.Descendants("person")
where (int)query.Element("age") == 27
select new Person
{
FirstName = (string)query.Element("firstname"),
LastName = (string)query.Element("lastname"),
Age = (int)query.Element("age")
};
listBox.ItemsSource = data;
But how can I get (for example) the FirstName into a string variable to use it in TextBox.text ? And how do I movenext in the dataset?
LINQ queries return IEnumerable<something>, in your case IEnumerable<Person>. You can iterate the result to get each value.
foreach (var item in data)
{
MessageBox.Show(item.FirstName);
// or in console app
//Console.WriteLine(item.FirstName);
}
Ok, so my application is a simple baby gift registry. In the left groupBox, I have 2 input text boxes(item, store) and a listBox(lstWish) for output. This side is the Wish List. In the right groupBox, I have 2 input text boxes(firstName, lastName) and a listBox(lstPurchased) for output. Upon a button_click, the selected item from lstWish is moved to lstPurchased. lstWish currently outputs like "Item available at Store".
When the item goes from lstWish to lstPurchased I want to add "from firstName lastName". So the lstPurchased will read "Item available at Store from firstName lastName". How do I add the second half of the string while moving it over?
Here is the method that moves the item over:
private void MoveListBoxItems(ListBox lstWish, ListBox lstPurchased)
{
ListBox.SelectedObjectCollection sourceItems = lstWish.SelectedItems;
foreach (var item in sourceItems)
{
lstPurchased.Items.Add(item);
}
while (lstWish.SelectedItems.Count > 0)
{
lstWish.Items.Remove(lstWish.SelectedItems[0]);
}
}
And here is the code for the button click:
private void btnBuy_Click(object sender, EventArgs e)
{
if (txtFirst.Text == "" || txtLast.Text == "")
{
MessageBox.Show("Please enter first and last name.", "Warning");
this.DialogResult = DialogResult.None;
}
else
{
DialogResult result = MessageBox.Show("Click Yes to confirm purchase. Click No to cancel."
, "Thank You", MessageBoxButtons.YesNo);
if (lstWish.SelectedIndex >=0)
{
MoveListBoxItems(lstWish, lstPurchased);
}
else
{
return;
}
}
}
The list is a collection is a List. There are two custom classes, Gift and Guest. Here is the class Gift that has the toString that populates the listBox:
namespace GiftRegistry
{
class Gift
{
#region Fields
private string _item;
private string _store;
//private string _purchaser;
#endregion
#region Properties
public string Item { get; set; }//modify the set clauses to include regex and title case and .trim()
public string Store { get; set; }
//public string Purchaser { get; set; }
#endregion
#region Constructors
public Gift()
{
this.Item = String.Empty;
this.Store = String.Empty;
}
public Gift(string Item, string Store)
{
this.Item = Item;
this.Store = Store;
}
#endregion
#region Method
public override string ToString()
{
return string.Format("{0} availble at {1}", this.Item, this.Store);
}
#endregion
}
}
Guest class:
class Guest
{
#region Fields
private string _lastName;
private string _firstName;
#endregion
#region Properties
public string LastName
{
get { return _lastName; }
set
{
if (value == null)
throw new ArgumentNullException("Name", "Please enter a name");
_lastName = value.Trim();
}
}
public string FirstName
{
get { return _firstName; }
set
{//see above about regular expressions
if (value == null)
throw new ArgumentNullException("Name", "Please enter a name");
_firstName = value.Trim();
}
}
#endregion
#region Constructors
public Guest()
{
this.LastName = String.Empty;
this.FirstName = String.Empty;
}
public Guest(string lastName)
{
this.LastName = lastName;
this.FirstName = String.Empty;
}
public Guest(string lastName, string firstName)
{
this.LastName = lastName;
this.FirstName = firstName;
}
#endregion
#region Method
public override string ToString()
{
return string.Format("{0} {1}", this.FirstName, this.LastName);
}
#endregion
}
In MoveListBoxItems(), you can concatenate the firstName and lastName with:
lstPurchased.Items.Add(string.Format("{0} from {1} {2}", item.ToString(), firstName, lastName));
I'm trying to move items to a listbox, but it's calling my listbox a method group. The program has multiple classes. Here's what I have at the moment...
private void Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Add("Full Name\tAddress\tPhone Number");
Person Customer = new Person();
Customer.FirstName = NameTextBox.Text;
Customer.LastName = NameTextBox2.Text;
Customer.Address = AddressTextBox.Text + AddressTextBox2.Text + AddressTextBox3.Text;
Customer.PhoneNumber = PhoneNumberMaskedTextBox.Text;
Listbox1.Items.Add = (Customer.FirstName + Customer.LastName + "/t" + Customer.Address + "/t" + Customer.PhoneNumber);
}
Person is another class. Here's the code for that...
class Person
{
string _FirstName;
string _LastName;
string _Address;
string _PhoneNumber;
//Constructor
public void CustomerInfo()
{
_FirstName = "";
_LastName = "";
_Address = "";
_PhoneNumber = "";
}
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
public string Address
{
get { return _Address; }
set { _Address = value; }
}
public string PhoneNumber
{
get { return _PhoneNumber; }
set { _PhoneNumber = value; }
}
}
In yor second Add call probably you wanted to have
Listbox1.Items.Add(...)
instead of
Listbox1.Items.Add = (
Something else incorrect on your code is constructor name for Person. It should be named Person since contructors have same name as the class.