Problem with TextBox Focus in WPF - c#

I would like to set the focus on a TextBox in my custom document content in WPF. I set all those focusable parameters to true. Still, the focus is not on the TextBox. Any thought or comment ?
So far, I've added these:
textbox.Focus();
textbox.SelectAll();
to the constructor of my WPF page.
The TextBox is in a Canvas inside a DockPanel, and all of them are part of a custom:DocumentContent.
thank you in advance,

Take a look at this blog post and the MSDN Focus Overview article. From your question it sounds like you're trying to set focus in the constructor. The UI Elements have not been created at that point. You should set focus during the Loaded event of your control.

That should work. Check if textbox.Focus() is returning true, it will tell you if call did work. Also, try calling textbox.Focus() from Loaded event of Window/Page.

Related

KeyPress at form level with controls

I am wondering if it is possible to handles KeyPress event at form level when there are controls in a form.
I can achieve this when there is no control on the form, but when i add something, like a button, the form loses the focus and i can't give it back, even with Me.Focus. The focus stays on the button.
Is there a way to do it ? If not, i would like to know why. Looks interesting.
Just set true to key preview property of your form it will work.
You can do it by enabling KeyPrivew Property of your form.

How to disable focus on textbox on load in a view in WinRT?

I have a TextBox in WinRT which got the focus (Pointer focus) on load in a view in WinRT. I want to disable this and change the focus to unfocused at load. When I am in constructor of the view, the TextBox is Unfocused, but when I reach the event Loaded, the TextBox got, I don't know why, automatically, the focus at Pointer.
No instruction are done to put the focus on this control. I don't understand why it got the focus.
I try to change TabIndex, no success, the control got the focus again. When I try to put manually the focus on unfocused, I have an exception: "Value does not fall within the expected range.". I don't understand why I have this exception. I have only one control with the name that I gave to him.
Thank's for reply.
Solution 1:
You can set the textbox TabStop property to false.
Solution 2:
Set another controls TabIndex to a lower value than the TabIndex value of the texbox (the control you wish to recieve focus on startup).

Know which textBox has focus in xaml?

I have 3 textboxes that will change while it's being typed on a 4th one. I have tried the TextChanged="function" and change the content of the other three, but by chaging the .Text property, the event TextChanged is triggered again providing an undesired result. I was thinking of checking which textbox has the focus at the time, but I have no idea on how to implement that. I am experienced with Java and I'm very frustrated with all this c# and XAML. Thank you in advance.
If you search for FocusManager then you will find two kind of FocusManager class. But you have to go for
Windows.UI.Xaml.Input.FocusManager
not
System.Windows.Input.FocusManager.
It has static method GetFocusedElement() which queries the focus system to determine which object in the UI has focus.
There is a FocusManager-class, which might help you. Use the textbox' parent as scope-element.

TextBox TextChanged event does not fire when Visible = False?

I have a textbox bound to a datasource. The textbox's TextChanged event updates another textbox.
The problem is, I do not want the first textbox to show, so I set its Visible property to false.
However, now the TextChanged event does not fire!
I can work around it by setting Visible=True, Left=-100000 on form load, but I'd like a proper solution.
Can anyone offer an explanation?
Set your textbox.Visible = false in the FormLoad event instead of in the designer. It has to do with handle creation. If the textbox is not visible during construction, then the handle is not created. If the textbox is made invisible after construction, then the handle will have been created and events will occur.
See this discussion on MSDN.
An alternative solution to the accepted answer is to set up the TextChanged listener on Loaded, this works for me just the same (in Silverlight at least) and keeps the designer view as it should be.
What type of datasource is it? It might have an event that you can use directly instead of using a textbox to listen for an update.
If Visible is equal to false then the Control is not rendered. Therefore it will be unable to fire an event.
Instead, set the style to display:none. You can set/unset this programmatically using the Attributes collection:
MyTextBox.Attributes.Add("style", "display: none");

How do I move a tooltip?

I'm using the CellToolTipTextNeeded event of a DataGridView, and the tooltip is being shown under the mouse. I can get the ToolTip object out via reflection, but I don't have any control over where it's positioned since I'm not the one calling Show(). How do I move a tooltip?
How about a custom one?
I think the short answer is: you can't, not yet anyway. I would probably think about building a custom one like 'unknown (yahoo)' suggested.
You'll have to control the tooltip yourself using the Show and Hide methods if you want any control over how and where it is positioned using the 2.0 framework unless you want to get into Window's API calls.
Unfortunately, it looks like the tooltip position is set very, very early on in its creation. The tooltip only has 3 events to handle, and we aren't very interested in Dispose in this case.
The Draw event is fired after the Popup event and it seems as if the position has already been set as the Bounds property in the PopupEventArgs is readonly.
Here's a link to one of many articles (at the time of this post) on creating a custom control:
http://andrusdevelopment.blogspot.com/2007/10/implementing-custom-tooltip-in-c.html
And the MSDN documentation on using the tooltip's Show and Hide methods is actually fairly decent:
http://msdn.microsoft.com/en-us/library/44atyhsa(VS.80,printer).aspx
Sorry, but it just doesn't look like there is a quick and easy answer to this one. Hopefully someone will prove me wrong for both are sakes.
Scott
There is a good answer at this link that uses a Tooltip object.
How to override default tooltip behavior of a C# datagridview to increase the tooltip delay for particular cells

Categories