Welcome everyone to the static Windows Phone 7: C# Snippets page, this page will contain ALL my little suggestions or others C# (Sharp) programming tip’s, tricks, functions and many other programming related items to help you make your WP7 App’s as good as they can be.
So remember, please Bookmark this page (or visit the navigation menu and click “Windows Phone 7″) so you can read and try out some of the simplest to some of the most extremely useful coding that may just save you, HOURS!
Also, to understand any of the coding, please remember that “T” stands for Task (e.g. Use a MessageBox as a response) and “C” for the Code (e.g. MessageBox.Show(“What to Say.”);).
T: Set Maximum characters imputed in to a Textbox Control.
private void txtZipcode_TextChanged(object sender, TextChangedEventArgs e)
txtZipcode.MaxLength = “5″; // Telling the Textbox, don’t accept more than 5 Characters.
/* As you see from above, we’re telling the Textbox as it receives input/change to make sure it doesn’t accept anymore than 5 characters, which is great for Zipcodes, Usernames, Passwords, Pins, and whatever else you can feel a need to use it for.
Q: What if I don’t want to put the code as the person types because maybe depending on what option a user selects, they can input more characters?
A: Great point, you can do the following:
public partial class MainPage : PhoneApplicationPage
txtPinCode.MaxLength = “8″; // Let’s say only 8 Characters can be used…
private void btnPin_Click(object sender, RoutedEventArgs e)
txtPinCode.MaxLength = listBox1.SelectedIndex; /* We use txtPinSize to allow the user to input how many numbers they’d like to have their Pin to be. Once they ‘Click’ the btnPin button (aka Submit), we instantly update the PinCode to be aware of the change.
Note: Most likely you won’t be able to use the listBox1.SelectedIndex without a .ToString() or at all as it’ll probably error you stating that it can’t convert int to string, so write a small function and define little variables so that when a index is selected it stores it (e.g. var pinVal = listBox1.SelectedIndex; which you may need to .ToString() but it’s a horrible idea and way of doing it and I don’t recommend it AT ALL as this was simply just an idea that another Textbox could be updated during the program running).
Checkbox Snippets – Coming Soon
WebBrowser Control HOWTO/Snippets – Coming Soon
GPS Task/Navigation Snippets – Coming Soon
& Much, Much More…
Are you interested in me giving out tip’s and tricks to everyday WP7 Coding, leave feedback and follow me on Twitter (@LanceSeidman), Circle me on Google+ to let me know you like what you see or hope to see more (or even suggest something) and please remember to share the content!