I have a series of textboxes with which I want to associate a tooltip with. This tooltip would appear when the user clicks on a black textbox, then disappear when they start typing or when they leave the textbox. The tooltip should be placed directly above the textbox, this is why I'm using the ToolTip.Show method instead of the ToolTip.SetTooltip method (it lets me control the placement).
So far, for each textbox I have 3 methods; Enter, Leave and TextChanged:
tt = new ToolTip();
String message = "some message"; //different for each textbox
private void textbox1_Enter(object sender, EventArgs e)
{
if (textbox1.Text == String.Empty)
{
tt.Show(message, textbox1, new Point(0, -2 * textbox1.Height));
}
}
private void textbox1_Leave(object sender, EventArgs e)
{
tt.Hide(textbox1);
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
tt.Hide(textbox1);
}
Now consider two textboxes. Clicking on textbox1 triggers the tooltip as expected, in the expected location, then exiting textbox1 causes it to disappear. Trying the same thing on textbox2 also works. Now if I click on textbox1 again, the tooltip has the proper message, but the placement is in the same place as if I had clicked on textbox2. Not only that, but the shape of the tooltip is the same as for textbox2, meaning that my message gets truncated. (The message for textbox1 is longer than the one for textbox2). Does anyone know what might be causing this?
This only happens I think when the IsBalloon property is true. Unfortunately, known bug.
Try it like this:
private void textbox1_Enter(object sender, EventArgs e) {
if (textbox1.Text == String.Empty) {
tt.Show(string.Empty, textbox1, 0);
tt.Show(message, textbox1, new Point(0, -2 * textbox1.Height));
}
}
Related
I used System.Globalization.CultureInfo.InvariantCulture on my Textbox, but whenever I enter the Textbox and leave without altering the value it adds two more zeros, how can I sort this out?
private void textBox1_Leave_1(object sender, EventArgs e) {
double txt = double.Parse(textBox1.Text,
System.Globalization.CultureInfo.InvariantCulture);
textBox1.Text = txt.ToString("N2");
return;
}
Well, in general case, you can keep a collection of Coltrols which were manually edited and on control leaving check if the control is in the collection. Assuming that you work with WinForms:
using System.Globalization;
...
private readonly HashSet<Control> m_EditedControls = new HashSet<Control>();
// On each textBox1 change we should decide if we are going
// to update control's Text on leave or not
private void textBox1_TextChanged(object sender, EventArgs e) {
// If TextBox1 was edited while having keyboard focus,
// i.e. user got focus and edit the textbox manually
// put textbox as edited
if (sender is TextBox box && box.Focused)
m_EditedControls.Add(box);
}
Then on leaving textBox1 you can check if textBox1 has been edited manually:
// On leaving textBox1 we and an additional check
// if textBox1 has been edited manually
private void textBox1_Leave(object sender, EventArgs e) {
if (sender is TextBox box &&
m_EditedControls.Contains(box) &&
double.TryParse(box.Text, CultureInfo.InvariantCulture, out var value)) {
box.Text = value.ToString("N2", CultureInfo.InvariantCulture);
m_EditedControls.Remove(box);
}
}
You all know those Login Textboxes, where inside it says "Username" and "Password".
So my problem is, that i canĀ“t find anything in the Internet that explains to me how i can detect if anything is written in the textbox.
Then i could use "Username" as a background image, that turns white when i start typing. Something like:
if (Textbox_is_not_empty)
{
Change_Background_image_To_white_colour
}
You can erase the text inside the TextBox by handling the Enter event.
private void textBox1_Enter(object sender, EventArgs e)
{
textBox1.Clear();
}
And set the text to "Username" when you leave and the TextBox is empty.
private void textBox1_Leave(object sender, EventArgs e)
{
if(textBox1.Text == "")
textBox1.Text = "Username";
}
This is probably an easy one for some of you.
I have a TextBox and a ListBox. ListBox provides options for the TextBox and copies selected item's text to TextBox on DoubleClick event. ListBox becomes visible only when TextBox fires Enter event. I do not want to discuss my reasons for selecting this control combination.
I want ListBox to disappear when any other control within the Form gets focus. So I capture Leave event of TextBox and call ListBox.Visible = fale The problem is that TextBox will also loose focus when I click on ListBox to select provided option thus preventing me from selecting that option.
What event combination should I use to preserve ListBox to select option but hide it whenever other controls get focus?
In the Leave method, you can check to see if the ListBox is the focused control or not before changing its Visibility:
private void myTextBox_Leave(object sender, EventArgs e)
{
if (!myListBox.Focused)
{
myListBox.Visible = false;
}
}
This example will provide you with the desired outcome:
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
listBox1.Visible = true;
}
void textBox1_LostFocus(object sender, EventArgs e)
{
if(!listBox1.Focused)
listBox1.Visible = false;
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void Form1_Shown(object sender, EventArgs e)
{
//if your textbox as focus when the form shows
//this is the place to switch focus to another control
listBox1.Visible = false;
}
i have a from c# and i want to show text Box after a click in a check box.
but when i scroll down, and check , the text Box is shown in the wrong place !!!
the text Boxes must be in the same level with check Boxes.
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
textBox1.Visible = true;
}
and changing the Location of the text Box don't give good results !
thanks for help.
You are running into an awkward quirk of the Panel control, it only scrolls controls that are visible. When you make it visible in your code, it will have the wrong Location property if you've used the scrollbar. You will need to make the correction yourself. Make it look like this:
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
if (!textBox1.Visible) {
textBox1.Location = new Point(textBox1.Left + panel1.AutoScrollPosition.X,
textBox1.Top + panel1.AutoScrollPosition.Y);
textBox1.Visible = true;
}
}
A better alternative is to use the Enabled property instead, also much less disorienting for the user. Set it to False in the designer and then:
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
textBox1.Enabled = true;
}
I know its a silly question but, still I wanted to know it. I have two textboxes, textbox1 and textbox2. I entered some text in textbox1. Now I want that the value of textbox one should be displayed on textbox2 when I move from textbox1 to textbox2 using tab index or by clicking my mouse on textbox2. I know I can make us of the mouse over event. But it will be great if I get some good opinion from you. Thanks in advance
private void textBox1_Leave(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
private void textBox2_Enter(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}