vpv
vpv

Reputation: 938

Why text box text not showing from starting position in c#?

I am really confused about this. Please consider below scenario.

Scenario:
I have a C# Winform app with few Textbox controls. Now when I input data in these text boxes, for example, "THIS IS MY SAMPLE TEXTBOX", which overlaps the Textbox's visible area and displaying as "AMPLE TEXTBOX". But I want the text to be displayed from the starting position like "THIS IS MY S" and then if needed, overlaps. How can I do this? I have tried below but no luck. Please help. Thanks.

(sender as TextBox).TextAlign = HorizontalAlignment.Left;

Edit
I am also using AutoCompleteMode.Suggest so that when I press any key, corresponding list will be displayed similar to dropdownlist. But the first item of this list is selected by default, which I do not want. Can you please suggest in this also. Thanks.

Final Solution
I am using this to solve the issue

(sender as TextBox).TextAlign = HorizontalAlignment.Left;
(sender as TextBox).Select(0, 0);

Thanks to @Har Har.

Upvotes: 1

Views: 1604

Answers (1)

Mogli
Mogli

Reputation: 2012

I found the solution, To position the cursor at the beginning of the contents of a TextBox control, call the Select method and specify the selection start position of 0, and a selection length of 0.

private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Text = "Hello this is a sample application";
        textBox1.Select(0, 0);
    }

It will show the cursor position at 0 index, It's working.

Upvotes: 3

Related Questions