C# WPF Load ComboBox from txt file similar to Listbox - c#

I have a listbox that I am populating from a text file with this code..
(Works)
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
Listbox1.ItemsSource = File.ReadAllLines(#"c:\temp\servers.txt");
}
But when i try to populate a Combobox dropdown list the same way.. the list is empty.
(Doesnt work.. is blank combobox)
private void ComboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox2.ItemsSource = File.ReadAllLines(#"c:\temp\servers.txt");
}
thanks!

You're setting the value for the ComboBox in the Selection_Changed event handler. The code in this block will only fire if someone changes the selected value in the combo box - which will never happen because there isn't anything in it - hence your code isn't being run. You should put that code in the constructor for the page, or an OnNavigatedTo() method.
public YourPageName()
{
this.InitializeComponent();
ComboBox2.ItemsSource = File.ReadAllLines(#"c:\temp\servers.txt");
}

Related

C# combobox dropdown

I Want my combobox to drop down when i press the textfield and the dropdown symbol
I have done this:
private void comboBoxOpretKomponentLevel_Enter(object sender, EventArgs e)
{
if (comboBoxOpretKomponentLevel.SelectedIndex <= 0)
{
comboBoxOpretKomponentLevel.Text = null;
}
comboBoxOpretKomponentLevel.Focus();
comboBoxOpretKomponentLevel.DroppedDown = true;
}
The .Droppeddown = true makes it work if text is selected ("Select Product")
But when the dropdown symbol of dropbox is pressed - the droppeddown goes false again.
How do I make this work?
And as far as I know I cant use DropDownList because i canĀ“t have my ("Select Product") Text.
I would simply use the MouseClickevent. Since you're question is too broad I only have this piece of code that may help you.
What it does is that only if you click the ComboBox or any controller regarding that ComboBox it is going to open the dropdownlist. To close it simply click on the Form_Load event and it will idle the dropdownlist
private void comboBoxOpretKomponentLevel_MouseClick(object sender, MouseEventArgs e)
{
//This piece will dropdown the combobox once you click it.
comboBoxOpretKomponentLevel.DroppedDown = true;
comboBoxOpretKomponentLevel.Focus();
}
private void YourForm_Click (object sender, EventArgs e)
{
//This piece will simply close the dropdown from your combobox and use the selected value.
comboBoxOpretKomponentLevel.DroppedDown = false;
}
Hope it helps, otherwise simple reformulate your question so we can help you.

Multiple OnClick events in c#

I want to ask if i have multiple labels with same function Onclick but with different parameters. How i can handle them without make 30 methods.
I want to make A-Z Filter in windows forms application with C#. I have label for each character (A,B,C,D....,Z). Also i have TreeView with data from DB.
private void labelLetter1_Click(object sender, EventArgs e)
{
//this.labelLetter1.Text
// get value of the label and refresh treeview
}
I want to make this on every characters but without repeat same code.
subscribe an example event to other ones. try like this:
private void labelLetter1_Click(object sender, EventArgs e)
{
Label lbl = (Label) sender;
var text = lbl.Text;
//this.labelLetter1.Text
// get value of the label and refresh treeview
}
now set this event to other labels from Properties window.
The sender parameter is going to be the original object that triggered the event. In your case, it is going to be a Label. This means you could cast the object to a Label.
Additionally you could make a single label_click method and have all labels user that single method.
For example:
private void label_Click(object sender, EventArgs e)
{
String labelText = (sender as Label).Text;
//Your process
}

cannot find where event is being raised from

I Create a function NewLoad() and call it in butto1_click.
And i have event listBox1_SelectedIndexChanged which called itself during operation function "NewLoad"
private void button1_Click(object sender, EventArgs e)
{
NewLoad();
}
private void NewLoad()
{
String text = textBox1.Text.Trim();
textBox1.Text = text;
oleDbSelectCommand1.Parameters[0].Value = text;
dataSet11.Clear(); <<<--- call listbox1_SelectedIndexChanged
oleDbDataAdapter1.Fill(dataSet11);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dataSet21.Clear();
}
why this happens and how i can to avoid it?
My psychic debugging skills tell me that the listbox is databound to the dataset.
When you clear your dataset, the listbox is emptied, and the selection changes.
This raises the relevant event.
If you have something selected in list box 1, when you clear it, the selected index will change, thus raising selection changed event.

Do I have to remove a selectedIndexChange event from a listBox when refreshing the data in it?

I have a listbox in which a have a series of cases retrieved from a database. When i create a new case i wan't to update the listBox to reflect the actual state of the cases-table in the database. But I get a NullReferenceException from the event handler for this line: populateBoxes((int)lb.SelectedValue) when i try to update it.
This is my event-handler on the listbox:
private void lbCases_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox lb = (ListBox)sender;
populateBoxes((int)lb.SelectedValue);
}
The update event:
private void button1_Click(object sender, EventArgs e)
{
this.casesTableAdapter.Fill(this.caseDB.cases);
}
I've used the built-in feature of VSE2008 to set the datasource, displaymember and valuemember of the listbox.
You have to make sure that when the datasource is set the lbCases_SelectedIndexChanged either does not fire or ignores the event.
Either create a 'Loading' boolean to ignore the event or set the index to -1 and add a check in lbCases_SelectedIndexChanged for the -1 index value to prevent the exception.
Set the selectedindex to -1 and then populate the list box over.
I suggest, you do a check in that event handler:
private void lbCases_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox lb = sender as ListBox;
if(lb == null)
return;
populateBoxes((int)lb.SelectedValue);
}
Check the SelectedValue for null before you run the
if (lb.SelectedValue != null)
{
populateBoxes((int)lb.SelectedValue);
}
Also, if you want to end up with a selected value, you will have to select it after you call:
this.casesTableAdapter.Fill(this.caseDB.cases);

