Require Input in Masked Text Box - c#

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

Related

C# Winforms - Display Tooltip over textbox when mouse not over it

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.

How can I select all the text within a Windows Forms textbox?

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

How to have a textbox click event in asp.net with c#

if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "txt2OnClick")
{
txt2_Click();
}
txt2.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackEventReference(txt2, "txt2OnClick")); //within page load
private void txt2_Click()
{
ImageMap1.ImageUrl = "guide/2.jpg";
}
This is a perfect piece of code to have a click event on textbox, with asp.net (C#).
But the only problem is, we cant type when we apply this code to a textbox. So what I did was set the focus txt2.Focus(); then I can type, but the textbox is not getting validated (I have added a regular expression validator) . Any help? Even to have a better onClick event for a textbox than this?
I don't see any point in having click event for text box.. Remove your 'perfect piece of code' and txt2 if not required..If you have added regular expression validator correctly, it will take care of validation..
EDIT:
For changing some text or showing some image, you don't need to post back to server..
You can either use javascript Focus event or jquery to do that..
In the link you provided, all those photos and texts are placed on divs and are already loaded on browser and when textbox gets focus, those div styles are just toggled between none and block..
Something like this(jsFiddle)..

How can I fire an event if a user enters too large a value in a TextBox?

At the moment I have a text box that is bound to a byte property. If the user enters between 0 and 255 the application behaves as expected and the property's setter executes.
However, if the user enters 256 or greater the property's setter does not execute. All that happens is the TextBox becomes outlined in red. I presume this is to indicate that it's an invalid value.
This isn't good enough though. I need to display a messagebox or some note to the user to inform them it's an invalid value. What do I need to do to make this happen?
You need to add a Validation Summary control on the page.
This will be hidden by default, but when the validation error occurs (as in this case when a value greater than 255 is entered) it will appear telling the user what's wrong.
There are several such controls available for WPF, you will need to evaluate them and pick the one that works for you. You will probably need to set some attributes on the data layer to control the exact error message that's displayed.
Another possibility would be to define the TextChanged event of the text box so that it does a Int32.Parse every time the text changes. It can then fire off a message box if the value exceeds 255.
If you want to be mean you can make the maximum length two characters long and force users to input the number in hex.
You can add a validation error handler to the control or window.
In window constructor:
this.AddHandler(Validation.ErrorEvent, new RoutedEventHandler(OnValidationError));
Handler:
private void OnValidationError(Object sender, RoutedEventArgs e)
{
if (e.OriginalSource is DependencyObject)
{
DependencyObject instance = e.OriginalSource as DependencyObject;
if (Validation.GetHasError(instance))
{
System.Collections.ObjectModel.ReadOnlyObservableCollection<ValidationError> errors = Validation.GetErrors(instance);
// todo build message from errors and display
}
}
}
I have an article that covers what you are asking for. The title might not appear to match with what you are asking, but it happens to demo the feature you are asking for.
How to disable a Button on TextBox ValidationErrors in WPF
This will show you how to not only have the red around the TextBox but also how a message too.

JQuery Auto-Complete With TextChanged / OnBlur PostBack

I'm having a pretty nasty problem with an auto-completing textbox. I want to initiate an asynchronous PostBack whenever a user selects an item from the auto-completed field and retain the value of the item, rather than the text inputted. This works perfectly when enter is pressed rather than a mouse click.
An example of my issue:
Someone goes to the page, and types 1000 into the textbox. The autocomplete displays 10002, 1000B, and 10000. The user clicks on 1000B and an asynchronous PostBack initiates. Instead of 1000B, the TextBox.Text value is still 1000.
My assumption is that the textbox is initiating the PostBack before the value is actually getting assigned to it. I'm just curious if anyone has any possible solutions for what I'm talking about.
I fixed it in this manner:
As per another question on the site I added an autoPostBack parameter to the options list.
In bottom of the SelectCurrent() function, I added these lines.
if (options.autoPostBackSelection == true) {
__doPostBack($input.id, "");
}
Then, my blur function looked like this:
.blur(function() {
hasFocus = 0;
if (!config.mouseDownOnSelect) {
hideResults();
}
if (options.autoPostBackSelection == true) {
selectCurrent();
}
I actually struggled with this for a bit, my Javascript/DOM event skills aren't very good. Hopefully this helps someone.

Categories