Friday, October 12, 2012

Set TextBox Focus in Safari and Chrome?

Set TextBox Focus in Safari and Chrome?

I need to set focus for the first textbox in my silverlight form. I tried, 

System.Windows.Browser.HtmlPage.Plugin.Focus();
textBox.Focus();

but this workaround is not working in chrome and safari.

this.Dispatcher.BeginInvoke(textBox.Focus);

Even I tried to use Dispatcher, but no luck. How set the focus for Chrome and safari??


Answers & Comments...

Answer: 1

Hi Siva ,

Try this one

on your xmal add Loaded="UserControl_Loaded"

 private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
           txtname.Focus();
        }



Answer: 2

I tried that but that is not working. I am getting the cursor in the text box specified but not able to type into that textbox. I need to click on the textbox again to start typing.

I tried all the work arounds available, and they are working fine with IE and Mozilla but unable to make this work in chrome and safari. 



Answer: 3

hi,

I also tried it not working but using following code

 HtmlDocument doc = HtmlPage.Document;
            doc.GetElementById("silverlightControlHost").Focus();
            HtmlPage.Plugin.Focus();
            txtname.Focus();

and if you click anywhere in form it will work.


 



Answer: 4

There is a known issue with Safari and Chrome, when you can't pass a focus to plugin (Flash or Silverlight).

http://code.google.com/p/chromium/issues/detail?id=27868



Answer: 5

Did you try the other solutions mentioned at http://silverlight.farreachinc.com/post/2010/09/13/Why-Cant-I-Focus.aspx - even those in the comments to that post?



Answer: 6

Finally I was able to fix this with the help of Javascript on the page, where my silverlight object is hosted.

<script type="text/javascript">

<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" _mce_style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">function setFocusOnSilverlight() {</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" _mce_style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">            var silverlightObject = document.getElementById('silverlight');</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" _mce_style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">            if (silverlightObject) { silverlightObject.tabIndex = 0; silverlightObject.focus(); }</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" _mce_style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">        }</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" _mce_style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">    </script></div>

function setFocusOnSilverlight() {

            var silverlightObject = document.getElementById('silverlight');

            if (silverlightObject) { silverlightObject.tabIndex = 0; silverlightObject.focus(); }

        }

    </script>

<body onload="setFocusOnSilverlight()">


Thanks pitchai for your link. I got this workaround from the issue link.



Answer: 7

Hm ... this solution seams to me that it is exactly the solution presented in the comment by "Andrus" in the link I posted. Am I wrong?



Answer: 8

This is late, but if you are running OOB in SL5 this methods works:

        ' NOTE: Can't use System.Windows.Browser.HtmlPage.Plugin.Focus when in OOB
If System.Diagnostics.Debugger.IsAttached Then
' Works for VS 2010 IDE
System.Windows.Browser.HtmlPage.Plugin.Focus()
User_tb.Focus()
Else
' Works for IIS7 deployment
User_tb.UpdateLayout()
User_tb.Focus()
User_tb.TabIndex = 0
End If
As you can see, the VS 2010 IDE operates differently from a real web deployment on an IIS7 server.  Why Microsoft provide a development that doesn't work the same as one's deployment is beyond me??  Just lazy I guess.
  
Rob
  


Answer: 9
No need of adding that much javascript ....
Follow below instructions ..... it works for Silverlight in Chrome! It's necessary to set tabindex of silverlight object to 0 and also windowless param to true: <object ... tabindex="0"> <param name="windowless" value="true" /> ... </object>




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog