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 / April 2005

Tip: Looking for answers? Try searching our database.

How to give initial focus?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Axel Dahmen - 21 Apr 2005 02:24 GMT
Hi,

I've programmed a simple form having some panes and one button. There is a
basic thing I can't find a solution for:

1) I gave the button a TabIndex of 0.
  The main pane has a TabIndex of 1.
  Still the button doesn't get the focus when the form opens.
  Why?

  I tried to give focus to the button at Form_Load(), but CanFocus is false
at that time.

TIA,
Axel Dahmen
Marvin Varela - 21 Apr 2005 17:25 GMT
I assume you are using the TabControl.

This seems to be a bug, because the button gets the focus simply by
assigning the tab index when outside the tab page. When the button is inside
the tab page the only way i could find to set its focus is the follosing
(but i don't like it at all...).
private void Form1_Load(object sender, System.EventArgs e) {

   tabControl1.SelectedIndex=1;

   tabControl1.SelectedIndex=0;

}

private void tabControl1_SelectedIndexChanged(object sender,
System.EventArgs e) {

   if(tabControl1.TabIndex==0) button1.Focus();

}

Despite the appearences it doesn't visually show the change between the tab
pages, but displays the main page selected from the beggining and the button
with the focus. I know it is not very clean, but it works and it seems an
acceptable workaround if this is really a bug.

Please someone correct me if this is not a bug or if there is some other
(cleaner) way to achieve this.

> Hi,
>
[quoted text clipped - 11 lines]
> TIA,
> Axel Dahmen
Axel Dahmen - 21 Apr 2005 18:16 GMT
Thanks for trying to help me.

I'm not using a Tab control. I was referring to the TabIndex property of the
Button control (myBtn.TabIndex = ...)

The tab index determines the sequence of controls within the same hierarchy
level getting focus when hitting the <TAB> key.

Axel

-------
> I assume you are using the TabControl.
>
[quoted text clipped - 41 lines]
> > TIA,
> > Axel Dahmen
Stoitcho Goutsev \(100\) [C# MVP] - 21 Apr 2005 23:33 GMT
Alex,

Can focus is false because the all controls are not visible at the moment
the Load is being raised.

If you want to specify teh button that will have initialif the focus at this
very moment you can assign the button to the ActiveControl property. It also
works if you set the tab indices correctly
You say that  the  main pane has index 1 it makes mi thing that something
else has index 0 and this is what gets the focus.

Signature

Stoitcho Goutsev (100) [C# MVP]

> Thanks for trying to help me.
>
[quoted text clipped - 59 lines]
>> > TIA,
>> > Axel Dahmen
Axel Dahmen - 22 Apr 2005 00:16 GMT
Thanks, Stoitcho, it works!

I'm curious: So "ctrl.focus()" and "ActiveControl = ctrl" are more or less
equivalent?

TIA,
Axel Dahmen

-----------------
"Stoitcho Goutsev (100) [C# MVP]" <100@100.com> schrieb im Newsbeitrag
news:#A$NUIsRFHA.3560@TK2MSFTNGP14.phx.gbl...
> Alex,
>
[quoted text clipped - 73 lines]
> >> > TIA,
> >> > Axel Dahmen
Axel Dahmen - 23 Apr 2005 02:11 GMT
I don't get it: Sometimes it works, but most of the time it doesn't....

You wrote that there might be more than one control having TabIndex = 0.
This is only true for controls contained within a panel. This should be
correct, shouldn't it? Each panel creates its own TabIndex "scope", doesn't
it?

I remember a designer feature in VB6 and VC++6 creating correct TabIndex
values for me. Isn't a thing like that available in VS7?

TIA,
Axel Dahmen

-----------------
"Stoitcho Goutsev (100) [C# MVP]" <100@100.com> schrieb im Newsbeitrag
news:#A$NUIsRFHA.3560@TK2MSFTNGP14.phx.gbl...
> Alex,
>
[quoted text clipped - 73 lines]
> >> > TIA,
> >> > Axel Dahmen
Stoitcho Goutsev \(100\) [C# MVP] - 25 Apr 2005 23:24 GMT
Axel,

Yes, each container creates it own scope for the TabIndex. You can have two
control's with tab index 0 as long as they belong to different containers.

Why some times works some times not I cannot say unless we have some working
code to work on. If you write some compilable code that demonstrates the
problem I'll more than happy to help you.

Signature

Stoitcho Goutsev (100) [C# MVP]

>I don't get it: Sometimes it works, but most of the time it doesn't....
>
[quoted text clipped - 99 lines]
>> >> > TIA,
>> >> > Axel Dahmen

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.