How to delete selected text from textbox in c#(SWF) - c#

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 = "";

Related

Clicking button to send info in text box not working

I try to make this: static private long number = (long)Math.Floor(GlobalRandom.NextDouble * 9_000_000_000L) + 1_000_000_000L; to be returned with this:
public string MyValtwo
{
get { return myValtwo; }
set { myValtwo = value; }
}
and finally to be sent to this:
private void button1_Click(object sender, EventArgs e)
{
MyValtwo = textBox2.Text;
}
I tried converting: public string MyValtwo = Convert.ToString(number); but nothing returns or the long (number) can't be converted into string.
The main idea is to be generated a number and automatically putted in form 1 text box
You dont need MyValtwo. Change code to below and it will work:
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = number.ToString();
}
In case you want to store the value of number as text in MyValtwo then you can add below line in button1_Click function.
MyValtwo = number.ToString();

How do I check the contents of a TextBox?

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;
}
}

How to copy, paste, cut in FastColoredTextBox?

I need three functions: Copy, Paste, Cut ,
For a FastColoredTextBox.. so far with my methods, the job is done but afterwards,
the cursor's position get changed and I got no clue on how to keep it where it
was before.
Here's my methods:
private void OnMouseMenuCut(object sender, EventArgs e)
{
var sPoint = rtbScript.SelectionStart;
var ePoint = rtbScript.SelectionLength;
var text = rtbScript.SelectedText;
rtbScript.Text = rtbScript.Text.Remove(sPoint, ePoint);
Clipboard.SetText(text.Replace("\n", "\r\n"));
rtbScript.Text = rtbScript.Text.Insert(sPoint, text);
}
private void OnMouseMenuCopy(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(rtbScript.SelectedText)) return;
Clipboard.SetText(rtbScript.SelectedText.Replace("\n", "\r\n"));
}
private void OnMouseMenuPaste(object sender, EventArgs e)
{
if (!Clipboard.ContainsText()) return;
var index = rtbScript.SelectionStart;
rtbScript.Text = rtbScript.Text.Insert(index, Clipboard.GetText());
}
Also, If there's a better way to do those functions, please post..
Thanks!
For a RichTextBox your code has more issues than loosing the Cursor position, It also looses all formatting! Here are versions that should work better:
private void OnMouseMenuCut(object sender, EventArgs e)
{
var sPoint = rtbScript.SelectionStart;
var text = rtbScript.SelectedText;
rtbScript.Cut();
Clipboard.SetText(text.Replace("\n", "\r\n"));
rtbScript.SelectionStart = sPoint;
}
private void OnMouseMenuCopy(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(rtbScript.SelectedText)) return;
Clipboard.SetText(rtbScript.SelectedText.Replace("\n", "\r\n"));
}
private void OnMouseMenuPaste(object sender, EventArgs e)
{
if (!Clipboard.ContainsText()) return;
var index = rtbScript.SelectionStart;
rtbScript.SelectedText = Clipboard.GetText();
rtbScript.SelectionStart = index + Clipboard.GetText().Length;
}
Note: You must never change the Text property of a RTB or else you will mess up the formating!
Since you wrote that this also works with your FastColoredTextBox I have undeleted the solution..
In the current version of FCTB, these methods already exist inside the FCTB.cs file. They just need to be linked up.

Messagebox.Show doesnt work because its a method group? C#

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;
}

How to obtain values from multiple Combo Boxes

As the titles suggest I am interested in obtaining the value from 4 combo boxes. All 4 combo boxes are the same in the that they list numbers from 0-9. I would like to take those numbers and assigning them to a single string variable. So for example if users selects (CB1 = 4)(CB2 = 3) (CB3 = 2) (CB4 = 1) I would like to take those selection and assign them to a string variable. Thanks in Advance.
-Nogard
if you are using winforms
string s"";
private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
private void combobox2_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
private void combobox3_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
private void combobox4_SelectedIndexChanged(object sender, EventArgs e)
{
s = combobox1.Text+ combobox2.Text+ combobox3.Text+ combobox4.Text;
}
or call only a selected index change event in all comboboxes since all are doing same

Categories

Resources