setting the initial focus

R

Raja S.

How does one set the initial focus to a control such as a picture box?

Suppose I have a form with a single picture box on it.
When the application starts to run I would like to set the focus to the
picture box so that it can respond to KeyDown events.

I tried placing a picBox.focus in a Form_load event handler but that didn't
seem to help.

Any suggestions?

Thanks in advance,
Raja
 
A

Armin Zingler

Raja S. said:
How does one set the initial focus to a control such as a picture
box?

Suppose I have a form with a single picture box on it.
When the application starts to run I would like to set the focus to
the picture box so that it can respond to KeyDown events.

I tried placing a picBox.focus in a Form_load event handler but that
didn't seem to help.

Any suggestions?

See the documentation on the Focus method describing the preconditions that
must be met to set the focus.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Raja S.) scripsit:
How does one set the initial focus to a control such as a picture box?

Suppose I have a form with a single picture box on it.
When the application starts to run I would like to set the focus to the
picture box so that it can respond to KeyDown events.

I tried placing a picBox.focus in a Form_load event handler but that didn't
seem to help.

Assign the control the lowest 'TabIndex' and it will be focused
automatically.
 
R

Raja S.

Assign the control the lowest 'TabIndex' and it will be focused
automatically.

Thanks for the response but picture boxes don't seem to have the
tabindex/tabstop properies appearing in the properties window.

In form_load if I try to set

picbox.tabstop = true
picbox.tabindex = 0

the focus still doesn't go to the picture box on start up.

I'm able to place a pixbox.focus in a MouseEnter event handler and then I
get the behavior I'd like. But I presume there ought to be a more direct
way of having a picture box the initial focus on application startup.

What am I missing?

Thanks,
Raja
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Raja S.) scripsit:
Thanks for the response but picture boxes don't seem to have the
tabindex/tabstop properies appearing in the properties window.

AFAIS pictureboxes cannot get the focus.
 
A

Adrian Forbes [ASP MVP]

What am I missing?

The picturebox has no key events so why bother with the focus? If your
ultimate goal cannot be achieved why spend time on it?
 
R

Raja S.

The picturebox has no key events so why bother with the focus? If your
ultimate goal cannot be achieved why spend time on it?

Thanks for the follow-up. I have wondered why for a picture box there are
no keyevents in the event drop-down-combo-box. But interestingly VB.net
-does- allow me to write such an event handler:

Private Sub picBox_KeyDown(ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.KeyEventArgs) Handles picBox.KeyDown
...
End Sub

As I mentioned in an earlier note, I am able to give the picture box focus
with:

Private Sub picBox_MouseEnter(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles picBox.MouseEnter
picBox.Focus()
End Sub

and then it responds to KeyDown events. I'm wondering if there is a way for
it to get the initial focus automatically on startup.

Thanks,
Raja
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top