Wednesday, December 12, 2012

Windows Phone Application Development custom TextBox

Hi, as i feel good when i was writing codes over and over this time i made few customized components in windows phone with silver-light  these are the c# code and you can just use them in you application, maximum of features i added but you can do an infinity number of approaches to reach the optimality of the code and for the good object orientation, i will wait for the review and the ideas from yours side. if you will enhanced the code and all please share with me as well that i can gain the knowledge. you can mail me the queries and valuables at upadhyay.jitesh@gmail.com.

use the code on yours silver-light windows phone  application. the code is written in C#.


using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;



namespace CustomExample.CustomComponents
{
    public class CustoomTextBox : TextBox
    {
        String offbordercolor, onbordercolor, offbackground, onbackground, onforground, offforground, hint, fontfamilyvalue = null;
        int fontsize = 25;
        int thickness, fontweightvalue;
     
        private string textvalue = string.Empty;
        public string D_Text
        {
            get
            {
                return textvalue;
            }
            set
            {
                textvalue = value;
                Default_Text();
            }
        }
        public CustoomTextBox(String offbordercolor_, String onbordercolor_, String offbackground_, String onbackground_,
            String offforground_, String onforground_, int fontsize_, String fontfamilyvalue_, int fontweightvalue_, int thickness_,String hint_)
        {
            this.offbordercolor = offbordercolor_;
            this.onbordercolor = onbordercolor_;
            this.offbackground = offbackground_;
            this.onbackground = onbackground_;
            this.offforground = offforground_;
            this.onforground = onforground_;

            this.fontsize = fontsize_;
            this.fontfamilyvalue = fontfamilyvalue_;
            this.fontweightvalue = fontweightvalue_;

            this.thickness = thickness_;
            this.BorderThickness = new Thickness(thickness);

            this.hint=hint_;

            this.BorderBrush = new SolidColorBrush(HexColor(offbordercolor));
            this.Background = new SolidColorBrush(HexColor(offbackground));
            this.Foreground = new SolidColorBrush(HexColor(offforground));

            this.FontSize = fontsize_;
            this.FontFamily = new FontFamily(fontfamilyvalue);
            this.FontWeight = getFontWeightValue(fontweightvalue_);


            this.GotFocus += (sender, e) =>
                { if (this.Text.Equals(D_Text)) { this.Text = string.Empty; } };
            this.LostFocus += (sender, e) => { Default_Text(); };
        }
        private void Default_Text()
        {
            if (this.Text.Trim().Length == 0)
                this.Text = D_Text;


        }
        protected override void OnGotFocus(RoutedEventArgs e)
        {
         
           
            this.BorderBrush = new SolidColorBrush(HexColor(onbordercolor));
            this.Background = new SolidColorBrush(HexColor(onbackground));
            this.Foreground = new SolidColorBrush(HexColor(onforground));
            // base.OnGotFocus(e);
        }
        protected override void OnLostFocus(RoutedEventArgs e)
        {
           
            this.BorderBrush = new SolidColorBrush(HexColor(offbordercolor));
            this.Background = new SolidColorBrush(HexColor(offbackground));
            this.Foreground = new SolidColorBrush(HexColor(offforground));
            //  base.OnLostFocus(e);
        }
        public Color HexColor(String hex)
        {
            //remove the # at the front
            hex = hex.Replace("#", "");

            byte a = 255;
            byte r = 255;
            byte g = 255;
            byte b = 255;

            int start = 0;

            //handle ARGB strings (8 characters long)
            if (hex.Length == 8)
            {
                a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
                start = 2;
            }

            //convert RGB characters to bytes
            r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
            g = byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber);
            b = byte.Parse(hex.Substring(start + 4, 2), System.Globalization.NumberStyles.HexNumber);

            return Color.FromArgb(a, r, g, b);
        }
        public FontWeight getFontWeightValue(int i)
        {
            FontWeight returnedvalue = FontWeights.Light;
            switch (i)
            {
                case 1:
                    returnedvalue = FontWeights.Bold;
                    break;
                case 2:
                    returnedvalue = FontWeights.Normal;
                    break;
                case 3:
                    returnedvalue = FontWeights.Medium;
                    break;
                case 4:
                    returnedvalue = FontWeights.Light;
                    break;


            }
            return returnedvalue;
        }
    }
}

No comments:

Post a Comment