Sorry if this question is a dumb one, but I'm new in C# so..
How can I validate text that the user writes in a maskedTextBox with the OK button
E.g. The user enters the program and needs to enter a password to have access. If the password is correct, he has access and the main window apppears. + Enters "Sample" into maskedTextBox 1.

Validation
lord_8
Your question seems to be skirting two areas.
1. I'm not sure why you're using a MaskedTextBox control. Typically, such a control is used when the input needs to conform to some pattern, such as a number or a date. It is pretty rare to see a MaskedTextBox control used to handle password input. Instead, a TextBox is used with the password character set to an asterisk so that the characters are not visible.
2. The validation of the password would take place in the Click event of the OK button. This event is raised when the button is clicked by the user and, I suspect, is the most appropriate place for the validation to take place.
Hope this helps.
Rogge
I'll start with the second one first. You could certainly use Compare to validate the passwords, but unless the password is culturally dependent, it might be overkill. textBox1.Text == "password" would work as well.
As for the first question, the technique is to have Form1 expose a public property called TextBox1Value. The implementation of TextBox1Value would be to either assign (for the set) or retrieve (for the get) the TextBox1.Text property. In that manner, Form2 can get an instance of Form1 and assign the value "Sample" as needed.
The challenge here is to get the current instance of Form1. The specifics are going to depend on how Form1 and Form2 get created and instances get passed back and form.
Hope that helps.
Alex Dresko (MVP wannabe)
Thanks, both answers helped.
But my question is more about how can Form2 control stuff in Form1
I have 2 forms. And I'd like to write "Sample" to TextBox 1 on Form1 when I click Button 1 on Form2.
And another question, will String.Compare work for password validation
Like this:
private void button1_Click(object sender, EventArgs e)
{
string CorrectPassword = "password";
string EnteredPassword = textBox1.Text;
String.Compare(EnteredPassword, CorrectPassword);
}
pyeung
And, just so you know, the problem with Form1.textBox1.Text is two-fold:
1. textBox1 is probably not a static property exposed by the Form1 class. It's an instance property, which means that it needs to be accessed through an instance of Form1.
2. textBox1 is probably not publically accessible. It if is, it shouldn't be. Instead you should expose properties that allow external classes to manipulate the TextBox indirectly.
Alan Robbins
My apologies for double-post.
I realised it's better to use if for this one, but it's not working.
private void button1_Click(object sender, EventArgs e)
{
string CorrectPassword = "password";
if (textBox1.Text == CorrectPassword)
Form1.textBox1.Text = "SAMPLE";
else
Form2.Close();
}