I want to Empty The Text which is by default written in the textbox , i can empty it by doing
Textbox1.Text="";
but i want to know, which event should i exactly use ?will it be GotFocus,KeyEnter,Keyup,Tap or any other method .
One simple way is using on GotFocus and LostFocus. On GotFocus the code should be something like this:
if (textbox1.Text == "My default text...")
textbox1.Text = "";
Just to make sure to no delete a text previously entered by the user. And on LostFocus just something like this:
if (String.IsNullOrWhiteSpace(textbox1.Text))
textbox1.Text = "My default text...";
So again you are checking to no delete a user entered text.
Hope this helps
You can use PhoneTextBox of WPToolkit, which will maintain that functionality on its own
Related
Whenever a user click's on the checkBox controls, the text in the checkBox will be appeared on the richTextBox.text and it wasn't so hard because all I needed was :
if (CBSefalexin.Checked == true)
richTextBox1.Text += CBSefalexin.Text;
But now, I want to do something. Whenever user unchecked the checkbox, I want to delete that text (which recently added up to richtextBox.text) from richTextBox. It is obvious that I cannot use the code below:
if (CBSefalexin.Checked == true)
richTextBox1.Text += CBSefalexin.Text;
else
richTextBox1.Text -= CBSefalexin.Text;
Because it's impossible to use "-" for strings. But I want to know if I can write that code in my project. Any suggestion so far ?
Thanks in advance.
UPDATE
I do not want to clean the whole of the text. I just want to delete that specific text which I just added up to richTextBox(Because I have so many checkboxes that I want to do the same things with them)
richTextBox1.Text = richTextBox1.Text.Replace(CBSefalexin.Text,"")
This should replace just the text hat you initially set
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 . :)
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!
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
}
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