How to gray out default text in textbox? - c#

When I click on a textbox, I want the default text to disappear. Is there any other property that would work for this purpose?

The following Will set Default text in Gray color. Also When you leave the textbox as blank, again set default text to the textbox.
private void Form1_Load(object sender, EventArgs e)
{
this.textBox2.Enter += new EventHandler(textBox2_Enter);
this.textBox2.Leave += new EventHandler(textBox2_Leave);
textBox2_SetText();
}
protected void textBox2_SetText()
{
this.textBox2.Text = "Default Text...";
textBox2.ForeColor = Color.Gray;
}
private void textBox2_Enter(object sender, EventArgs e)
{
if (textBox2.ForeColor == Color.Black)
return;
textBox2.Text = "";
textBox2.ForeColor = Color.Black;
}
private void textBox2_Leave(object sender, EventArgs e)
{
if (textBox2.Text.Trim() == "")
textBox2_SetText();
}

Add a method to the GotFocus event of the TextBox that will change the Text property to ""
private void Form1_Load(object sender, EventArgs e) {
this.textBox1.GotFocus += new EventHandler(textBox1_Focus);
this.textBox1.Text = "some default text...";
}
protected void textBox1_Focus(Object sender, EventArgs e) {
textBox1.Text = "";
}

It sounds like you are wanting a Watermark in your Textbox.
See these articles.
http://www.codeproject.com/Articles/319910/Custom-TextBox-with-watermark
Watermark in System.Windows.Forms.TextBox
and if you are using wpf something like this http://msdn.microsoft.com/en-us/library/bb613590.aspx

Related

How to Change generated textbox.text using textbox.textchange event

How to access the textbox.text from the other method?
private void button_Click(object sender, EventArgs e)
{
TextBox textBox = new TextBox();
textBox.Text = "";
textBox.TextChanged += textBox_TextChanged;
}
static void textBox_TextChanged(object sender, EventArgs e)
{
textBox.Text = "0";
}
static void textBox_TextChanged(object sender, EventArgs e)
{
var textBox = sender as TextBox;
textBox.Text = "0";
}
You can cast the sender to TextBox type and use a boolean to avoid re-entry (not necessary but good smell):
private bool IsUpdatingTextBox = false;
static void textBox_TextChanged(object sender, EventArgs e)
{
IsUpdatingTextBox = true;
((TextBox)sender).Text = "0";
IsUpdatingTextBox = false;
}
Don't you want to set the text to "0" in button_Clickand textBox.Enabled to false? In this case you can change the BackColor to Window for the appearance.

C# Disjointed Link Labels Navigation for TabControl - How to highlight current tab open with link labels

Is it possible to get any value from the currently displayed tabcontrol that is open (displayed)? Trying to highlight the adjacent / corresponding tab / link label.
I am using link labels as navigation for the tabs. The real (ugly top) tabs will hidden when the project is finished.
//LINK LABELS CLICK EVENTS TO DISPLAY / OPEN TABS
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 0;
//COLOURS TO BE APPLIED WHEN THE CORRESPONDING TAB IS OPEN
linkLabel1.BackColor = Color.Black;
linkLabel1.ForeColor = Color.White;
linkLabel1.ActiveLinkColor = System.Drawing.Color.White;
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 1;
txtFirstName.Focus();
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 2;
}
After #Idle_Mind answer I still was not sure how to bind / wireup the event. This is for anyone else with same question:
//LINK LABELS CLICK EVENTS TO DISPLAY / OPEN TABS
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 0;
labels_LinkClicked(sender, e);
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 1;
txtFirstName.Focus();
labels_LinkClicked(sender, e);
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
tabControl1.SelectedIndex = 2;
labels_LinkClicked(sender, e);
}
//METHOD TO CALL ON EACH CLICK OF LINK LABELS
private void labels_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel lbl = (LinkLabel)sender;
tabControl1.SelectedIndex = labels.IndexOf(lbl);
foreach (LinkLabel curLbl in labels)
{
curLbl.BackColor = (lbl == curLbl) ? Color.Black : Color.Transparent;
}
}
Wire up the LinkClinked() events to the same event handler like below:
private List<LinkLabel> labels;
private void Form2_Load(object sender, EventArgs e)
{
labels = new List<LinkLabel>() { linkLabel1, linkLabel2, linkLabel3 };
}
private void labels_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel lbl = (LinkLabel)sender;
tabControl1.SelectedIndex = labels.IndexOf(lbl);
foreach(LinkLabel curLbl in labels)
{
curLbl.BackColor = (lbl == curLbl) ? Color.Black : Color.Gray;
}
}

