I created a Windows Form that display some info in a listbox. First of all I created the UserInfo class with 5 properties:
public class RegUnregUser
{
public string Company { get; set; }
public string Email { get; set; }
public string Role { get; set; }
public object ID { get; set; }
public string RegistrationStatus { get; set; }
and a FullInfo that return those properties one under the other:
public string FullInfo
{
get
{
return
$"Company: {Company}"
"\r\n"
$"Email: {Email}"
Environment.NewLine
$"Role: {Role} {Environment.NewLine} ID: {Id} {Environment.NewLine}"
$"RegistrationStatus: {RegistrationStatus}";
}
}
As you can see I tried all the possible combinations but the result is always:
Company: companytest1Email: emailtest1 ..
What can I do to display those info like this:
Company: companytest1
Email: emailtest1
..
The code in the Form1. There is a textbox which users search for an email and when they click on Search button, there is a query on the DB that searches for this email in a table and return the result. I know this is an easy sql injection but I'll be the only who use this code
public partial class Dashboard : Form
{
List<RegUnregUser> regUnregUsers = new List<RegUnregUser>();
public Dashboard()
{
InitializeComponent();
RegUnregUsersListBox.DataSource = regUnregUsers;
RegUnregUsersListBox.DisplayMember = "FullInfo";
}
private void RegUnregSearchButton_Click(object sender, EventArgs e)
{
DataAccess db = new DataAccess();
regUnregUsers = db.GetRegUnregUsers(EmailTextBox.Text);
RegUnregUsersListBox.DataSource = regUnregUsers;
RegUnregUsersListBox.DisplayMember = "FullInfo";
}
}
Thanks in advance for the help
CodePudding user response:
What can I do to display those info like this
Simple answer? You can't*
Listboxes aren't designed to have multiple lines of text per item, and it looks like you're expecting your query to return multiple users, so the suggestions in the suggested duplicate are (in my opinion) fairly terrible, because they essentially create multiple items in the listbox, one per line of text.. If you have 50 users returned, and 5 properties, it's a 250 item listbox.. Very primitive :/
Consider using a DataGridView instead:
public partial class Dashboard : Form
{
public Dashboard()
{
InitializeComponent();
}
private void RegUnregSearchButton_Click(object sender, EventArgs e)
{
DataAccess db = new DataAccess();
RegUnregUsersListBox.DataSource = db.GetRegUnregUsers(EmailTextBox.Text);
}
}
You will, of course, need to come to terms with your RegUnregUser's properties spreading out one per column, but I promise, it's better that way :)
* can't is, of course, incredibly unlikely to be an absolute truth.. I'm sure if you want to pour hours into writing a something where you paint the items yourself, you'll manage it