Hi - I'm writing a program the changes what you cut and paste. One problem is that it doesn't delete the text you cut...

This is the function i'm using to delete text..

//Deleting selected text
        public string deleteSelected()
        {
            string ret = "";
            string str = textBox1.Text;
            MessageBox.Show(str);
            for (int i = 0; i <= textBox1.Text.Length - 1; i++)
            {
                if (!(i >= textBox1.SelectionStart && i < textBox1.SelectionStart + textBox1.SelectionLength))
                {
                    ret += str[i];
                }
            }
            MessageBox.Show(ret);
            return ret;
        }

and here is the code where i'm using the function:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.A) //Select all
            {
                textBox1.SelectAll();

            }
            if (e.Control && e.KeyCode == Keys.C) //Copy
            {
                Clipboard.SetText(textBox1.SelectedText);
                
            }
            if (e.Control && e.KeyCode == Keys.X) //Cut
            {
                string afterDelete = deleteSelected();
                MessageBox.Show(afterDelete);
                textBox1.Text = afterDelete;
                Clipboard.SetText(textBox1.SelectedText);
            }
        }

When I run the compiler I receive no errors, but when debugging I get this "Un-handled exception has occurred in your application." it also says "Value cannot be null. Parameter name: text"

I don't know why it thinks the i'm setting the text to null! Please help!

(and if you find a built in function for deleting text - please let me know!)

Duh - never mind, problem solved! I just had to rearange the code. The deleteSelected method had to be after I put the text on the clipboard!

The String class has a Remove method. It can remove chars from a certain position and of a certain length.

Thanks. that works!

Here is how to cut a section out of a std::string

int main()
{
    string str = "Now is the time";
    str.erase(4,3);
    cout << str << "\n";
}

In your code, instead of 4,3 str.erase(textBox1.SelectionStart,textBox1.SelectionLength); [edit]Too late -- already suggested :)[/edit]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.