How to validate text in textbox control in winforms?
I have a control, where user have to put string, like "13:55". I want to show MessageBox, when this value will be diffrent, than "XX:YY".
How to do it?
In asp.net it was so easy to make, but how to implement it on winforms?
Check out the MaskedTextBox if you don't want to have to validate in the first place.
var l_control = new MaskedTextBox();
l_control.Mask = "00\:00";
If you want to make the first digit optional:
l_control.Mask = "90\:90";
Otherwise, you could use a regular expression. 4 digits separated by a colon would be: #"^\d{2}:\d{2}$". (The # symbol prevents C# from treating '\' as an escape character - nothing unique to regex.)
There are three validation videos at http://windowsclient.net/learn/videos.aspx that will walk you through the whole process.
But using a Masked Textbox might be easier, depending on what you are collecting for data.
Heck, for what you're doing, you could be really safe and use two NumericUpDown controls and not have to deal with the validation at all.
You should take a look at C# Regex
Match match = Regex.Match(input, "^\d\d:\d\d$"));
if (!match.Success) MessageBox.Show("Error");
You could also use an ErrorProvider instead of popping up a messagebox. An example is available on msdn for the ErrorProvider class. Basically you subscribe to the Validated event
this.nameTextBox1.Validated += nameTextBox1Validated;
and then check if the value is valid
private void nameTextBox1Validated(object sender, EventArgs e) {
if(isNameValid()) {
// clear error
nameErrorProvider.SetError(nameTextBox1, String.Empty);
}
else {
// set some helpful message
nameErrorProvider.SetError(nameTextBox1, "Invalid value.");
}
}
private bool isNameValid() {
// The logic for determining if a value is correct
return nameTextBox1.Text == "hello";
}
the error provider can be created like this
ErrorProvider nameErrorProvider = new ErrorProvider();
nameErrorProvider.SetIconAlignment(nameTextBox1, ErrorIconAlignment.MiddleRight);
nameErrorProvider.SetIconPadding(nameTextBox1, 2);
nameErrorProvider.BlinkRate = 1000;
nameErrorProvider.BlinkStyle = ErrorBlinkStyle.AlwaysBlink;
Related
I would like to have three textboxes for user input, but for each textbox I would like a different keyboard layout to be used while entering text (without the need to manually change the kayboard layout).
In textxbox 1 I would like my normal (swedish) keyboard layout to be used while I write. In the second I would like to have Japanese Hiragana layout. And in the third I would like too have Japanese Katakana layout.
Is it possible to do?
As it is now I must manually change the keyboard layout with Windowskey+Space (this cycles through all keyboard layouts).
Hope this works for you:
private void textBox1_Enter(object sender, EventArgs e)
{
// Get index of current Input Language
int currentLang = InputLanguage.InstalledInputLanguages.IndexOf(InputLanguage.CurrentInputLanguage);
// Calculate next Input Language
InputLanguage nextLang = ++currentLang == InputLanguage.InstalledInputLanguages.Count ? InputLanguage.InstalledInputLanguages[0] : InputLanguage.InstalledInputLanguages[currentLang];
// Change current Language to the calculated:
ChangeInputLanguage(nextLang);
}
public void ChangeInputLanguage(InputLanguage InputLang)
{
// Check is this Language really installed. Raise exception to warn if it is not:
if (InputLanguage.InstalledInputLanguages.IndexOf(InputLang) == -1)
throw new ArgumentOutOfRangeException();
// InputLAnguage changes here:
InputLanguage.CurrentInputLanguage = InputLang;
}
Here is the reference link - Change Input Language programmatically?
Here's the code.
private void SaveButton_Click(object sender, EventArgs e)
{
string textBoxText = TextBox.Text;
var lines = File.ReadAllLines(#"F:\Bioshock2SP.ini");
foreach(string line in lines)
{
if (line.Contains("VoVolume="))
{
//This is where I get confused.
string settingLine = line;
string replaceline = (line.Replace(line, textBoxText));
File.WriteAllText(#"F:\Bioshock2SP.ini", settingLine);
}
break;
}
MessageBox.Show("Setting saved!");
}
The idea is to replace part of a setting in a Settings.ini file for a game I play, using the user input of a textbox in my form. The user types in a number for example, "1.56" and then hit the Save button to replace the existing line with their input. In this case that setting is the volume.
The application runs completely fine, but after hitting save and going into the settings file my input isn't saved.
There should be a change in the way you save the file.
Save each line of the file as you get it, editing if required.
void SaveButton_Click(object sender, EventArgs e)
{
var textBoxText = TextBox.Text;
var lines = File.ReadAllLines(#"F:\Bioshock2SP.ini");
using (var file = new StreamWriter(#"F:\Bioshock2SP.ini"))
foreach(string line in lines)
{
if (line.Contains("VoVolume="))
file.WriteLine(line.Substring(0, 9) + textBoxText); // Writes something like 'VoVolume=1.56'
else file.WriteLine(line); // No editing required
}
MessageBox.Show("Setting saved!");
}
I think there are a couple of separate issues with your code:
Replace Function
string replaceline = (line.Replace(line, textBoxText));
Replace accepts a string to look for and a string to replace it with. Your first argument is 'line', so it would replace the whole line with the value in textBoxText. I assume you only want to replace a portion of the line with that value. In that case, you need to use something like line.Replace(searchString, textBoxText) where you have previously defined searchString as the text you want to replace. If you don't know what that value is, but there is a pattern, you might want to look into using regular expressions which will let you define a pattern to search and replace.
WriteAllText Function
File.WriteAllText(#"F:\Bioshock2SP.ini", settingLine);
This line will replace the entire contents of BioShock2SP.ini with the value in settingLine. There are two problems here.
One is that settingLine was the saved value before you did the replacement - so it has not included the results of your replace operation. You should use replaceline (assuming it has been correctly modified).
Even if you do that, though, the other is that File.WriteAllText will replace the whole file with the value in settingLine - which is probably not what you want. You'd be better off modifying the line in the array and using File.WriteAllLines to re-output the whole array - assuming the file has multiple lines in it.
The hints above may help you resolve this - to properly answer the question though, I'd need to see a sample of what the file looks like, and the patterns you are trying to replace.
I see here lot of similar question, but I still not find answer that help me in situation.
I have two frame(lets say FrameChild), one is "in" another(practically FrameChild is in this frame, lets say FrameMain).
When I insert all parameters in FrameChild and tap on button witch is on bottom of FrameMain I call method that return string...
Now when i get string i need to change textbox text in FrameChild
I have tray many way.
First idea was something like:
FrameChild frm = new FrameChild;
frm.textbox.text = "somestring";
But nothing happen.
Than i thing use some property.
in FrameChield:
public string setTicNo
{
set
{
textBox.Text = value;
}
}
in FrameMain:
FrameChild frm = new FrameChild;
frm.setTicNo = "somestring";
When i debbuging I get value, but textbox still is empty...
On the end I try to bind textbox text on setTicNo;
public string setTicNo
{
get
{
return setTicNo;
}
set
{
setTicNo = value;
}
}
Xaml:
Text = {Binding setTicNo, Mode=TwoWay,UpdateSourceTrigger=Explicit}
(here i try use more bindings, but every time i get infinite loop.
Please help , I not have more ideas..
Thanx
Did you try building a single view model and bind it to both frames, if it was passed by ref which is the default it will change the value once you do.
A side note implement a INOTIFYPROPERTYCGANGED in the View model
I'm adding validation to an existing asp.net web app built with web form. The input textbox controls I need to validate are created dynamically on the server side.
While creating the textboxes, I can also create RangeValidator controls and set its ControlToValidate to the ID of the textbox.
When validation fails, RangeValidator displays an error message at where the validation control is placed.
But I rather change the border or background-color to red of the textbox instead. How can that be done?
You can add custom logic to your validators both sever side and client side. check this link for help. http://msdn.microsoft.com/en-us/library/f5db6z8k%28v=vs.90%29.aspx
I'm not sure if you want help validating or changing the color (or both), but I just tried this little two-button app and it seems to work. Granted, I'm not using any dynamic control names and banking that "testText" is the first and only "testText" control on the page. You might also want to add some validation to ensure the control exists.
private void button1_Click(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.Name = "testText";
this.Controls.Add(tb);
}
private void button2_Click(object sender, EventArgs e)
{
TextBox tb = (TextBox)this.Controls.Find("testText",true)[0];
tb.BackColor = System.Drawing.Color.Red;
}
As Irfan said, you can use a custom validator and implement on the server validate event the logic. However, a more direct aproach would be to check if the page is valid and the check if the rangevalidator is not valid. Keep in mind to set the displaymode to none if you don't want the validator to render any message.
if(!Page.IsValid && !myRangeValidator.IsValid)
{
// simplified, you will need to search for the control in the whole hierarchy
var ctrlToValidate = Page.FindControl(myRangeValidator.ControlToValidate) as WebControl;
if(ctrlToValidate != null)
{
ctrlToValidate.BorderColor = Color.Red;
}
}
this being said, I prefer the custom validator approach. The good thing is you can almost reuse all the code above. Just remove the first if, add the range validation, set the args.IsValid = false and then use the rest of the code.
Loop over all validators and add specific CSS class to controls. I use master page, so need to find controls in content placeholder.
const string ErrorCssClass = "error";
Validate();
if (IsPostBack && !IsValid)
{
var content = Form.FindControl("MainContent") as ContentPlaceHolder;
foreach (BaseValidator validator in Validators)
{
if (validator.IsValid)
continue;
var controlToValidate = content.FindControl(validator.ControlToValidate) as WebControl;
if (controlToValidate != null && !controlToValidate.CssClass.Contains(ErrorCssClass))
controlToValidate.CssClass += " " + ErrorCssClass;
}
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to prevent richTextBox to paste images within it?
If you're using Richtextbox, there are several advantages in Richtextbox for example:
we can use color font on it
Setting custom font in a region
Attach files on it.. etc
take a look at the picture:
Here is my problem:
Can i just make it text only?
In my project, attach file or the like is unnecessary at all. I even didn't want attach or paste an images on it, i just want "text only" on Richtextbox
How can i do that?
Since RichTextBox doesn't have a Images or Objects collection you have to go for the RTF formatting codes. All data of RichTextBox is stored as plain text with special formatting codes, this is exposed by the control through its RTF property. Learning this code language is essential if you want to read or change it, learning resources are easily available throughout the web, see for example this overview. RichTextBox uses more simplified rtf codes than several full-feature editors like MS Word etc, so it is usually beneficial to load data into a RTB before manipulating it, this will remove much redundant data.
Making a long story short, I found that it is necessary to search for rtf groups that start with either "pict" or "object" command. Knowing that groups may be nested you can't just find the first end-group char from there, you have to parse the string char by char while keeping count of grouping to find the end of those groups. Now you have enough information to remove that part of the string. Rtf may contain multiple picture/object groups so you have to do this until all are removed. Here is a sample function that return rtf string after removing those groups:
private string removeRtfObjects(string rtf)
{
//removing {\pict or {\object groups
string pattern = "\\{\\\\pict|\\{\\\\object";
Match m = Regex.Match(rtf, pattern);
while (m.Success) {
int count = 1;
for (int i = m.Index + 2; i <= rtf.Length; i++) {
//start group
if (rtf(i) == '{') {
count += 1;
//end group
} else if (rtf(i) == '}') {
count -= 1;
}
//found end of pict/object group
if (count == 0) {
rtf = rtf.Remove(m.Index, i - m.Index + 1);
break; // TODO: might not be correct. Was : Exit For
}
}
m = Regex.Match(rtf, pattern);
//go again
}
return rtf;
}
When should this be done? You have already mention Paste, there is also Insert, these can be trapped with the KeyDown event where you get the clipboard info and handle it accordingly. Setting e.Handled=True when you have handled the operation yourself signals that the control should not do any default processing for this key combination. This is also how you block pasting images without destroying the users clipboard. Example:
private void RichTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
//aware of Paste or Insert
if (e.Control && e.KeyCode == Keys.V || e.Shift && e.KeyCode == Keys.I) {
if (Clipboard.ContainsImage || Clipboard.ContainsFileDropList) {
//some images are transferred as filedrops
e.Handled = true;
//stops here
} else if (Clipboard.ContainsData(DataFormats.Rtf)) {
RichTextBox rtbox = new RichTextBox();
//use a temp box to validate/simplify
rtbox.Rtf = Clipboard.GetData(DataFormats.Rtf);
this.RichTextBox1.SelectedRtf = this.removeRtfObjects(rtbox.Rtf);
rtbox.Dispose();
e.Handled = true;
}
}
}
Yes, it is possible.
Handle Ctrl+V in RichTextBox1_KeyDown, then check the data format in the Clipboard: if data is plain text, paste it; if data is RTF, convert it to plain text (in a buffer without changing the Clipboard content) and paste it; don't paste any other type of data.
This is a partial example just to show you how to proceed:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.V)
{
// suspend layout to avoid blinking
richTextBox2.SuspendLayout();
// get insertion point
int insPt = richTextBox2.SelectionStart;
// preserve text from after insertion pont to end of RTF content
string postRTFContent = richTextBox2.Text.Substring(insPt);
// remove the content after the insertion point
richTextBox2.Text = richTextBox2.Text.Substring(0, insPt);
// add the clipboard content and then the preserved postRTF content
richTextBox2.Text += (string)Clipboard.GetData("Text") + postRTFContent;
// adjust the insertion point to just after the inserted text
richTextBox2.SelectionStart = richTextBox2.TextLength - postRTFContent.Length;
// restore layout
richTextBox2.ResumeLayout();
// cancel the paste
e.Handled = true;
}
}