C
C#8mo ago
RdZoid

listbox SelectedIndexChanged on load?

Hello, I'm newer to C# and I'm making a windows form where a listbox lists customers and populates txtboxes to the right if the index changes, er is supposed to because it does it on load. Where would this be caused to fill in even when the form loads and the index hasn't changed?
No description
No description
26 Replies
RdZoid
RdZoidOP8mo ago
I've verified this is the only space those get populated because if I comment that section out nothing loads there
SpReeD
SpReeD8mo ago
In unselected state the index is -1, 0 is the first item selected, since this is zero-based-index. Is there an item selected? -> Yes -> Your condition will result in true
RdZoid
RdZoidOP8mo ago
how can I have it not start selected/ at 0?
SpReeD
SpReeD8mo ago
It depends on how you fill the ListBox items
RdZoid
RdZoidOP8mo ago
it's being filled from an XML file
SpReeD
SpReeD8mo ago
No, I mean is it filled by setting the DataSource or manipulating the Items list
RdZoid
RdZoidOP8mo ago
data source
RdZoid
RdZoidOP8mo ago
if I comment it out here they disappear
No description
RdZoid
RdZoidOP8mo ago
this may clarify
No description
SpReeD
SpReeD8mo ago
Setting the DataSource on a ListBox will make an automatic selection on the first item 0. This is somewhat a ListBox bug, you could workaround by manipulating the Items list or workaround bool when changing the DataSource.
RdZoid
RdZoidOP8mo ago
That's weird, doesn't make it seem very indexchanged-ey, can you think of an easy way to make sure the txtboxes on the right side stay blank unless something's actually selected?
SpReeD
SpReeD8mo ago
Yes, as written before.
RdZoid
RdZoidOP8mo ago
it might be wacky but could I just "" the textboxes after the refresh() is called in form load?
SpReeD
SpReeD8mo ago
private bool dataSourceChange;

private void Main_Load(object sender, EventArgs e)
{
this.dataSourceChange = true;
this.listBox1.DataSource = new string[] { "foo", "bar" };
this.dataSourceChange = false;
}

private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.dataSourceChange)
{
this.listBox1.SelectedIndex = -1;
return;
}

//more of your code here
}
private bool dataSourceChange;

private void Main_Load(object sender, EventArgs e)
{
this.dataSourceChange = true;
this.listBox1.DataSource = new string[] { "foo", "bar" };
this.dataSourceChange = false;
}

private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.dataSourceChange)
{
this.listBox1.SelectedIndex = -1;
return;
}

//more of your code here
}
Or just do AddRange(customers), since it seems it's an Array/List.
RdZoid
RdZoidOP8mo ago
hmmm the bool thing didn't seem to work
SpReeD
SpReeD8mo ago
It does, try stepping through the code.
RdZoid
RdZoidOP8mo ago
I might just have inserted it incorrectly, how do you add code here?
SpReeD
SpReeD8mo ago
$codegif
RdZoid
RdZoidOP8mo ago
public partial class frmBanking : Form
{
List<Customer> customers;
MySettings settings;
private bool dataSourceChange;

public frmBanking()
{
InitializeComponent();

}

private void frmBanking_Load(object sender, EventArgs e)
{
this.dataSourceChange = true;
this.lbxCustomers.DataSource = new string[] { "foo", "bar" };
this.dataSourceChange = false;

try
{


settings = Program.Configuration.GetSection("MySettings").Get<MySettings>();

this.Text = settings.Text;


lblStatus.ForeColor = Color.Black;


//customers = CustomerManager.Populate();
customers = CustomerManager.ReadXML(settings.CustomerXMLFilename);


Refresh();



}
catch (Exception ex)
{
lblStatus.ForeColor = Color.Red;
lblStatus.Text = ex.Message;
}
}

private void Refresh()
{
lbxCustomers.DataSource = null;
lbxCustomers.DataSource = customers;
lbxCustomers.DisplayMember = "FullName";
lbxCustomers.ValueMember = "Id";
lblStatus.Text = $"{lbxCustomers.Items.Count} Customers loaded";
lblStatus.ForeColor = Color.Blue;
}



private void lbxCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.dataSourceChange)
{
this.lbxCustomers.SelectedIndex = -1;
return;
}

