Solved

Control text

Posted on 2010-09-21
12
352 Views
Last Modified: 2012-05-10
I am writing a windows application with .net framework 2.0

I want to know how to display control text like in webapps
0
Comment
Question by:GRChandrashekar
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 2

Expert Comment

by:vsosu
ID: 33725312
Can you give more info? what you are expecting?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33725335
Do you want to show text on your form ? use a textbox, label, ...
0
 

Author Comment

by:GRChandrashekar
ID: 33725474
No. I want to display within textbox like "Please enter your name". which is actually background text
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33725602
I don't understand i completely....

Do you mean that you want the text "please enter your name" in a textbox. When the user enter's this textbox, this text must be removed ?



0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33725667
Add leave and enter-event
        private void Form1_Load(object sender, EventArgs e)

        {

            textBox1.Text = "Please enter your name";

            textBox1.ForeColor = System.Drawing.SystemColors.InactiveCaptionText;

        }



        private void textBox1_Enter(object sender, EventArgs e)

        {

            if (textBox1.ForeColor != System.Drawing.SystemColors.WindowText)

            {

                textBox1.Text = "";

                textBox1.ForeColor = System.Drawing.SystemColors.WindowText;

            }

        }



        private void textBox1_Leave(object sender, EventArgs e)

        {

            if (textBox1.Text == "")

            {

                textBox1.Text = "Please enter your name";

                textBox1.ForeColor = System.Drawing.SystemColors.InactiveCaptionText;

            }

        }

Open in new window

0
 

Author Comment

by:GRChandrashekar
ID: 33726205
Yes somewhat like this. but tedious to write for every text box may be some sort of public function would help
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 65

Expert Comment

by:rockiroads
ID: 33726440
Why dont you put the default text on your webpage code?

<asp:TextBox ID="TextBox1" runat="server" Text="Please Enter Username"></asp:TextBox>

If you want the text cleared then add some javascript on the OnClick event.

<asp:TextBox ID="TextBox1" runat="server" Text="Please Enter Username" OnClick="ClearAll('TextBox1');"></asp:TextBox>

So create a javscript function called ClearAll in your asp webpage code
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33732012
>> Yes somewhat like this. but tedious to write for every text box may be some sort of public function would help <<

Write a class which extends TextBox and put the above suggested code in that class. Instead of textbox1, use this reference in that class. Now use the new TextBox wherever you need it. - OOP Inheritance concept ;)
0
 

Author Comment

by:GRChandrashekar
ID: 33732144
It is windows app not web
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33733614

Here is a public common class that can be used from any form.

In constructor of the form having the textbox, call the method like:

TextBoxMessageHandler.AddMessage(textBoxName, "Please enter your name");

TextBoxMessageHandler.AddMessage(textBoxAddress, "Please enter your address");





public Form1()

        {

            InitializeComponent();



            TextBoxMessageHandler.AddMessage(textBox1, "Please enter your name");

        }



//////////////////////////////

TextBoxMessageHandler.cs



using System;

using System.Windows.Forms;



namespace WindowsFormsApplication4

{

    public class TextBoxMessageHandler

    {

        private string _messageText;



        public static void AddMessage(TextBox textBox, string messageText)

        {

            textBox.Text = messageText;

            textBox.ForeColor = System.Drawing.Color.Gray;



            TextBoxMessageHandler handler = new TextBoxMessageHandler();

            handler._messageText = messageText;



            textBox.Leave += new System.EventHandler(handler.TextBox_Leave);

            textBox.Enter += new System.EventHandler(handler.TextBox_Enter);

        }



        private void TextBox_Enter(object sender, EventArgs e)

        {

            TextBox textBox = sender as TextBox;

            if (textBox != null)

            {

                if (textBox.ForeColor != System.Drawing.SystemColors.WindowText)

                {

                    textBox.Text = "";

                    textBox.ForeColor = System.Drawing.SystemColors.WindowText;

                }

            }

        }



        private void TextBox_Leave(object sender, EventArgs e)

        {

            TextBox textBox = sender as TextBox;

            if (textBox != null)

            {

                if (textBox.Text == "")

                {

                    textBox.Text = _messageText;

                    textBox.ForeColor = System.Drawing.Color.Gray;

                }

            }

        }

    }    

}

Open in new window

0
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 33733641

Another way is:

Extend TextBox class into a new class say TextBoxM and include the required event handlers in the derived class.

Then in Form designer, instead of adding TextBox from Toolbox, add TextBoxM control and set its MessageText property to like "Please enter your name"
using System;

using System.Windows.Forms;



namespace WindowsFormsApplication4

{

    public class TextBoxM : TextBox

    {

        private string _messageText = "Please enter value";



        public string MessageText

        {

            get { return _messageText; }

            set 

            { 

                _messageText = value;

                Text = _messageText;

            }

        }



        public TextBoxM()

        {

            Text = _messageText;

            ForeColor = System.Drawing.Color.Gray;



            this.Leave += new System.EventHandler(this.TextBoxM_Leave);

            this.Enter += new System.EventHandler(this.TextBoxM_Enter);

        }



        private void TextBoxM_Enter(object sender, EventArgs e)

        {

            if (ForeColor != System.Drawing.SystemColors.WindowText)

            {

                Text = "";

                ForeColor = System.Drawing.SystemColors.WindowText;

            }

        }



        private void TextBoxM_Leave(object sender, EventArgs e)

        {

            if (Text == "")

            {

                Text = _messageText;

                ForeColor = System.Drawing.Color.Gray;

            }

        }

    }

}

Open in new window

0
 
LVL 65

Expert Comment

by:rockiroads
ID: 33736458
Ok, so what is wrong with just changing the text property in design view?

in design view, click your textbox, look at right hand side for a properties window. if one not there right click on textbox and select properties.

then find the Text property and assign it your default text you want displayed

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now