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
Related
For some reason, this one will not work:
if (richTextBox2.Text == "settings")
panel5.Visible = true;
The other inputs work like "close", inputting close will close the form.
What i want to happen is when "settings" is typed into the text box, i want the panel to become visible.
You could try comparing in this way:
if (richTextBox2.Text.ToUpper() == "SETTINGS")
panel5.Visible = true;
Other way to try, is debug "if" line, and check the content of Text property. Maybe, richTextBox2.Text contains some hidden characters.
I hope this could help you.
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 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
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
Basically, I am creating a button in an oval shape. But my button label is too long to display in one line, so I wanted to split it into multiple lines so that the oval button looks good.
How do I enable word wrap on a button?
If you want to set a button's label to multi-line text inside the VS designer, you can click on the "down arrow" at the right of the property field and then you are able to enter multiple lines of text.
I tried this in VS 2015.
Set the label text on form load and add Environment.Newline as the newline string, like this:
btnOK.Text = "OK" + Environment.NewLine + "true";
Just add a newline in the text at the place where it should split.
Try to add "\n" to button's Text property in the places you want to wrap.
There are two options:
If you are creating a custom control, then place a label control on it with the Autosize = true option. And adjust its size as per the buttons size.
Add a new line wherever you want (a bit crude).
You can create custom Button with one additional property (say, Label) which converts "\n" occurrence into "real" newline (because VS designer cannot do it already 10 years):
public string Label
{
get { return (string.IsNullOrEmpty(Text) ? Text : Text.Replace("\n", #"\n")); }
set {
Text = (string.IsNullOrEmpty(value) ? value : value.Replace(#"\n", "\n"));
}
}
Once you created such class, your SuperButton will be visible in Toolbox at Project page, so you don't loose visual way of design.
You just need to insert a line break (i.e. \n) in the button text.
Example:
Button1.AutoSize = true;
Button1.Text = "This is \n The Button Text";