How to give initial focus?

  • Thread starter Thread starter Axel Dahmen
  • Start date Start date
A

Axel Dahmen

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
 
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.
 
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



-------
 
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.
 
Thanks, Stoitcho, it works!

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

TIA,
Axel Dahmen

-----------------
Stoitcho Goutsev (100) said:
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.


--
Stoitcho Goutsev (100) [C# MVP]

Axel Dahmen said:
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 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) said:
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.


--
Stoitcho Goutsev (100) [C# MVP]

Axel Dahmen said:
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
 
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.


--
Stoitcho Goutsev (100) [C# MVP]

Axel Dahmen said:
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) said:
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.


--
Stoitcho Goutsev (100) [C# MVP]

Axel Dahmen said:
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.

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,

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
 
Back
Top