How to make that textbox lost focus in WPF? - c#

I have textBox.I begin write something in textbox,and it opens popup with listBox.When i select any item from listBox,textBox losts focus.How to make that listbox not to take focus,or how to get focused textbox?I use MVVM

The simplest way would be just to set focus manually to the textbox whenever it loses the focus. Mind however that this may automatically close your popup, so you'll need to tweak it as well.
By the way, maybe you just need a combobox instead? It's exactly a textbox with a drop-down list of choices.
(MVVM doesn't matter here, the problem is purely in the view.)

Related

FocusManager.FocusedElement and MVVM

I'd like to control which button is focused in my view. User must be able to perform a job without need to use mouse. And job is going through all elements. Depending on element, some buttons will appears, some disappears.
I could do it with dependency properties. To example, if there are buttons Previous and Next, then I can provide IsPreviousFocused and IsNextFocused to set focus to them after certain event. Events can be: showing window for the first time (something should have focus already), reaching 1 element (hiding Previous button, setting IsNextFocused), reaching last element (opposite), etc.
This looks reasonable more or less.
But, if I have, to example, 10 buttons (different operations), then all of them will have to have dependency property!
So I was thinking about much easier approach: when I hide button, there will be no focus
if(FocusManager.FocusedElement == null) { ... }
If I can detect, when there are no focus, then I can try to set it to one of the buttons somehow. This way, I don't really need any complicated focus management
Question is: how to deal with FocusManager.FocusedElement in MVVM scenario? How to detect when there is no focus (when window is shown first time, when certain button is clicked and become invisible, etc)?
P.S.: I actually hate ms for making another technology without thinking fully into it; focus is very basic feature (and everybody care about it in their software), but there is no direct support for it (in xaml); looks like "oh, its too complicated, lets skip it" solution.
You could control your focus from your ViewModel by using the approach shown here:
Set focus on textbox in WPF from view model (C#)

Form only saving when inputs lost focus

I'm using a form (FormView) with databinding (ObjectDataSource) and all my input fields are bound by using '<%# Bind("field") %>'.
Everything works fine, but I have two problems (which I found various hints about like using this.Validate() or .EndEdit() - but none seem to work):
Entries are only saved after leaving the input field so it looses focus
Let's say I have a textbox with an ID of Name and enter "George". When I would tab to the next textbox or when I click somewhere else and click save - everything is saved. But when I keep the focus in the textbox the value is not saved. Why is this happening? What magic can I use to circumvent this (JavaScript to the rescue?).
I set a textbox's field value (element.value) via Javascript (upon selecting something in a combobox).
The same problem as above applies, only when I give the textbox focus and tab out the value is saved. This creates the problem that I only want the user to choose something in the combobox (the textbox is updated accordingly) and move on - I don't want the user to click into the textbox afterwards and tab out again.
Edit:
The second problem I resolved now by setting the focus onto my textbox via Javascript (textbox.focus();) and right after set the focus back to the combobox (combobox.focus();) and that does the trick - this seems fairly hackish to me, doesn't it?
I'm assuming this is fairly common, but my mighty Google fu hasn't help me find a simple solution.
A similar issue can crop up in Winforms development when working with DataGridView controls. I typically attach some logic to the submit button's Click event to cause the DataGridView to validate. I suspect a similar solution would work for you here.

Combobox with checkbox in winforms

I am trying to look for a simple way to design a winform with a combobox that has checkbox values in it to select multiple values.
But there are no free samples I could find.
If anybody has a good link for a sample which does not require a license.
Please let me know.
I am not looking for controls like telerik and infragistics.
Maybe this example can help you.
CheckBox ComboBox Extending the ComboBox Class and Its Items
It sounds like what you really want is a checked listbox control or maybe even just a listbox. These controls do multi-select in a way that is more standard for Windows.
If you really need a combo box with checkboxes in it, here's an article on code project I used once.
My suggestion, if space is an issue as #rmc00 has eluded to, place a button at the end of a readonly Textbox with perhaps an elipse (...) or down arrow (same as combobox) as the text of the button and when clicked or MouseDown make visible and position a CheckBoxList or open a popup dialog with a CheckBoxList this way you can either prepopulate at design time or pass a DataTable as a parameter/property to your control/form so it is databound at runtime. You can always place your control or write code to position the control/form exactly below your TextBox in the MouseDown/Click event. On check change update your textbox with a comma separated list (or go fancy and say if more than 3 items the text box can have the list stored in the Tag and the TextBox Text can have the count of items checked). Finally on LostFocus hide the Control (or Form), and further if you want to get fancy make the exception to not hide when the ActiveControl is the Button that way you can toggle the visibility of consecutive button presses.

Getting handle of Autocomplete dropdown box of textbox in winforms

I wanted to adjust the width of the Autocomplete dropdown box of a textbox. I dont want to adjust the width of that textbox, but only Autocomplete dropdown. I know that there is no way I can increase the width of the Autocomplete dropdown by using properties provided with textbox.
Hence I wanted to know whether there is any way to get the handle of that Autocomplete box and then increase the width of that drop downlist without changing the textbox width?
If this is not possible then I would like to create my custom textbox with autocomplete, in this case how to use the existing autocomplete functionality provided by microsoft? Is there any way to do it. Are there any libraries available for this?
I don't think you can use Microsoft's implementation of autocomplete, which does not have an option to adjust dropdown width.
Create a background thread to not get into the way of typing, and hook up the text change event of a combobox box or a textbox to update the candidate list (assuming autosuggest mode since you mention a dropdown). You can probably add/remove the combobox items on the fly if you have a combobox. But for dropdown list and textbox items you need a popup window
It is easy to get a popup to show, but you need to not use a fixed position so it won't go off the screen when the textbox is close to the edge of the screen. And the focus logic is a little bit complex. you need to keep focus on the textbox unless the user press the arrow keys to make a selection.
so
when focus is on textbox:
arrow keys move the focus to the popup
other keys goes to the textbox, if not handled by the dialog itself, except for the delete key when mouse is over the popup.
when focus is on popup:
arrow keys move the focus to the sibling candidate item or the textbox
other keys goes to the textbox, if not handled by the dialog itself, except for the delete key
mouse clicks:
dismiss the popup outside of the popup or the popup.
update the value of textbox if a candidate item in the popup is clicked on
It takes a lot of effort to get the focus/threading right. If you can afford some form space, you can just add a fixed width listbox to the form instead, like Visual Studio help viewer's index pane.
After going through lot of blog posts and different articles, I came to a consensus that it is next to impossible to get a solution to my problem in the way I wanted. So I've decided either to come up with a custom solution or as Sheng Jiang said I need to implement my own autocomplete object.
I've come up with a solution which fits my requirement by increasing the width of the textbox as per the largest string in the autocomplete string list while I'm adding the autocomplete custom source. As I said I cant increase the width of the text box because of the size constraint on the form, so I decided to keep this textbox in a panel and increase the size of the textbox inside that. Panel will not grow with the textbox so that solved my problem.
I know this is not perfect solution but it fits my requirement.

How can I prevent a Checkbox state switch when the user clicks on the text?

I have a list box with Checkboxes in it. I want to prevent the Checkbox from changing its status if the user clicks on the text next to it. I only want it to change if the small box is clicked directly.
Is there any way to do this in windows forms?
Greetings and thanks in advance.
Place the text next to it in a Label, instead of the Text property of the Checkbox. Or you could create your own control which has a Checkbox and a Label. The Text property of the control would then fill the Text in the Label, and you could expose all of the Checkboxes regular properties in your control.
That's fairly non-standard behavior. Users are going to expect to be able to change the checkbox when clicking on its label, and are going to be frustrated, confused, and surprised when it doesn't work. I'd recommend not doing this. I'm not the only one.
(Yes, it's about web design, but many of the concepts are applicable in desktop application design as well.)
You could always not fill in the Text property of the Checkbox and make a completely separate Label control.
Otherwise, you will probably have to do explicit hit testing within the control to see if they hit the box or text. And then you will have to worry about checking the margins, which side the box is on, and other things that can change the position of the box.
I personally was only able to freeze things.
I freeze the check boxes by handling the Click and ItemChecked events,
and change the check state back, when it gets modified.
I use a menu to check/uncheck items and let user decide to use the menu or classic behave.
Cheers, good luck.

Categories