Hi I'm very new to C# and I was just writing a very basic code could anyone help me out? I am trying to simply show a variable (inputted by the user via the textbox) show up in the message box... Thanks :)
private void button1_Click(object sender, EventArgs e)
{
string name1 = textBox1.Text;
MessageBox.Show = (name1);
textBox1.Text = ("");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string name1 = textBox1.Text;
}
It's a method, not a property, so you need to call it using this syntax
MessageBox.Show(name1);
Related, textBox1.Text is a string property so you need to change that syntax to
textBox1.Text = "my string";
Your click event could be rewritten as this:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(textbox1.Text);
textBox1.Text = "";
}
and this section of code doesn't actually have any significance. name1 is only in scope inside the text changed event handler, and you do nothing with it.
private void textBox1_TextChanged(object sender, EventArgs e)
{
string name1 = textBox1.Text;
}
Related
I have a textbox. In that textbox I write Human. Then I click a button, and if the word is human, then on a richtextbox, the word human will appear.
Here's the code I've tried.
private void button3_Click(object sender, EventArgs e)
{
if (textBox4.Text)
{
richTextBox1.Text = "human";
}
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
string value = textBox4.Text;
}
I tried making the textbox into a string so I could use it in the if statement, but it didn't work, so instead I used texbox4.text, but it is still wrong.
You could simply do with this piece of code,
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text =="human")
{
richTextBox1.Text = textBox1.Text;
}
}
i am trying to use SendKeys.Send() to send textbox1.Text to a application ( like notepad ), and i want to disable or remove spaces from the textbox so there is no spaces in the application.
Any help is appreciated!
private void InfoSend_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
SendKeys.Send(input_info);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
input_info = textBox1.Text;
}
If user can input white spaces to TextBox then you can write this:
private void InfoSend_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
input_info = input_info.Replace(" ", string.Empty);
SendKeys.Send(input_info);
}
I tried TextBox1.Clear but it doesn't work and gives me error.
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Clear
}
Do this way,
TextBox1.Text = string.Empty;
This could be one way to do it:
TextBox1.Text = string.Empty;
I try to do a notepad in c#,
I have some problems at delete function,
I want to delete selected text...
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a;
a = textBox1.SelectionLength;
textBox1.Text.Remove(textBox1.SelectionStart,a);
}
what is wrong?
Remove will return the truncated string, so you just need to reassign to the TextBox:
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a = textBox1.SelectionLength;
textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart,a);
}
Use SelectedText like this :
textbox1.SelectedText = "";
I have a query regarding with TextBox.
When I type in the text box the words automatically changes. for example: "my name is kumar" to "My Name Is Kumar" and should be done on textBox1_TextChanged event.
currently i am doing this on Leave event
private void textBox1_Leave(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.Substring(0, 1).ToUpper() + textBox1.Text.Substring(1);
}
please help me to accomplished it. lots of thanks in advance. :)
Use TextInfo.ToTitleCase Method
private void textBox1_Leave(object sender, EventArgs e)
{
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
textBox1.Text = myTI.ToTitleCase(textBox1.Text)
}
As a followup to the previous answer, if you add the following lines to the remainder of the body you'll ensure the correct behavior is maintained:
textBox1.SelectionStart = textBox1.TextLength;
textBox1.SelectionLength = 0;
So the full solution would be:
private void textBox1_Leave(object sender, EventArgs e)
{
//Original from JW's answer
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;
textBox1.Text = myTI.ToTitleCase(textBox1.Text);
//New lines to ensure the cursor is always at the end of the typed string.
textBox1.SelectionStart = textBox1.TextLength;
textBox1.SelectionLength = 0;
}
This should solve your problem:
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
textBox1.Text = myTI.ToTitleCase(textBox1.Text);
textBox1.SelectionStart = textBox1.Text.Length;
}
I would use a Regex here, since it's simpler to implement and I don't think your TextBox will hold large strings. Since you want the string to be auto-corrected as you write, you need the TextChanged event instead of the Leave one:
private void textBox1_TextChanged(object sender, EventArgs e)
{
Regex regex = new Regex(" [a-z]");
foreach (Match match in regex.Matches(textBox1.Text))
textBox1.Text = regex.Replace(textBox1.Text, match.Value.ToUpper());
}