Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2007

Tip: Looking for answers? Try searching our database.

setting focus on a winform

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AVL - 11 Jun 2007 08:46 GMT
Hi,
I want to set focus on a textform when my form is displayed to the user.
I've set tablndex of textbox to 0 and tried the
textBox1.Focus()..but still the text in the textbox is not being
hightlighted...

how shud i proceed
DArnold - 11 Jun 2007 10:05 GMT
> Hi,
> I want to set focus on a textform when my form is displayed to the user.
[quoted text clipped - 3 lines]
>
> how shud i proceed

You go to the TextBox Properties/Events Icon/Focus/Enter and you give
the event a name like Textbox1_GotFocus or prefix it with whatever the
name of the textbox is. In the event, you call the TextboxHighlight
routine, giving it the Textbox control.

You can de-select the highlight by setting SelectionLength = 0

        private void TextBox1_GotFocus(object sender, EventArgs e)
        {
            TextboxHighlight(textBox1);

        }

        private void TextboxHighlight(TextBox cntrl)
        {
            int ilen = cntrl.Text.Length;
            cntrl.SelectionLength = ilen;
        }

Then all you have to do is Textbox1.Focus, and if there is text in the
Textbox, it's going to be highlighted.
Herfried K. Wagner [MVP] - 11 Jun 2007 20:14 GMT
"AVL" <AVL@discussions.microsoft.com> schrieb:
> I want to set focus on a textform when my form is displayed to the user.
> I've set tablndex of textbox to 0 and tried the
> textBox1.Focus()..but still the text in the textbox is not being
> hightlighted...

In addition to the other reply, note that 'Focus' will fail if the form is
not yet visible.  You may want to use 'Select' instead.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.