Windows Phone Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 26 October 2007

Building a TextBox in Silverlight 1.0...

Posted on 11:58 by Unknown
One of the problems I have seen a lot of is building full blown forms in Silverlight 1.0. For the most part, Silverlight apps have placed HTML over Silverlight. This certainly works, albeit not the ideal situation and Silverlight 1.1 promises to deliver us a real text box. However, in the mean time, I decided I needed a 'Silverlight 1.0' text box. So here is how I made one work.
In building a text block there are a number of key bits of functionality we need todo: making the control look like a text box, making the text box behave with focus like a text box (have a cursor) and getting key board events into the text box. If you can address these then things like wrap, overflow and size ratio's etc are pretty straight forward.

Actually making a control to look like a text box is not too hard. Basically we have a canvas with a nice boarder in XAML and you can make something that looks like a text box. In this case something like this:




Nice and simple. Now that it 'looks' like a text box we need to have a cursor when it gets focus... For the cursor you can do a number of other things but lets start with a little line rectangle. We set the size and starting position in our XAML and then make it invisable. In our code behind we wire up an event on the box so that on 'MouseLeftButtonDown' we get the event and set the 'textbox' into a focus state. Basically these means we set a flag, changed opacity and started an animation on the cursor to make it blink.

Now our text block looks like a text block when we click on it and the cursor shows up... Now we need to actually make it work. Todo this we can add a text block element and wire an additional event to the document object of the page to capture keyboard events. In our keyboard event handler we need to check the key codes against our look up table so we know what they mean and add the character to our text box. If we run our text block now we notice that this appears to work but the cursor doesn't move. So then we make our event handler move the cusor as well.

So in less then a few hundred lines of code we have a working text box. From here we now need to be able to deal with text overflow moving the cursor back and forth and sizes and ratios. When the control is wrapped nicely in a class and xaml file we can use it in our projects by just creating the class and passing in a destination or target and 'poof' instance text box. Almost like its built in.

Now to see if anyone is actually reading the blog... If any one is interested in the code let me know. :)

Ok Ok, so it seems some one actually does really read my blog. :) I feel much better so I'm going to post more on the text box.
Lets start by updating our xaml a bit.



now we have added the cursor and textblock we talked about earlier. In our code behind we have our event handler called:

TextBox_OnKeyPress and an initialization routine called InitTextBox. In the onload event we then call our iniatilizer and wire up the key press event handler. Now the trick here is that we are not wiring anything to 'silverlight' persay but in IE we wire it up to the document object like this:

function onload(sender, args)
{ InitTextBox(sender); document.onkeyup = TextBox_OnKeyPress; }

Ideally this all is wrapped in a consumable control which I'm will finish when I finish this cool char size algorythm so everything isn't hard coded but in the mean time this example uses a number of present values in the xaml and things like font size and base increment in the code. Also there is a GetWidth function with hard coded values that is used for moving the cursor on the text box surface.

So back to our function InitTextBox(sender). In the sample I did initially this function basically saves some references to different parts of the control such as the cursor and text box for manipulation and then it adds an event listener to the text box canvas. This function is around actually giving the control 'focus' effectiving. There are a few other things you might want todo that is not in the sample like code for getting the control out of focus etc. So our add event in the initializer looks like this:

TextBox.addEventListener("MouseLeftButtonDown",
function( sender, args )
{
IsTextBoxMouseCaptured = true;
TextBoxCursor.Opacity = "1";
CursorAnimation.begin();
} );

So when some one actually clicks on to the control we set our flocus flag to true and turn the cursor on by setting the opacity and turning on the blink animation.

Back to our key press event. This guy remember we wired up to the document object but we only want to get events when the control has focus. That means in the event handler we need to test for the flag and if true then we can do something. Here is a sample:

