Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to scroll to selected item in listBox?
    primarykey
    data
    text
    <p>I am adding a Customer using stored procedures. I somehow managed to select newly added customer after insertion, but I cannot focus on the record in listbox..</p> <p>I tried everything, like :</p> <pre><code>lbxCustomers.ScrollIntoView(this.lbxCustomers.SelectedIndex); </code></pre> <p>and many modifications using Items and stuff, but nothing is working. It's still not scrolling the view to selected item.. Any ideas?</p> <p>It's WPF.</p> <p>My init looks like this :</p> <pre><code> private void init() { SqlConnection connection = null; SqlDataReader reader = null; try { connection = new SqlConnection(this.strConnection); connection.Open(); SqlCommand command = new SqlCommand("SELECT CustomerID, CompanyName, ContactName FROM Customers", connection); SqlDataAdapter sda = new SqlDataAdapter(command); reader = command.ExecuteReader(); ListItem listItem; while (reader.Read()) { listItem = new ListItem(reader.GetValue(0).ToString(), reader.GetValue(1).ToString(), reader.GetValue(2).ToString()); this.lbxCustomers.Items.Add(listItem); } } catch (Exception e) { MessageBox.Show(e.Message, "Error", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation); } finally { connection.Close(); reader.Close(); } } </code></pre> <p>init2 is the same except the line :</p> <pre><code>SqlCommand command = new SqlCommand("SELECT CustomerID, CompanyName, ContactName FROM Customers WHERE CompanyName = '" + correctID + "'", connection); </code></pre> <p>It reffers to ListItem which looks like this :</p> <pre><code> class ListItem { private string customerID, companyName, contactName; public ListItem(string customerID, string companyName, string contactName) { this.customerID = customerID.Replace("'", "''"); this.companyName = companyName.Replace("'", "''"); this.contactName = contactName.Replace("'", "''"); } public override string ToString() { return this.companyName + " (" + this.contactName + ")"; } public string CustomerID { get { return this.customerID; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload