Forum  Core :: SDK (Vi...   How do I...?  When the TextBox the focus changes, how to switch the display Text?
Previous Previous
 
Next Next
New Post 11/19/2010 12:53 AM
Unresolved
  anqingrong
18 posts
No Ranking


When the TextBox the focus changes, how to switch the display Text? 
Modified By anqingrong  on 11/19/2010 4:55:44 AM)

hi:

 When the TextBox the focus changes, how to switch the display Text?

The following is my code, how do I do?

When the focus changes textBox1, and not re-call GetText.

CustomTextBox.cs

using System;
using System.Data;
using Gizmox.WebGUI.Forms;


namespace TestWebGuiTextBoxFocus
{
    public class CustomTextBox : TextBox
    {
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                if (base.Focused)
                    string.Format("Test [{0}] ", base.Text);
                else
                    base.Text = value;
            }
        }
    }
}

Form1.cs

#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

#endregion

namespace TestWebGuiTextBoxFocus
{
    public partial class Form1 : Form
    {

        private CustomTextBox textBox1;
        private Gizmox.WebGUI.Forms.TextBox textBox2;

        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_GotFocus(object sender, EventArgs e)
        {
            base.Update();
        }

        private void textBox1_LostFocus(object sender, EventArgs e)
        {
            base.Update();
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new TestWebGuiTextBoxFocus.CustomTextBox();
            this.textBox2 = new Gizmox.WebGUI.Forms.TextBox();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(63, 47);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "Value";
            this.textBox1.LostFocus += new System.EventHandler(this.textBox1_LostFocus);
            this.textBox1.GotFocus += new System.EventHandler(this.textBox1_GotFocus);
            //
            // textBox2
            //
            this.textBox2.Location = new System.Drawing.Point(63, 88);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 21);
            this.textBox2.TabIndex = 1;
            //
            // Form1
            //
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Size = new System.Drawing.Size(600, 301);
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
    }
}

 
New Post 11/20/2010 4:30 AM
  palli
11824 posts
1st Level Poster




Re: When the TextBox the focus changes, how to switch the display Text? 

Hi anqingrong,

I am not exactly sure I got your idea correct, but based on my understanding, this can be done with the following customization of the TextBox control.

 

    public partial class CustomTextBox : TextBox
    {
        public CustomTextBox()
        {
            InitializeComponent();
            this.GotFocus += DummyEventHandler;
            this.LostFocus += DummyEventHandler;
        }
 
        private void DummyEventHandler(object sender, EventArgs e) {
        }
        protected override void OnGotFocus(EventArgs e)
        {
            if (! (this.Tag == null)) {
                base.Text = (string) base.Tag;
            }
            base.OnGotFocus(e);
        }
        protected override void OnLostFocus(EventArgs e)
        {
            base.Tag = base.Text;
            base.Text = string.Format("Test [{0}] ", base.Text);
            base.OnLostFocus(e);
        }
    }

A few points to note here. First, Visual WebGui has a highly optimized engine, and one of the optimizations is not to fire an event if noone is listening to the event anyway. This is why you need a dummy event handler registered within the control, otherwise your overridden OnGotFocus and OnLostFocus would not fire at all. This could as well be implemented as event handlers instead of the dummy and the On* methods, I just wanted to point this out to you.

Second, there may be cases where using the Tag property is not possible, for instance if that property is used for something else already. In that case, you can just implement your own private property for this purpose.

Third, this implementation is fairly similar to what the WatermarkTextBox is doing, so you might want to look on how it is constructed as a custom control within our framework. The main gain of building a custom control to do this for you would be that it could be constructed clientside only, without the extra roundtrips needed to the webserver, so the user experience will be more snappy. You can view some samples of custom textbox control in this wiki article here.

Hope this helps,

Palli

 


Páll Björnsson - Visual WebGui support team - Email: support@visualwebgui.com
 
New Post 11/20/2010 7:14 AM
  anqingrong
18 posts
No Ranking


Re: When the TextBox the focus changes, how to switch the display Text? 

Hi Palli, Thanks for your answer.

I want to achieve the following functions:
1.TextBox bound to the DataTable.
2. Field decimal dalue is 100.000000 for example, when the TextBox gets the focus, the show "100.000000"; loses focus display "100", that is, dynamic format.
3. I do not know what method is the focus of changes in the TextBox to re-trigger TextBox.GetText ()?

 
New Post 11/20/2010 8:13 AM
  palli
11824 posts
1st Level Poster




Re: When the TextBox the focus changes, how to switch the display Text? 

Hi,

See if you can use Ryan's idea in this thread here. This thread, and more TextBox related discussion is linked from the TextBox wiki page that you will find here.

Hope this helps,

Palli

 


Páll Björnsson - Visual WebGui support team - Email: support@visualwebgui.com
 
New Post 11/20/2010 11:22 PM
  anqingrong
18 posts
No Ranking


Re: When the TextBox the focus changes, how to switch the display Text? 

Hi Palli:

1.Ryan 's idea I did not understand how to use CurrencyFormatter class.
2. To correct my First Code

    public class CustomTextBox : TextBox
    {

        public CustomTextBox()
        {
            this.GotFocus += DummyEventHandler;
            this.LostFocus += DummyEventHandler;
        }

        private void DummyEventHandler(object sender, EventArgs e)
        {
        }

        public override string Text
        {
            get
            {
                //old code if (base.Focused)   //Control.Focused property always returns a True result
                if (Form.ActiveControl == this)
                    return string.Format("Test [{0}] ", base.Text);
                else
                    return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }

        protected override void OnGotFocus(EventArgs e)
        {

            //if (!(this.Tag == null))
            //{
            //    base.Text = (string)base.Tag;
            //}
            base.OnGotFocus(e);
            Update();
        }
        protected override void OnLostFocus(EventArgs e)
        {
            //base.Tag = base.Text;
            //base.Text = string.Format("Test [{0}] ", base.Text);
            base.OnLostFocus(e);
            Update();
        }

    }
3. Subsequently, after the test is Control.Focused property always returns a True result.

4.Generated after the code above into another problem: should have been the focus of control, when the implementation of the Update () method after, CustomTextBox lost focus.

 
Previous Previous
 
Next Next
  Forum  Core :: SDK (Vi...   How do I...?  When the TextBox the focus changes, how to switch the display Text?
Azure banner
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS