I am working on a project in Visual Studio 2017, using Winforms and C#. I have created a textbox which accepts only certain characters, and I would like it to display a bubble tooltip saying "Only letters and numbers are allowed" whenever a user enters an invalid character. I have managed to display a tooltip:
//titleInput - The textbox.
//characterWarning - the tooltip.
private void TitleInputKeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsLetter(e.KeyChar) && !Char.IsNumber(e.KeyChar) && !Char.IsWhiteSpace(e.KeyChar) && !Char.IsControl(e.KeyChar)) //Check if character is invalid.
{
characterWarning.SetToolTip(titleInput, "Only letters and numbers are allowed"); //Display the tooltip.
e.Handled = true; //
}
}
But it is shown only if the mouse is over the textbox, and if the textbox is in focus. How do I make a tooltip show over a textbox even if the mouse is not over the control? Thanks in advance.
You can use Tooltip control on the form, you can do it like this:
ToolTip1.Show("Text to display", Control)
Check this link for reference.
Consider using System.Windows.Forms.ToolTip.Show Method instead.
Related
I have a problem in a winform app.
I have several masked textbox which use the short date mask (//____), the problem is that if I select all the text (either with Ctrl + a or from code) and I write a new date is like that the first character goes at an 11th position (the mask disappear and i see only the character I wrote) and if I press backspace the text becomes something like this _1/01/1979 and I have to select all again and press backspace or delete everything.
I handled in this way
private void maskedTxt_KeyPress(object sender, KeyPressEventArgs e)
{
MaskedTextBox msk = sender as MaskedTextBox;
if (msk != null)
{
if (!string.IsNullOrEmpty(msk.Text.Replace("/", string.Empty).Replace(":", string.Empty).Trim()) && _focused)
{
_focused = false;
SendKeys.SendWait("{BACKSPACE}");
}
}
}
_focused is a Boolean variable that I set to true if a validation error happens at the leave event of the masked textbox (invalid date, the date is too big or too small etc...)
so that when someone enters the textbox the text can be written correctly
Is there a better way to handle this "error" or this is good? I tried it and it worked but a lot of people will have to use this application and probably there will be some errors along the way.
Thanks
I want to select all the text that is with in text box.
I've tried this using the code below:
textBoxResults.SelectionStart = 0;
textBoxResults.SelectionLength = textBoxResults.Text.Length;
Source: I got this code from here http://msdn.microsoft.com/en-us/library/vstudio/hk09zy8f(v=vs.100).aspx
but for some reason it doesn't seem to work - meaning, no text gets selected.
You can use the built in method for this purpose.
textBoxResults.SelectAll();
textBoxResults.Focus(); //you need to call this to show selection if it doesn't has focus
You can also try the following which might solve you problem:
textBoxResults.SelectAll();
This works well with multi-lined textbox.
This method enables you to select all text within the control.
public void CopyAllMyText()
{
// Determine if any text is selected in the TextBox control.
if(textBox1.SelectionLength == 0)
// Select all text in the text box.
textBox1.SelectAll();
// Copy the contents of the control to the Clipboard.
textBox1.Copy();
}
Check this link for more info. http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.selectall.aspx
I am really confused about this. Please consider below scenario.
Scenario:
I have a C# Winform app with few Textbox controls. Now when I input data in these text boxes, for example, "THIS IS MY SAMPLE TEXTBOX", which overlaps the Textbox's visible area and displaying as "AMPLE TEXTBOX". But I want the text to be displayed from the starting position like "THIS IS MY S" and then if needed, overlaps. How can I do this? I have tried below but no luck. Please help. Thanks.
(sender as TextBox).TextAlign = HorizontalAlignment.Left;
Edit
I am also using AutoCompleteMode.Suggest so that when I press any key, corresponding list will be displayed similar to dropdownlist. But the first item of this list is selected by default, which I do not want. Can you please suggest in this also. Thanks.
Final Solution
I am using this to solve the issue
(sender as TextBox).TextAlign = HorizontalAlignment.Left;
(sender as TextBox).Select(0, 0);
Thanks to #Har Har.
I found the solution, To position the cursor at the beginning of the contents of a TextBox control, call the Select method and specify the selection start position of 0, and a selection length of 0.
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "Hello this is a sample application";
textBox1.Select(0, 0);
}
It will show the cursor position at 0 index, It's working.
I have a masked text box (number of days) in a form that has to always have an integer inside. It cannot be blank when the user pressed the "OK" button on the form.
What I need is when the user pressed "OK", if the text box is not filled as it should be, a tool tip come up and show the user they need to enter information there before they can proceed.
What should I use for this? I'm guessing a variation of a Tool Tip and Masked Text Box, I just need a push in the right direction as I can't find the information anywhere.
use a regex control to validate the input and a required field validator. on the validation you could fill out a div with display:none then use jquery tooltip (or something like it) to display a tooltip with the information in that div. thats my first blush response anyway.
it will work for you
private void button1_Click(object sender, EventArgs e)
{
if (maskedTextBox1.Text == "")
this.errorProvider1.SetError(this.maskedTextBox1, "Plz fill it");
else
this.errorProvider1.Dispose();
}
Use Validation controls provided by asp.net. You can set a required field validator and a Regular Expression Validator for the input field. Then, set the CausesValidation property of the input control to true. Validator controls provide error message labels which will be visible if the input value is invalid. For more information, you can visit http://msdn.microsoft.com/en-us/library/debza5t0.aspx
I've not gone into much research here, but the intuitive thing is not working:
private void SerachButton1_Click(object sender, EventArgs e)
{
String serchTerm = searchTerm1.Text;
String text = usualTextBox.Text;
Int32 index = text.IndexOf(serchTerm);
if (index >= 0)
{
usualTextBox.Select(index, serchTerm.Length);
}
}
SelectedText, SelectionLength and SelectionStart properties are as I expect them after Select is called but there's no visible selection.
What am I doing wrong here?
Edit: I've also tried RichTextBox. When I set background and text colors for the selection it shows up, but it won't unselect automatically when you manually select another part of text or just click on a position in text. Are these two types of selection inherently different and if you select programmatically you have to also deselect programmatically?
You need to set usualTextBox.HideSelection to false so that the selection remains visible when the focus is not in the TextBox.