Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Silverlight 2.0 Textbox focus issue
47 replies. Latest Post by GearWorld on September 17, 2009.
(2)
HiHoSilver
Member
16 points
12 Posts
03-27-2008 2:12 PM |
Using silverlight 2.0, I created a simple logon xaml on a grid control with two textboxes, userid and password with two buttons. In the vb code, I set the focus to the userid textbox. When I execute, I can not type anything in the user textbox (it does show the focus with the cursor blinking) until I either click inside the user textbox or the control itself first. It does not behave this way with a Windows Forms or ASP .Net application. Is this an issue with the silverlight textbox control or is there something else I should be doing?
daveloper
37 points
13 Posts
03-27-2008 2:20 PM |
Could you please share which eventhandler method you've put the code to enable the focus?
03-27-2008 2:27 PM |
I set the focus to the textbox in the 'loaded' handler, the very last thing that gets executed before displaying the control.
Private Sub Logon_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded Try ClearForm() txtStatus.Text = "Please Logon..." Me.txtUserName.Focus() Catch ex As Exception End Try End Sub
03-27-2008 2:43 PM |
Thanks for clearing up.
I just whipped up a SL2 application aswell, and I tried the same thing you did.
And I couldn't type in aswell (did it in C#, but no matter).
Seems like this is a bug, since its so easy to reproduce.
I'll take a look for a work around, I'll post again if I've found anything.
-Daveloper
03-27-2008 2:45 PM |
Thanks for taking the time to look into it....
03-27-2008 3:06 PM |
I think I figured out a workaround.
I noticed that you can just click anywhere on the Silverlight control to enable typing, and it seems that when the page has loaded, there isn't an initial focus on the object element which is hosting the Silverlight control.
So!
I wrote a small Javascript function which does the trick. (use the .aspx pages because it automatically goes around the Eolas trouble)
<script type="Text/javascript">window.onload = function (){ // Note: the paremeter has to match the ID of the asp:Silverlight control or object element document.getElementById('Xaml1').focus();}</script>
Paste this in the head of the page, and it'll put the focus on your Silverlight content, and in turn use the focus you specified on the textbox!
I hope this helps, if it did work, please mark this reply as helpful.If it didn't work, let me know as soon as you can.
03-27-2008 3:28 PM |
I placed this javascript in the *.aspx page within the Head of the page just as it is, but replacing the 'Xaml1' with the name of the Logon with the name of my Logon xaml and it still does not work. Did you try this out on your application?
03-27-2008 3:44 PM |
The parameter of the document.getElementById method has to correspond to the name of the Silverlight control, thats defined inside the page, heres my working source.
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="height:100%;"><head runat="server"> <title>Test Page For TextFocusTest</title> <script type="text/javascript"> window.onload = function() { document.getElementById('Xaml1').focus(); } </script></head><body style="height:100%;margin:0;"> <form id="form1" runat="server" style="height:100%;"> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <div style="height:100%;"> <asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/TextFocusTest.xap" Version="2.0" Width="100%" Height="100%" /> </div> </form></body></html>
Note the bold, as you can see there are no references being made to any identifiers inside your .xaml code, just the asp.net Silverlight control.I think you don't even have to change the javascript call, if you're using the same standard test website project that the Silverlight solution generates for you.
Let me know how it works out for you.
03-27-2008 4:06 PM |
You are right. My mistake. The HTML stated that the name of the control was 'Xaml1'. I changed the ID to 'Xaml1' and it worked just fine. Thank you!
03-27-2008 4:12 PM |
Great!
Do remember that this probably won't change during the beta, unless the asp:silverlight control is updated to automatically enable focus on the object tag.
zwrm1
9 points
9 Posts
06-17-2008 1:19 PM |
I can't seem to make this work in 2 beta 2.
I start a new silverlight application choosing web application as the hosting option.
I add a new textbox to the page.xaml:
<
</
I then add the onload event to the aspx text page:
<%
{
}
I then add this to the code-behind page:
InitializeComponent();
test.Focus();
After i run the application and it loads, the textbox does not appear to have focus, that is, the cursor is not visible inside the textbox nor does the appearance of the textbox change as it normally does, yet, when i type text appears in the textbox.
Thoughts?
06-17-2008 1:22 PM |
i just noticed something else... if i run the application, and do not interact with IE at all but take the focus away from IE via the task bar and then give it back via the taskbar, the textbox suddenly appears normally (with a focus border and blinking cursor)
06-17-2008 3:19 PM |
i had a friend test this in beta 1 and it seemed to work fine, he then upgraded to beta 2 and the issue presented itslef, so i think it's a beta 2 issue. any thoughts on how to work around it would be appreciated :)
sridhark...
2 points
1 Posts
06-19-2008 8:37 AM |
I'm also facing the same problem on textbox. When page loads first time, its focusing on textbox. If i refresh the browser, the cursor focus is lost. But i can enter the text on textbox control. Can any one give solution for this probelm.
Montago
267 points
191 Posts
07-07-2008 8:33 AM |
I have the similar problem - and have reported it as a bug...
The bug is only present in Exploder 7.0... while Firefox does it all right
Ciaran M...
157 points
71 Posts
07-14-2008 6:18 AM |
Yes bug is absent in Firefox and visible in IE7 & 8. However I've notice that in IE it only happens for me after the first load. When I first load my app all is fine. If I hit F5 then the cursor is invisible and there is no focus indicator. You can type into a textbox still... but its disconcerting to type in blind!! Please fix guys!
Raguvind
6 points
15 Posts
07-22-2008 10:17 AM |
Hi,
It is not only the issue with the focus. but because of this issue the data is also not binded.
Could any one suggest any work arround
manski
4 points
2 Posts
08-01-2008 12:29 PM |
The following code works for me. It sets the focus to silverlight controls and also makes textboxes to be displayed normally.
Add the following JavaScript code to your HTML oder ASPX page:
<script type="Text/javascript">// We have to set the focus to the Silverlight control to use the focus system// programmatically within the Silverlight app as the control doesn't gain the// focus automatically.function OnLoaded(sender, args) { // Note: the paremeter has to match the ID of the asp:Silverlight control or object element // Note 2: We can't directly call "focus()" here. We have to use a small timeout instead. setTimeout("document.getElementById('TheGame').focus();", 100);}</script>
Then add the following attribute to your <asp:Silverlight> tag:
OnPluginLoaded="OnLoaded"
(i.e. <asp:Silverlight ID="TheGame" ... OnPluginLoaded="OnLoaded" />)
Note also that you may adjust the ID of the Silverlight tag.
sladapter
All-Star
17441 points
3,172 Posts
08-01-2008 1:57 PM |
This would work:
void Page_Loaded(object sender, RoutedEventArgs e) {HtmlPage.Plugin.Focus(); theTextBox.Focus(); }
HtmlPage.Plugin.Focus();
theTextBox.Focus();
08-01-2008 2:15 PM |
sladapter:This would work: void Page_Loaded(object sender, RoutedEventArgs e) {HtmlPage.Plugin.Focus(); theTextBox.Focus(); }
Unfortunately this code doesn't solve the problem that the textbox isn't properly displayed. It has the focus but doesn't display the cursor nor the "focused" frame. (see the first post from zwrm1)
GearWorld
Participant
846 points
1,105 Posts
08-11-2008 8:20 PM |
I have a form whcih I present to the user when he presses a button.
This form has a user name and password and I try to focus the username textbox but it doesn't focus it at allAlso, if I try to click on it, the cursor doesn't appear and if I click on the password textbox and then on the uisername textbox the cursor reappear
However, if I do not use username.focus(), the cursor problem goes away but I'm still unable to fucus on the username textbox.
UserName TabIndex = 1Password Tabindex = 2
So I think there's no way to focus a control at this point and I presume its some kind of bug. I did try the Htmlpage.plugin idea. No success. I did try the control.Focus then the username.focus same problem of cursor and no focus on the control
so I'm out of ideas :)
08-11-2008 8:27 PM |
Oh, I found a trick here.
Password.Focus();txtUsername.Focus();
Seems that if I focus on the second control first then to the first control, I can get my control focused :)
prithwis...
8 points
4 Posts
08-25-2008 11:09 AM |
I am getting focus on the textbox but when i refresh the page the textbox is getting focus but the cursor is not visible. I have tested this in IE7, Firefox and Safari. I have tried all the tricks given in this thread.
Please help me out....
08-26-2008 3:57 AM |
The idea of focusing second cntrol first and then the first control does not work in my case..
I am totally puzzled please help me....
09-05-2008 2:02 AM |
Add the following Javascript method in the ASPX page that hosts the XAP file
function setFocus(){ window.focus();}
And add the method in the onload of the body
<body style="margin: 0px; padding: 0px" onload="setFocus();">
Hope it solves the issue
hwsoderlund
411 points
118 Posts
10-01-2008 4:27 AM |
Could somebody who has upgraded to RC0 please let us know whether this bug has been fixed (I'm talking about the issue where a textbox accepts input but does not appear to have focus, and no cursor is visible). Thank you.
10-01-2008 6:29 AM |
I'm with RC0. Everything is fine.
In the VERY LAST EVENT if any animation you have or in the Loaded event and you should be in business.I didn't see any invisible cursor to date and focusing on my control works fine.
Good luck
10-01-2008 7:43 AM |
That's a relief. Thank you, GearWorld.
sunilurs
199 points
35 Posts
12-14-2008 7:11 AM |
I think I found a workaround
Place the following code inside the <head> element
<script type="text/javascript"> var OnLoaded = function() { window.focus(); document.getElementById('Xaml1').focus(); } </script>
And call OnLoaded function once the plugin loads.
<asp:Silverlight ID="Xaml1" OnPluginLoaded="OnLoaded" runat="server" .......
ccoombs
Contributor
5168 points
758 Posts
01-08-2009 4:01 PM |
you should only have to put
window.focus();
inside your script tag, and not inside a function. it'll run once, you don't need to wire the event.
mepfuso
683 points
153 Posts
04-03-2009 11:36 AM |
Instead of using javascript, I think it's easier to do that in C# in the silverlight plugin itself:
System.Windows.Browser.HtmlPage.Plugin.Invoke("focus");
Of course the HTML access must be enabled.
Thomas D
46 points
66 Posts
05-04-2009 5:22 AM |
I too have problems with focusing a TextBox. I've got a custom control, in which I want to set the focus to the TextBox with the following code:
HtmlPage.Plugin.Focus();listboxDialog.Focus();((listboxDialog.Content as Grid).Children[1] as TextBox).Focus();
05-04-2009 5:35 AM |
Servus nach Österreich aus Bayern :-) ... do you try to do that in the constructor, i.e. directly after InitializeComponent()? Maybe focus only works reliably in the Loaded event.
05-04-2009 5:43 AM |
mepfuso:Servus nach Österreich aus Bayern :-) ... do you try to do that in the constructor, i.e. directly after InitializeComponent()? Maybe focus only works reliably in the Loaded event.
Grüße zurück ... I've tried to put HtmlPage.Plugin.Focus(); just after InitializeComponent(), but this didn't helped either. I can't set focus to the TextBox directly after loading as it is created at runtime and it's visibility changes multiple times during interaction with the program (focus should be applied, when the component's visibility is set to visible). So that didn't solve the issue unfortunately ...
05-04-2009 7:48 AM |
Try this, see if it works.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Browser;
namespace SilverlightApplication20
public partial class Page4 : UserControl
public Page4()
Dispatcher.BeginInvoke(DoFocus);
private void DoFocus()
HtmlPage.Plugin.Invoke("focus");
MyTextBox.Focus();
slavag
07-30-2009 6:19 PM |
Try to change TabIndex:
PasswordTextBox.TabIndex = 0;
UserNameTextBox.TabIndex = 1;
PasswordTextBox.Focus();
08-23-2009 7:55 AM |
This problem is hard to pin point. Sometimes it works sometimes not. Here's a scenario where I'm unable to focus on the TextBox
VisualStateManager
Seems that when there's a VisualState in progress at the same time we're trying to focus, it doesn't work.I've been looking for an event to tell me when the visual state is completed but I didn't find anySeems that focusing on a control works only when Everything else is completed. VERY COMPLETED otherwiseFocusing is useless.
Misinfor...
162 points
111 Posts
08-31-2009 1:39 PM |
I combined what I read above into the following:
I put the first line below in my constructor. The purpose of the multiple Dispatcher calls is to delay the focus from happening right away and interfering with the VisualState (as suggested by GearBox).
The "InitialFocus.Focus()" call represents a call to a Collapsed TextBox and then I switch to the real TextBox that I want focus.
this.Loaded += (s, e) => Dispatcher.BeginInvoke(() => Dispatcher.BeginInvoke(GetFocus)); private void GetFocus() { HtmlPage.Plugin.Focus(); InitialFocus.Focus(); SearchBox.Focus(); }
This seems to work 100% of the time for me and is the best thing I've found so far. Thanks for all your help.
09-02-2009 10:23 AM |
This thread : http://silverlight.net/forums/t/122682.aspx helps solve the Focus problem after a VisualStateThe idea is to set the Completed Event to the VisualState StoryBoard and in the completed event you wire up you put the focus there.
Because all the focus problems are always related to the UI thread doing something and until it isn't finished, focusing is useless.Focusing will walways work if you're at the VERY END of anything the UI thread is working on.
09-02-2009 10:40 AM |
Setting controls' focus on Microsoft platform has always been a problem, I dunno why. It works a lot easier on other platforms. ;-) ... Also the solutions presented here are obviously mere hacks. The render engine must handle this internally. It must save every call to any .Focus() method of the visual tree in a field, and then, after layouting and other heavy UI work is done, execute the actual focussing for the control that was the last one requesting the focus.
09-02-2009 11:41 AM |
GearBox, have you gotten it to work 100% of the time?
I have the following XAML in MainPage.xaml in the Grid element
<VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="Startup"> <VisualState x:Name="Intro"> <Storyboard Completed="Intro_Completed"/> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups>
In MainPage.xaml.cs
this.Loaded += (s, e) => { VisualStateManager.GoToState(this, "Intro", true); HtmlPage.Plugin.Focus(); };
It's still not working 100% of the time. Better, but not fixed yet. Do I need something else?
09-02-2009 2:51 PM |
Are you sure your VisualState is the last thing going on by the UI Thread ?IF so, I can tell you that mine works 99% fine the 1% it's the cursor seems invisible but the focus is in the textbox at least.
Oh I just thing about something, Maybe the Easying is making just a bit more after the storyboard and this is why my cursor vanishessometimes. Not all the time.
but this is the only thing not going well. The focus always work toughtMake sure also you use the TextBox.Focus. Not only the htmlPage.Pluing.Focus();
09-02-2009 4:57 PM |
Clarification: Focus works 100% of the time, but an invisible cursor is a big problem.
I'm actually using an AutoCompleteBox, so when a user types a letter and the autocomplete comes up, it's impossible to tell which characters I types and which characters are part of the auto complete.
I thought the invisibleness was being caused by the VisualState. How do I know if VisualState is the last thing going on in the UI thread? The GoToState call is happening when the MainPage's Loaded event is called.
09-03-2009 5:45 AM |
MisinformedDNA: I thought the invisibleness was being caused by the VisualState. How do I know if VisualState is the last thing going on in the UI thread? The GoToState call is happening when the MainPage's Loaded event is called.
Is it the very last thing you do in your Loaded event ?Any Call back after ?
It must be the very last line of code you do in the Loaded event.
09-03-2009 1:16 PM |
My code above shows exactly what I am doing, except I swapped the two lines. Of course this is the main page, there is still the frame that has to load.
According to you, you are getting an invisible cursor at times. That tells me that you don't have the final solution yet either.
09-03-2009 6:53 PM |
That's right DNA :)
lonelypr...
09-15-2009 1:26 AM |
thanks, it works
09-17-2009 6:47 AM |
Thanx to whom ?What works ?