tags:

views:

528

answers:

5

i have 10 textboxes and i need to select text from each one of them. The problem is that i cant select text from multiple textboxes. Is there any solution for this problem my code is.

 private void Form1_Load(object sender, EventArgs e)
    {
        createTextBoxes(10);

        ((TextBox)textBoxes[0]).Select(1, 4);
        ((TextBox)textBoxes[1]).Select(1, 4); // <- it will not select text 
        ((TextBox)textBoxes[2]).Select(1, 4); // same here
    }
    Control[] textBoxes;
    private void createTextBoxes(int cnt)
    {
        textBoxes = new Control[cnt];
        for (int i = 0; i < cnt; i++)
        {
            TextBox tb = new TextBox();
            tb.Name = i.ToString();
            tb.Location = new Point(5, 5 + 14 * i);
            tb.Size = new Size(600, 20);
            tb.BorderStyle = BorderStyle.None;
            tb.Text = "sample text" + i.ToString();
            textBoxes[i] = tb;
            this.Controls.Add(tb);
        }
    }
A: 

This is possibly not working because even though you've added the TextBox instances to the Form, they have not yet been displayed. Until they are displayed and initially rendered it's likely not possible to initiate a selection on them.

JaredPar
no i can select from first textbox but not from others
Woland
A: 

Only one control can have a "Focus" at a time... you can't select (ie:highlight) text of multiple controls.

I also just tested by adding a button to the form and posted your 3 "select" snippets there too... nothing showed highlighted. However, when I did a TAB through each control, the first 3 respectfully showed the highlighted section. When I tabbed through the rest, the entire field of the rest of the textboxes were fully selected.

Or are you really trying to accomplish something else...

DRapp
i trying to creat rectangular text selection
Woland
+1  A: 

The text is selected you just can't see it cause of focus. I ran your code and after doing so tabbed through the controls. The first 3 are selected as specified.

j0tt
A: 

Actually it does, the problem is that the other 2 of your textboxes ([1] and [2]) don't have focus. Only one control can have focus at a time. If you hit tab to give focus to the next TextBox, you'll see the text selected.

Carlo
+3  A: 

Set the HideSelection property of the texboxes to false. They will maintain selection after losing focus.

Eric