Blog Archives

Setting the focus to an textbox in the “iteminvoked” event in a WinJS metro app

The last time I focus alot on question regarding Windows 8 metro style development on Stackoverflow because I really like the new WinRT development environment and already have been doing some metro style app developement. A few days ago I saw this question on Stackoverflow and was interested, because it seems pretty simple. The question was that the asker was not able to set the focus to an input textfield.

First I tried it myself just with a little project, click a button and set the focus on the textbox. So far no problems, works like a charm. I send my testproject to the asker and asked him to test this on his machine to check if this works. Now the asker came up with additional requirements. (Just like every project Glimlach ) The focus needs to be set to the textbox when he has clicked a item in a listview. I have updated my project, added a listview with some testdata, attach the “iteminvoked” eventhandler to the listview and within the handler set the focus to the textbox. That’s where the problems start, somehow the focus is not set to the textbox anymore.

I started playing around with adding eventhandlers to the textbox and the listview “focusin” and “focusout” events. I saw that the textbox got focus but lost it when the “blur” event is re-attached to the listview. On MSDN I found the msSetImmediate method which I added to the “iteminvoked” event handler so the focus is set immediate after the processing is completed.

My code for attaching the “iteminvoked” eventhandler looks now like below.

var listView = document.getElementById("basicListView"); listView.winControl.addEventListener("iteminvoked", function (evt) { if (listView.winControl.selection.count() > 0) { msSetImmediate(setFocus); } });

And below the code for the setFocus function.

function setFocus() { //Set focus on input textbox; var input = document.querySelector("#input-box") input.focus(); }

Now when we run the solution and click an item in the listview, the focus is set to the textbox. For me it’s not totally clear why this is needed to make this work, but for now (Windows 8 Consumer Preview) this works.

The entire solution can be downloaded here TestFocusApplication

Happy developing!

My TechDaysNL 2012 session is online

Friday 17 February I gave a presentation about Windows 8 ‘metro style’ development with JavaScript on the TechDays 2012 event in Den Hague, The Netherlands.

In this session I have covered the following topics:

  • - Metro design principles
  • - WinRT architecture
  • - WinJS library
  • - Demo’s on how to create your own ‘metro style’ app with Javascript.

Below you will find the recording of the session. Warning for my non-dutch readers: The session is spoken in Dutch.

 

Hope you enjoyed being at the Techdays 2012 and watching my session, or seeing it online on Channel 9.

If you have any questions about the presentation or the content don’t hesistate to contact me via the comments or via twitter @bloodyairtimer

I’m speaking at TechDays 2012

The last few years I have been attending the DevDays (which are now callled TechDays) and always had alot of fun being there with colleagues, catch up with some guys and learning alot of the sessions being held during these days. This year will be totally different, because this year I will be doing a session myself.

I will be speaking about developing Metro style apps in Windows 8 with Javascript. In this session I will guide you through the different steps to build your own ‘metro style’ app. In this session I will cover the following topics:

  • Promisses
  • Dataloading with XHR (asynchronous)
  • Datatemplates
  • Databinding
  • Adding controls to the AppBar
  • Livetiles
  • Navigation

For most of the above points I will also show you in demo’s how to do this by yourself. I’m pretty sure that after this session you will say: “Wow, that’s really easy to start developing ‘Metro style’ apps with Javascript!”.

I really hope to see you on Friday 17 february in Den Hague! More details about my session can be found at the Techdays site

Last thing to say is that I’m really proud to have this opportunity to speak at an event like this size.

See you there!