I've been doing this function, and i would like to ask, whats the problem behind this one? I Made if statement, and nothing happens, the chad doesn't change, i tried to make Application.Exit on the if function, and the app exited. I wanna to make that Show Password/Hide Password function on MetroTextBox
if (metroTextBox1.PasswordChar == '●')
{
metroTextBox1.PasswordChar = '\0';//
}
else
{
metroTextBox1.PasswordChar = '●';
}
I wanna to make that Show Password/Hide Password function on
MetroTextBox
void showPassword(TextBox textBox, bool show)
{
if(show)
textBox.PasswordChar = '\0';
else
textBox.PasswordChar = '*';
}
Now, anytime you want to show the password, call it like this:
showPassword(metroTextBox1, true);
Hide the password:
showPassword(metroTextBox1, false);
You can call this from a toggle box.
Use the text changed method of your textbox and to capture new inputs froms the user and validate if the key pressed corresponds to a printable character. You can apend this to a local variable and clear the text field, this way you capture the password while keeping the field empty. I think this is the idea you have
Have you tried to add button_Click ? Double click on the button ,and it will be automatically created, then you just have to paste your code!
Sorry if i was bad, but it's working fine for me!
Related
i'm having a problem . I'm trying to make an application in C# (Windows Form Application) and i'm stuck into something.
I want to do a log-in form like in the photo. For username i'm having a combo-box where i can choose the user , but for password i want to place some buttons(like in the photo) , and when i press the button 1 for example , i want to have a string which is 1 .After that i will place 2 ,and i want to concatenate 1 and 2 into that string . Do you know how can i do that
In your case, I believe you have one event for each button. In each event you could just sum the values to a global variable or directly change the textbox. Like this:
txtPassword.Text += "0"; //the 0 button
It will change the textbox value and when you click "Sign in" you can get the txtPassword.Text as the final string.
You can also declare a string or StringBuilder globally and append the value in every button click, tho this won't change the textbox text directly.
Use a StringBuilder to concatenate strings.
StringBuilder builder = new StringBuilder();
builder.Append(a);// where "a" is your input character
And whenever you get a new character input call builder.Append(a);
In the end you get your final string by calling builder.ToString();
As a variation on the answer from #LeoFormaggi, you could have a single event handler for all the numeric buttons (i.e., one that every button's click handler points to). Then, in the handler:
var buttonText = (sender as Button)?.Text;
txtPassword.Text += buttonText ?? String.Empty;
Don't forget to make that textbox be a password field (by setting the PasswordChar property).
You don't want to bother with a StringBuilder here. It's normally the right tool for concatenating strings, but it buys you nothing in this case since you want the result to appear immediately in the text box. In this case it would simply add overhead for no good reason.
i want to thank you, i made my solution like this :
`private void nb6_Click(object sender, EventArgs e)
{
password_box.Text += "6";
}`
in every button event that i have, the password_box (which is my textbox) and i concatenate everytime the string that i want(in this case is number 6 )
Thank you and have a nice day . :)
Good day guys, I checked for previous question, yes i saw them, but it was not really solving my problems.
I am writing an application that has two forms, there is a mainForm and Password form.
The form is for scanning two code and match if they are equal. you scan the first code and then the second code. If they Match it pop-up the green lights to show that they match. If the code does not match it pop-up a red light and a Password form at the same time and locks the form textbox to ReadOnly for supervisor to verify what the user has scanned.
It is doing all this.
The problem is i want when the supervisor enter Password it must show the form again so that the user can continue scanning, it is doing this. But the thing is when it open a new form, the one at the back that was locked it does not disapear. i tried to reset the form, it does not work. Please help in this.
//object references of the Main Form
MainForm fm=new MainForm();
if ((txtPassword.Text =="This"
{
fm.BringToFront();
//Reset the form to a normal state
fm.lblResult.Visible = false;
fm.txtResult.Visible = false;
fm.chkMtn.Checked = false;
fm.chkVodacom.Checked = false;
//Clear the textBox of the form
fm.txtMainFormScan1.Text="";
fm.txtMainFormScan2.Text="";
set the focus on the fist Scan
fm.txtFirstScan.Focus();
}
The code above does not work. now i am using the one below.
if ((txtPassword.Text =="ThisPassword")
{
lblInstructPassword.Text = "Password correct";
txtPassword.Text = "";
fm.ControlBox = true;
//fm.Activate();
this.Hide(); //This Close the Password form
fm.Show();
fm.chkMtn.Checked = true;
}
Even when i use the //fm.ShowDialog, i cannot win. I also tried to use the Mutex to allow only one instance of an application to run. result are Zero. Thanks for your help in advance.
You're doing a lot of work that .Net should be doing for you.
All else being mutable details, your real problem is this:
You have a working form that compares two values.
If the comparison result is false, you need a modal window to appear to prompt for a password.
If the password is correct, you need the first form to be editable again and the password form to disappear.
You were on the right path using ShowDialog(), as this does disable the window that fires the command to show the dialog. You need your password logic to be a part of that dialog form, not the parent form. This will allow you to return a DialogResult enum value to the parent form (if any value at all) and get you around the needless Focus() tossing.
If you want the calling form to disappear and reappear when you do this, wrap the call to your form's ShowDialog() call in this.Hide() and this.Show()
I googled a few things before posting, but I couldn't find anything like this. Basically, I want to take text from a textbox, save as a variable (say history1) to then be able to call that in the future to display the text. I can do that, but what I'm stuck with is that I want 3 variables (history1, history2 and history3, for example) and each time the button is pressed the string is moved to the next variable.
For example, the button is pressed, the text is saved as variable history1. The text is changed and the button is pressed again, the text from history1 is moved to variable history2, and the new text is saved as history1. This would only need to work for 3 instances though, not infinitely, so when text is stored in history3 and the button is pressed the text is just overwritten.
The way I had thought of approaching this was:
string history1;
string history2;
string history3;
for (int i = 1; i < 4; i++)
{
history1 = txtOutput.Text;
btnToFile_Click()
{
history2=history1;
btnToFile_Click()
{
history3=history2;
}
}
}
However, this isn't going to work because the btnToFile_Click doesn't take any arguements. Is there an easier way to go about this or just a way to fix the method not taking arguements ?
Thanks in advance!
Make sure that you delcare history1, history2, and history3 on the form level (not inside any method).
Then, have the following code inside the handler of the click event of the button:
history3 = history2;
history2 = history1;
history1 = txtOutput.Text;
You don't need to call the btnToFile_Click() method multiple times in your loop, just move the text from end textbox to another in reverse order. Nor do you need a loop because you only have three textboxes.
Why reverse order? So you move the value to the next textbox before it is overwritten by the new value.
So:
history3 = history2;
history2 = history1;
history1 = txtOutput.Text;
btnToFile_Click() is a Click event handler for btnToFile (a button). You're not supposed to call that method yourself, it's called by the UI framework (say WPF or WinForms etc.). By the way, it does receive a parameter, then event source (since you can assign the same event handler to multiple buttons and do something based on which one sent the event)
You can try saving in a string array and move the strings within it when you call the button clicked event
I'm trying to add text to a RichTextBox using the AppendText method, and would like to find a way to Not take focus of the text box in this motion - reason being that I have an event response to the text box getting focus, that causes a conflict in my overall scheme...
Again, the question here is effectively; How can I use the AppendText method without triggering focus on a rich text box.
As I'm typing this I've almost decided that I can remove my event response method before the append and add it in again after; but if anyone has a better suggestion I'm all ears.
Thanks. And if I can submit any code to spur suggestions I'm open to it; I just assume that most anyone using this site can visualize what I'm portraying.
You can use a boolean variable to determine if it was you who fired the event (or the user)
bool firedByUser ;
When calling the AppendText method do something like this
firedByUser = false ;
rtb.AppendText("sample") ;
firedByUser = true ;
And in the method that you are handling the Focus on the RichTextBox
if(firedByUser)
{
//keep doing what you are doing now
}
Using Windows Mobile 6.5 and C#
The CharacterCasing property seems to be missing from WinMo 6.5 and I decide to just catch the textchanged event and set the text with ToUpper.
This works - but on every keypress, it sends the cursor back to the beginning of the string, not the end.
I know this is old, but hopefully this can help somebody else. I implemented the KeyPress event like the following.
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.KeyChar = Char.ToUpper(e.KeyChar);
}
Ritu, just to comment on your answer. You should keep in mind that this might be confusing for a user if the user has positioned the caret in the middle of the string to perform some edit and then the caret jumps to the end of the string on the key press.
An alternative might be to change the text to upper case when the edit control loses focus.
Try here such implementation.
public MainForm()
{
InitializeComponent();
InitializeUpperCaseTextBox();
}
private void InitializeUpperCaseTextBox()
{
txtbox.CharacterCasing = CharacterCasing.Upper;
//... etc.
}
The soluttion of setting the text position to the end of the string seems like would be a hassle if you ever need to edit text that you have already entered.
It's been a while since I thought about the C# event model but, one alternative might be to catch the KeyPress event and change any lowercase KeyChar values to uppercase before passing them on to the next handler.
The way you are approaching seems to be wrong.
There are so many different ways to insert data into that textbox. What about copy & paste for instance?
Just perform a .Text.ToUpper() when accessing the value of the Textbox
Save the SelectionStart and SelectionLength before changing the text. The ToUpper should make no changes to the length, so you can simply set the SelctionStart and SelectionLength back to what they had been.
Also, I would expect to get a changed event again when you set it ToUpper. I'm not sure if you also need to check that the ToUpper actually changed anything before you set Text again. It may be smart enough to check that for you when you assign the text and avoiding giving you an infinite recursive loop of change events. However, you probably don't want to alter the selection in the event handler call for the case where you aren't making a further change, only in the outer call where you are assigning back to Text. So you might as well guard recursion directly.
Something like:
bool m_InMyTextChanged = false;
private void txtMyText_TextChanged(object sender, EventArgs e)
{
if (m_InMyTextChanged)
return; // Recursive! We can bail quickly.
m_InMyTextChanged = true; // Prevent recursion when we change it.
int selectionStart = txtMyText.SelectionStart;
int selectionLength = txtMyText.SelectionLength;
string originalText = txtMyText.Text;
string newText = originalText.ToUpper();
if (newText != originalText)
{
txtMyText.Text = newText; // Will cause a new TextChanged event.
// Set the selection back *after* the assignment, which has reset them.
txtMyText.SelectionStart = selectionStart;
txtMyText.SelectionLength = selectionLength;
}
m_InMyTextChanged = false; // Allow it for next time.
}
could work. I haven't worked in Windows Mobile, but I would think this would work the same as in general for .NET.
I figured it out. So on the textChanged event, I replace the entered text with the ToUpper version. Then I set the SelectionStart property to the Text.Length to move the cursor to the end.