Bassem Essam
Bassem Essam

Reputation: 13

Get index of repeated char in string

I have a textbox which contains the text "aaaaaa". If I select any "a" with the mouse it returns index of selected text. However I allways get zero for that index.

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = Convert.ToString(textBox1.Text.IndexOf(textBox1.SelectedText));
}

can anyone help me to get the real index ?

Upvotes: 1

Views: 140

Answers (1)

juharr
juharr

Reputation: 32266

I believe what you want is the TextBox.SelectionStart property. That will give you the index in the text box where the selection starts.

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = textBox1.SelectionStart.ToString();
}

Upvotes: 3

Related Questions