Change Button text color

When we pass the mouse over a button we can change the button back color using MouseOverBackColor and MouseDownBackColor using FlatApearance property box.
How can I change a button text color in the same mode when the mouse pass over it?
This should work for all sorts of Buttons, with or without FlatAppearance:
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.ForeColor = Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.ForeColor = SystemColors.ControlText;
}
By using RGB values, this can look like this:
//Hover Text Color changing
private void btnHome_MouseHover(object sender, EventArgs e)
{
btnHome.ForeColor = System.Drawing.Color.FromArgb(1, 102, 207);
}
private void btnHome_MouseLeave(object sender, EventArgs e)
{
btnHome.ForeColor = System.Drawing.Color.LightGray;
}
donĀ“t forget to do everytime a new method call for each button!
Just follow this figure to come to a nice solution:
If you only want to change button text color when mouse is over the button:
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.ForeColor = System.Drawing.Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.ForeColor = System.Drawing.Color.Black;
}

how to reset the focus to last entered textbox in windows application

I have two TextBoxes and a button control in the form. When the button is clicked the name of the last entered TextBox should be displayed in a MessageBox. At the same time I need to reset the focus to last entered TextBox.
string str=string.Empty;
bool foc;
In button click I wrote the following code
if (MessageBox.Show("You want to reset or continue", "control",
MessageBoxButtons.OKCancel) == DialogResult.Cancel)
{
if (foc== true)
{
textBox1.Focus();
}
else
{
textBox2.Focus();
}
}
When I clicks on cancel button the focus should be into textbox which is entered at last
private void textBox1_Enter(object sender, EventArgs e)
{
str = textBox1.Name;
foc= textBox1.Focus();
}
private void textBox2_Enter(object sender, EventArgs e)
{
str= textBox2.Name;
foc= false;
}
Other than the above lines of code is there any other possibility to focus into the textbox, but when number of textboxes increases how i need to write the conditions.
If I am having textbox,combobox,listbox,checkbox or any other controls in the form then how to find in which control the user enterd at last and set focus to that control by using any function instead of writing in every control Enter event
You can handle Leave event of the TextBoxes to store the Last TextBox Control.
Try this:
this.btnSubmit.Click += new System.EventHandler(this.Submit_Click);
this.btnCancel.Click += new System.EventHandler(this.Cancel_Click);
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
TextBox txtLast = new TextBox();
private void textBox1_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void textBox2_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void Submit_Click(object sender, EventArgs e)
{
MessageBox.Show(txtLast.Text);
}
private void Cancel_Click(object sender, EventArgs e)
{
txtLast.Focus();
}
public bool textBox1WasLastFocused = false, textBox2WasLastFocused = false; // Global Declaration
void textBox2_GotFocus(object sender, EventArgs e)
{
textBox2WasLastFocused = true;
textBox1WasLastFocused = false;
}
void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1WasLastFocused = true;
textBox2WasLastFocused = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1WasLastFocused)
MessageBox.Show("textbox1 was ladst focused !");
else if(textBox2WasLastFocused)
MessageBox.Show("textbox2 was ladst focused !");
}

TextBox for Live Search in Windows Forms

How to create a Textbox which displays "search" in grey color when it is empty and standard behavior when user starts typing text into it?
Do it via TextBox Events Enter and Leave and Attributes:
private void textBox1_Leave(object sender, EventArgs e)
{
if(textBox1.Text.Trim().Length == 0)
{
textBox1.Text = "Search";
textBox1.ForeColor = Color.LightGray;
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
See this thread at MSDN for a possible solution: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/93a67793-6426-4d4f-be9d-a9b79725efc8

Categories