function TextBox_OnKeyPress()
{
if( IsTextBoxMouseCaptured )
{
var EventCode = Number(event.keyCode);
switch(EventCode)
{
case 48: SetCharacter( "0", ")", event.shiftKey );
break;
case 37:
TextBoxCursor["Canvas.Left"] = LastPosition();
break;
}

So in this case we see the flag test and then we grab our 'EventCode' from the keyboard and we use a case statement to determine what character this is and what todo. To make the example easier I removed all but two character examples. The first case we are dealing with the zero/close paren key. Note we call a function called SetCharacter and pass in three events. The SetCharacter function gets the current shown text, determines which charater we want based on shift condition adds the charater to the real text value we keep in a variable and then determines what text is to be shown places the text and calculates the cursor position based on another function called GetWidth which determines all the offsets from the base character size (this is the one that needs the cool algorythm so all the values are not hard coded...).

The last case is a move back button which calls the 'LastPosition' function and moves the cursor based on the character behind it. So that is basically all the key aspects of doing a text box in Silverlight 1.0.

}}
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in controls, javascript, silverlight, textbox | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Silverlight Streaming in 5 minutes or less
    Microsoft as part of the whole Silverlight ‘thing’ has provided a service to allow people to upload videos and get those video streamed alon...
  • Silverlight Applications Taking All the Available Realestate
    Karim sent me this. It is a simple way make sure you Silverlight Application uses all the available realestate using just CSS: /*...
  • Silverlight TV Episode 3: Multi-Touch 101 with Silverlight
    John interviews Silverlight MVP David Kelley (thats me) about developing multi-touch applications in Silverlight. I discuss the types of mul...
  • Dependency Injection Made Easy
    Part of the whole fun with doing 'ard'd samples is just the fun of doing something not quit PC but the bottom line really is doing c...
  • Silverlight Preloader animation is the answer
    I got this email today: Hi! In our project we call a function which retrieves a data from a webservice. This function takes some time 1-3 se...
  • Silverlight 2 Event bindings
    So in the process of working on this presentation for dev teach and the article and the book I got in a long discussion with alot of the ubb...
  • Windows Phone 7 Development Using Visual Studio 2010
    with David Kelley and AppDev Windows Phone 7 is a new step for Microsoft for the mobile platform. This course will introduce the mobile OS a...
  • Dictionary Definition of Xaml (verb and noun)
    A friend 'Ariel' from www.facingblend.com did a short post about Xaml being a verb. I've heard this a few times and thought th...
  • More on Panels
    I was playing around and made a few more panels. Lets start with a random panel. This panel builds on what we learned about the animating ...
  • No Soap for you! - The No Silverlight Experience
    So I'm collecting hacks for my upcoming book, and I must say, here is a simple one, but one of my favorites... LOL! On my HackingSilver...

Categories

  • .net
  • 3D
  • adam
  • adcontrol
  • adobe
  • agile
  • algorithms
  • analytics
  • andrew
  • android
  • Animating Panel Base
  • animation
  • apache
  • apphub
  • apple
  • apps
  • architecture
  • ariel
  • article
  • ASP.NET
  • balder
  • bar camp
  • behavior
  • best practices
  • beta 1
  • beta 2
  • bi
  • bitmap effect
  • blend
  • blendables
  • blog
  • book
  • book review
  • bookreview
  • browser
  • brush
  • build
  • c#
  • channel9
  • cmm
  • codebrowser
  • codemagazine
  • codemash
  • codeplex
  • color
  • com
  • command
  • composite
  • controls
  • Craig
  • crossfader
  • csharp
  • CSS
  • custom event
  • Dan
  • data
  • datagrid
  • davidjkelley
  • davidkelley
  • ddj
  • Deep Zoom
  • dependencyproperty
  • design
  • design patterns
  • designers
  • devconnections
  • developer
  • developers
  • devin
  • DevTeach
  • dispatcher
  • dotnetslackers
  • dp
  • Dr WPF
  • easy
  • eclipse
  • ecma
  • education
  • einari
  • ET
  • event
  • exchange
  • expression
  • facebook
  • facing blend
  • Faisal
  • firestarter
  • flash
  • flex
  • font
  • free
  • fun
  • futures
  • gadget
  • game
  • games
  • gesture
  • google
  • Grid
  • hack
  • hacking
  • hacking phone 7
  • Hacking Silverlight
  • hard
  • hero
  • holst
  • howto
  • hta
  • HTML
  • html5
  • HTMLAppHostFramework
  • htmlapplication
  • ia
  • identitymine
  • IE
  • IE 8
  • iis
  • images
  • indexability
  • INETA
  • Infragistics
  • Integrator
  • interact
  • iphone
  • isolatedstorage
  • issues
  • itemscontrol
  • ixda
  • jared
  • jason cook
  • javascript
  • jeremiah
  • jobi
  • jobs
  • johnpapa
  • jordan
  • josh
  • jscript
  • json
  • Karim
  • kaxaml
  • kellywhite
  • keynote
  • KimSchmidt
  • law of
  • layout
  • linux
  • listbox
  • LOB
  • mac
  • mango
  • manning
  • marketing
  • marketplace
  • math
  • media element
  • media encoder
  • methodology
  • microsoft
  • MIX
  • MIXer
  • mobile
  • monitization
  • monitizationmodels
  • movie link
  • MSDN
  • msdnbytes
  • msdnradio
  • msretail
  • mstag
  • multitouch
  • MVP
  • MVVM
  • Netflix
  • nike
  • nui
  • object oriented
  • OOB
  • out of browser
  • packt
  • panels
  • parchment
  • parchment apps
  • paths
  • PDC
  • peter
  • phone7
  • phone7unleashed
  • phones
  • php
  • Pixel8
  • pixelshader
  • player
  • popfly
  • prediction
  • preemptive
  • preloader
  • presentations
  • radial panel
  • random panel
  • reference
  • requirements
  • retail
  • review
  • ria
  • robby
  • ROI
  • RPS
  • ryan
  • sajiv thomas
  • SCRUM
  • SD2IG
  • Sea Dragon
  • searchability
  • seattle
  • seattlesilverlight
  • seattleslug
  • sebastian
  • services
  • sharepoint
  • sharepoint2010
  • sic
  • side bar gadget
  • Silver Dragon
  • silverlight
  • silverlight 1
  • silverlight 2
  • silverlight 2.0
  • silverlight 3
  • silverlight 4
  • silverlight insiders
  • silverlight show
  • silverlight4
  • silverlight5
  • Silverlight5
  • silverlightconnections
  • silverlightcream
  • silverlighttv
  • simon
  • simonsaid
  • simple
  • SMART
  • snack
  • stackpanel
  • stevejobs
  • streaming
  • stuartcelarier
  • surface
  • symbian
  • tard
  • teched
  • TED
  • testing
  • textbox
  • TFS
  • threading
  • tim
  • tip
  • tiredallover
  • tool
  • touch
  • touchtag
  • training
  • twitter
  • ui
  • uml
  • usergroup
  • UX
  • uxdesign
  • vagas
  • victor
  • video
  • videos
  • vista
  • visual studio
  • volta
  • VS
  • vsm
  • WCF
  • win8
  • Windows7
  • windows8
  • windowsphone
  • windowsphone7
  • wirestone
  • workflow
  • wp7
  • wp7dev
  • WPF
  • wrappanel
  • wrox
  • xaml
  • xap
  • XML
  • xna
  • zen
  • zphone

Blog Archive

  • ►  2012 (5)
    • ►  May (1)
    • ►  April (2)
    • ►  March (1)
    • ►  February (1)
  • ►  2011 (29)
    • ►  December (2)
    • ►  November (2)
    • ►  October (3)
    • ►  September (1)
    • ►  August (5)
    • ►  June (5)
    • ►  May (2)
    • ►  March (1)
    • ►  February (5)
    • ►  January (3)
  • ►  2010 (51)
    • ►  December (5)
    • ►  November (4)
    • ►  October (3)
    • ►  September (5)
    • ►  August (3)
    • ►  June (3)
    • ►  May (6)
    • ►  April (3)
    • ►  March (9)
    • ►  February (3)
    • ►  January (7)
  • ►  2009 (75)
    • ►  December (3)
    • ►  November (2)
    • ►  October (3)
    • ►  September (7)
    • ►  August (4)
    • ►  July (7)
    • ►  June (9)
    • ►  May (12)
    • ►  April (13)
    • ►  March (8)
    • ►  February (2)
    • ►  January (5)
  • ►  2008 (119)
    • ►  December (8)
    • ►  November (10)
    • ►  October (12)
    • ►  September (10)
    • ►  August (11)
    • ►  July (4)
    • ►  June (10)
    • ►  May (5)
    • ►  April (3)
    • ►  March (11)
    • ►  February (8)
    • ►  January (27)
  • ▼  2007 (34)
    • ►  December (6)
    • ►  November (11)
    • ▼  October (17)
      • Disclosure
      • JavaScript type casting
      • Silverlight Key Events In full screen mode...
      • Embedding Popfly Silverlight Mashups in Community ...
      • Creating a 'Path' Object in Silverlight
      • JavaScript Class Structures
      • Silverlight 1.0, JSON and (gasp) PHP
      • Silverlight Host Loosing Mouse Capture...
      • Path Performance Issues in Silverlight
      • Parsing XML in Silverlight 1.0 - Cross Browser
      • Building a TextBox in Silverlight 1.0...
      • ASP.NET Server Controls in VS 2008 and .NET 3.5 an...
      • Silverlight 1.1 As A Sharepoint Web Part
      • Wiring Silverlight Into the Browser History/Back B...
      • Popfly goes Beta
      • Silverlight - Associating Files in Visual Studio
      • Silverlight, JSON and Automatic Design Asset Zip a...
Powered by Blogger.

About Me

Unknown
View my complete profile