try
{
lblStatus.ForeColor = Color.Black;
lblStatus.Text = string.Empty;


if (lbxCustomers.SelectedIndex >= 0)
{
Customer customer = customers[lbxCustomers.SelectedIndex];

txtFirstName.Text = customer.FirstName;
txtLastName.Text = customer.LastName;
txtSSN.Text = customer.SSN;
txtId.Text = customer.Id.ToString();
dtBirthDate.Text = customer.Birthdate.ToString();


dgvDeposits.DataSource = null;
dgvWithdrawals.DataSource = null;


}

}
catch (Exception ex)
{
lblStatus.ForeColor = Color.Red;
lblStatus.Text = ex.Message;
}
}
public partial class frmBanking : Form
{
List<Customer> customers;
MySettings settings;
private bool dataSourceChange;

public frmBanking()
{
InitializeComponent();

}

private void frmBanking_Load(object sender, EventArgs e)
{
this.dataSourceChange = true;
this.lbxCustomers.DataSource = new string[] { "foo", "bar" };
this.dataSourceChange = false;

try
{


settings = Program.Configuration.GetSection("MySettings").Get<MySettings>();

this.Text = settings.Text;


lblStatus.ForeColor = Color.Black;


//customers = CustomerManager.Populate();
customers = CustomerManager.ReadXML(settings.CustomerXMLFilename);


Refresh();



}
catch (Exception ex)
{
lblStatus.ForeColor = Color.Red;
lblStatus.Text = ex.Message;
}
}

private void Refresh()
{
lbxCustomers.DataSource = null;
lbxCustomers.DataSource = customers;
lbxCustomers.DisplayMember = "FullName";
lbxCustomers.ValueMember = "Id";
lblStatus.Text = $"{lbxCustomers.Items.Count} Customers loaded";
lblStatus.ForeColor = Color.Blue;
}



private void lbxCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.dataSourceChange)
{
this.lbxCustomers.SelectedIndex = -1;
return;
}

try
{
lblStatus.ForeColor = Color.Black;
lblStatus.Text = string.Empty;


if (lbxCustomers.SelectedIndex >= 0)
{
Customer customer = customers[lbxCustomers.SelectedIndex];

txtFirstName.Text = customer.FirstName;
txtLastName.Text = customer.LastName;
txtSSN.Text = customer.SSN;
txtId.Text = customer.Id.ToString();
dtBirthDate.Text = customer.Birthdate.ToString();


dgvDeposits.DataSource = null;
dgvWithdrawals.DataSource = null;


}

}
catch (Exception ex)
{
lblStatus.ForeColor = Color.Red;
lblStatus.Text = ex.Message;
}
}
SpReeD
SpReeD8mo ago
Your Refresh method lacks the bool set to true.
RdZoid
RdZoidOP8mo ago
private void Refresh()
{
lbxCustomers.DataSource = null;
lbxCustomers.DataSource = customers;
lbxCustomers.DisplayMember = "FullName";
lbxCustomers.ValueMember = "Id";
lblStatus.Text = $"{lbxCustomers.Items.Count} Customers loaded";
lblStatus.ForeColor = Color.Blue;
dataSourceChange = true;
}
private void Refresh()
{
lbxCustomers.DataSource = null;
lbxCustomers.DataSource = customers;
lbxCustomers.DisplayMember = "FullName";
lbxCustomers.ValueMember = "Id";
lblStatus.Text = $"{lbxCustomers.Items.Count} Customers loaded";
lblStatus.ForeColor = Color.Blue;
dataSourceChange = true;
}
like that^?
SpReeD
SpReeD8mo ago
private void Refresh()
{
dataSourceChange = true;
lbxCustomers.DataSource = null;
lbxCustomers.DataSource = customers;
lbxCustomers.DisplayMember = "FullName";
lbxCustomers.ValueMember = "Id";
lblStatus.Text = $"{lbxCustomers.Items.Count} Customers loaded";
lblStatus.ForeColor = Color.Blue;
dataSourceChange = false;
}
private void Refresh()
{
dataSourceChange = true;
lbxCustomers.DataSource = null;
lbxCustomers.DataSource = customers;
lbxCustomers.DisplayMember = "FullName";
lbxCustomers.ValueMember = "Id";
lblStatus.Text = $"{lbxCustomers.Items.Count} Customers loaded";
lblStatus.ForeColor = Color.Blue;
dataSourceChange = false;
}
RdZoid
RdZoidOP8mo ago
holy shit that worked
SpReeD
SpReeD8mo ago
You can delete the thingy in the Load method, this was for demo purpose. It's only important to set the bool to true before changing the DataSource. So the Event won't do it's logic.
RdZoid
RdZoidOP8mo ago
ok nice, weird workaround but a workaround nonetheless thank you for your help!
Want results from more Discord servers?
Add your server