I have maybe an easy question but I would like to ask about possibilities how to bind textbox Text property to combobox SelectedItem property. I do it through combobox SelectedItemChanged event and set text like this:
if(cmbMeasuring.SelectedItem != null)
txtMethod.Text = ((ListBoxItem)cmbMeasuring.SelectedItem).Value;
I have class ListBoxItem which holds 2 strings "Name" and "Value". Then I created BindingList for combobox:
private BindingList<ListBoxItem> lst;
and then set combobox data source in constructor:
cmbMeasuring.DataSource = lst;
cmbMeasuring.DisplayMember = "Name";
This works fine but I dont know if its the best way how to do it. But problem occurs when I change the textbox content. I do it through textbox Leave event:
private void txtMethod_Leave(object sender, EventArgs e)
{
if (cmbMeasuring.SelectedItem != null)
((ListBoxItem)cmbMeasuring.SelectedItem).Value = txtMethod.Text;
}
If textbox lost focus I assign item value. But I have also a menustrip to save input and when I click to it directly this event dont occur so the last input is not saved. I know that this could be done through textbox TextChanged event but it consume a lot of time.
Do you have any better solutions or is it OK? Im not using WPF.
Thanks.
If you have a Click event for the MenuStrip item, you can do the following
MyMenuStripItem.Focus();
This should cause the MenuStrip item to gain focus and therefore causing the TextBox to lose focus.
Try data binding on the TextBox in your form's constructor:
txtMethod.DataBindings.Add("Text", lst, "Value",
false, DataSourceUpdateMode.OnPropertyChanged);
Related
I have editable combobox, MVVM.
I need dropdown=true when I write something in combobox.
Text="{Binding textsearch, UpdateSourceTrigger=PropertyChanged}"
here in textsearch I wrote OnPropertyChange for ComboBox IsDropDownOpen=true, but it works only onсe, when I select row, then try to write again, the dropdown=false and not react to property...
Also, when I selected row and change text, I can't select this row again, because it was selected, I need change selected item first, but when I change selected Item, the text changes too.
How to make filter works?
Or like another variant, I added textbox for filter text, but when I write text and call textsearch property, the textbox lost focus and combobox has this focus... how to save focus on textbox and dropdown combobox itemslist?
The easiest way to do something like that is to handle one or more events. You could try to handle the PreviewTextInput event:
<ComboBox ItemsSource="{Binding Items}" IsEditable="True"
PreviewTextInput="ComboBox_PreviewTextInput" />
...
private void ComboBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
ComboBox comboBox = sender as ComboBox;
comboBox.IsDropDownOpen = true;
}
The code in this event handler will open the drop down each time a user types into the ComboBox TextBox. You may need to handle a few more events to get your exact desired behaviour, but you can see which events are available from the ComboBox Events page on MSDN and complete this yourself.
I have a listbox in my winform, when an item in the listbox is selected, a value is placed into a textbox in the same Form. There my many items in my listbox which when selected, i want the text box to be empty so i can pass in the new value. How do i check is if the user has clicked on something other their initial selected item? i get the currently selected item like this below:
var selectedItem = (ReportItems)listbox.selectedItem
You can use the SelectedIndexChanged event on your ListBox . You can create an event handler for this event to determine when the selected index in the ListBox has been changed. This can be useful when you need to display information in other controls based on the current selection in the ListBox. You can use the event handler for this event to load the information in the other controls.
See MSDN documentation: link
You can add a global variable for your ReportItems and call it 'selItem'.
After the user changed the selected Item you check the "new" selectedItem with the 'selItem'-variable.. i don't think that a listbox has a method that can check if the selection has changed from the previous one..
I'm not sure if there is a reason you're not leveraging the SelectionChanged event of the ListBox or not but you should be if you're not. Further, determining if it's different than the initially selected item should be pretty straight forward because you can save the initially selected item in a private variable in your form and compare every time the method handler for SelectionChanged fires.
Other than this, there's not much more I can suggest because your questions isn't terribly clear and there is no code to look at.
My solution was to always clear the textbox first. So as soon as a user selects an item in the listview, rather than populating the textbox straight away, clear the textbox before populating it.
clearText(); is called soon as a listbox item is clicked.
public void clearText()
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();
textBox9.Clear();
textBox10.Clear();
textBox11.Clear();
textBox12.Clear();
}
I have a fixed listbox, which contains fixed items. In addition, I create several listboxes. I want to add a selected item from the fixed listbox to one of selected listbox, which is created.
How do I know which listbox is actually selected?
For each created Listbox I'm giving it a different ListBox.Name. I thought this might help me but I can't still solve this problem.
For each Listbox I'm trying to create a Radiobutton, but I dont know how to use it with ListBoxes.
You could try something like this:
public partial class Form1 : Form
{
ListBox lstSelected = null;
private void lb_Enter(object sender, EventArgs e)
{
lstSelected = (ListBox)sender;
}
}
The idea is this: for every listbox set Enter event to lb_Enter(), so you always have selected listbox in lstSelected var.
When you create a new listbox, you can use
ListBox lst = new ListBox();
lst.Enter += lb_Enter;
By checking Focused of Controls you can check a control already has focus or not
But I do'nt know what dou you mean by creating a radiobutton for each listbox?!
You need a way to select a ListBox:
Use drag and drop (the drop shows what listbox is selected)
Use a radio button or something similar to mark a listbox as the target
Use separate buttons for each listbox to be clicked to move the item to a specific listbox
There is no standard way to manage this, in fact, only one control can have focus so selecting a listbox and selecting an item at the same time will require you to make one of these constructions.
To use a radiobutton you will have to find out in code what radio button is checked and then decide what listbox belongs to this radiobutton.
If you need specific implementation details post your questions, code and issues so we can have a look.
Depends on how you want to implement the selection of the listboxes. You can store the ids on the parent when you got the focus. See Enter event.
public partial class Form1 : Form
{
private string selectedListBox;
public Form1()
{
InitializeComponent();
}
private void listBox1_Enter(object sender, EventArgs e)
{
selectedListBox = (sender as ListBox).Name;
}
}
Regards,
Bogdan
How can I have the same text being entered in a text box to some other textboxes? I mean when I am typing in a textbox, the same string should go inside other textboxes on the fly!
Which event should be used in this case?
TextChanged event.
private void currencyTextBox_TextChanged(object sender, EventArgs e)
{
textbox1.Text = currencyTextBox.Text;
textbox2.Text = currencyTextBox.Text;
textbox3.Text = currencyTextBox.Text;
}
You can use TextChanged event. This event raises whenever there is a change in content of TextBox. You will have to handle it in all the textboxes. If there is any change in one copy this change to all but it is an ugly solution.
Also take a look at how Bindings work. It will be a much cleaner solution. Bind all the textboxes to a single variable. When the value in one textbox will change the associated variable will change and hence value in all the textboxes will change
Use the TextChanged Event
I have an interesting data binding question related to combobox. Hope someone has some good suggestion.
I have a simple form, it contains a file picker and a comboxbox. Every time a file is picked, I read a list of strings from the file to a List object and I set comboBox.DataSource = listOfStrings.
In the form load event, I set comboBox.DataBindings.Add("SelectedItem", myObject, "PickedValue");
The purpose is clear: every time a string is selected from the combobox, I want to write the string to myObject.PickedValue.
That is the whole story.
Now I launch the form, rather than go pick a file, I check the combobox first. Of course, at this point, comboBox.DataSource is null, comboBox.SelectedItem is null, too. But the data binding on the comboBox is already setup (since the setting is in form load event). Now my focus cannot be moved from the combobox to anywhere else.
I think the reason is, when I try to check the combobox, it has null as SelectedItem. When I try to move the focus to somewhere else, the data binding of the combobox is triggered. Underlying, it tries to convert the selected item to string and update myObject.PickedValue with that converted string. Since you cannot convert a null to a string, the data binding validation fails, and the validation mechanism doesn't allow my focus to be moved elsewhere and I am sucked at this moment, cannot even move to pick a file.
My question is, what is the normal binding setup work-flow for my application scenario to prevent this trap? What is the correct order of setting up such a data binding so I can check my combobox before its data source is filled by something?
FYI, I tried to bind myObject.PickedValue to SelectedText property of the combobox (I noticed that SelectedText is a string and never be null, even when SelectedItem is null). But interestingly, even if I select something from the combobox, SelectedText is still empty string when data binding is triggered. What's wrong here?
Thanks for any help.
The failure is a little simpler than you describe: Your ComboBox will fail just because there is no selected item, because there's nothing to select from.
I would just disable the ComboBox if there's nothing to select from. It's pretty easy to do. Remember to hook up a PropertyChanged event in your data object; the binding source will find it automatically with reflection.
class MyData
{
public event PropertyChangedEventHandler PropertyChanged;
// ...
public HasListOfStrings { get { return ListOfStrings != null && 0 < ListOfStrings.Count; } }
private void LoadListOfStrings
{
// ... load the list of strings ...
if ( PropertyChanged) {
PropertyChanged(this, "ListOfStrings");
PropertyChanged(this, "HasListOfStrings");
}
}
}
In the designer, bind the 'Enabled' property of the 'ComboBox' to the HasListOfStrings property. You can do it in code with:
listOfStringsComboBox.Bindings.Add ("Enabled", bindingSource, "HasListOfStrings");
I also recommend you change the AutoValidate property of the container (or container's container) to EnableAllowFocusChange.
This doesn't seem right; it should be possible to set a string property to null. Possibly the focus problem lies elsewhere. Have you tried setting a breakpoint on your property setter to confirm your theory?
The SelectedText property of a combo box refers to text that has been selected in the text portion of the combobox. This only works if the dropdown style is set to combo. Basically it's the selected text of the text box portion of the combo control (the reason a combobox is called "combo" is because it is a combination of a textbox and a selection list). You would ordinarily expect this property to be empty unless the user was editing the text portion of the combo.
If you want a workaround for this problem that is consistent with a good user experience, try disabling the combo box on form load, then enabling it when a file is picked.