C# Binding: How can I disable the CurrencyManager in BindingList so Current Item position is not maintained and not signaled?

I've got two ListBox'es that are databound to the same BindingList.
The issue is that when changing the selected item from the GUI it's changing the position in the BindingList and then the BindingList signals the other ListBox to change its selected item.
So I've got the two ListBoxes Selected Item also synchronized which is not good for me.
I'd like to maintain the list of items in sync. without the cursor position.
How do I disable that cursor so it's not maintained?
sample code (just add two ListBoxes to the Form at design time and register the SelectedIndexChanged events and register the button click event with a button):
public partial class Form1 : Form
{
BindingList<string> list = new BindingList<string>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
list.Add("bla1");
list.Add("bla2");
list.Add("bla3");
this.listBox1.DataSource = list;
this.listBox2.DataSource = list;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
System.Diagnostics.Trace.WriteLine("ListBox1: " + listBox1.SelectedItem.ToString());
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox2.SelectedIndex != -1)
System.Diagnostics.Trace.WriteLine("ListBox2: " + listBox2.SelectedItem.ToString());
}
// Register this event to a button
private void button1_Click(object sender, EventArgs e)
{
list.Add("Test");
}
}
Thanks,
--Ran.
Add this line to Form_Load:
this.listBox1.BindingContext = new BindingContext();
Declaring listBox1 and listBox2 to be of the following type seems to result in the desired behaviour.
class MyListBox: ListBox {
protected override void OnSelectedIndexChanged (EventArgs a) {
if (DataManager != null) {
DataManager.SuspendBinding();
}
}
}
Regards,
tamberg
My solution for this issue is to use a normal List instead of the BindingList and just call (before the change) on the Form object:
this.BindingContext[Your List].SuspendBinding();
and after the change to the List
this.BindingContext[Your List].ResumeBinding();
This updates all the bounded controls.
Notice it's also noted in the MSDN link here:
"If you are bound to a data source that does not implement the IBindingList interface, such as an ArrayList, the bound control's data will not be updated when the data source is updated. For example, if you have a combo box bound to an ArrayList and data is added to the ArrayList, these new items will not appear in the combo box. However, you can force the combo box to be updated by calling the SuspendBinding and ResumeBinding methods on the instance of the BindingContext class to which the control is bound."

Categories