locked
c#.net code for "How can we raise GotFocus event to TextBox control" in Windows Phone 8. RRS feed

  • Question

  • Dear Sir/Madam,

    How can we raise GotFocus event to TextBox Control in the Windows Phone 8.

    I am using like this but not raise that event.

           public _10MinUserPages()
            {
                InitializeComponent();
                tblUserName.Text = _10MinClass._10MinStaticClass.uid;
                string userIdPwdName = _10MinClass._10MinStaticClass.csUidAndPwd;
                string[] aryUserIdPwdName = userIdPwdName.Split(',');
                tblUserName.Text = aryUserIdPwdName[2].ToString();
    
                msgUserPages = _10MinClass._10MinStaticClass.csUserPages;
    
                if (_10MinClass._10MinStaticClass.csWaterMarkTbText == "" || _10MinClass._10MinStaticClass.csWaterMarkTbText == "Search Form")
                {
                    listmenu();
                }
                else
                {
                    eventRaising();
                    //watermarkSearchTB.GotFocus += new RoutedEventHandler(watermarkSearchTB_GotFocus);
                    //watermarkSearchTB.TextChanged += new TextChangedEventHandler(watermarkSearchTB_TextChanged);
                }
               
            }
            private void eventRaising()
            {
                watermarkSearchTB.GotFocus += new RoutedEventHandler(watermarkSearchTB_GotFocus);
            }

    private void watermarkSearchTB_GotFocus(object sender, RoutedEventArgs e)
            {
                SolidColorBrush Brush1 = new SolidColorBrush();
                if (_10MinClass._10MinStaticClass.csWaterMarkTbText != "")
                {
                    watermarkSearchTB.Text = _10MinClass._10MinStaticClass.csWaterMarkTbText;
                }
                if (watermarkSearchTB.Text == "Search Form")
                {
                    watermarkSearchTB.Text = "";
    
                    Brush1.Color = Colors.LightGray;
                    btnSearchImg.Background = Brush1;
                    tbSearchImg.Background = Brush1;
                }
                else if(watermarkSearchTB.Text != "Search Form")
                {
                    Brush1.Color = Colors.White;
                    btnSearchImg.Background = Brush1;
                    tbSearchImg.Background = Brush1;
                }
            }

    Actually my problem is When debugging point is came at  "

    watermarkSearchTB.GotFocus += new RoutedEventHandler(watermarkSearchTB_GotFocus);

    "

    it is not working i mean not raise that event.

    Thanks & Regards,

    SrinivaaS.B.

    Monday, April 21, 2014 6:05 AM

All replies

  • You have to use TextBox.Focus method

    http://msdn.microsoft.com/en-us/library/ms743469(v=vs.110).aspx

    private void eventRaising()
            {
                //Add handler only if you have not added before
                watermarkSearchTB.GotFocus += new RoutedEventHandler(watermarkSearchTB_GotFocus);
     
               //To set focus on TextBox
               watermarkSearchTB.Focus()
            }


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Monday, April 21, 2014 6:44 AM
  • Hi Khanna,

    Thanks for your reply,But it is not working.My problem is not rectified.

    Actually my problem is that User search one type of Form name in the First Page,after search that Form ,OnClick  to the Form as well as Open design page(Second Page).Now he click  GoBack ,again he came  to First Page with Searched Forms, but it show the empty page of First page.

    How can we raise the GotFocus event in the TextBox control.

    Thanks and Regards,

    SrinivaaS.

    Monday, April 21, 2014 9:21 AM
  • If you are coming back from another page (i.e. using GoBack) then instead of having TextBox.Focus in constructor you should have that code in Loaded event of Page

    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Monday, April 21, 2014 11:12 AM