RE: Give textbox focus when form loads modeless

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Bear,

I spent a lot longer on this problem than I wanted to

Tell me about it!

By the way, that code I sent you IS the calling macro. So my workaround code is >in the calling sub, just like yours.

Yes, I wasn't being clear. What I meant was that it seems that in 2000 you
need to switch focus from one control to the one you want whereas in 2003 you
don't seem to need to do that; simply moving the relevant code to the calling
macro (and qualifying the control name with the form name) seems to be
sufficient. Progress? Maybe. (I haven't tested with 2007 yet :-|)

Thanks again.

Regards.

Ed
"Bear" wrote:

Ed:

REALLY glad I was able to help out. I spent a lot longer on this problem
than I wanted to, and ended up with a workaround for functionality that
should have worked. By the way, that code I sent you IS the calling macro. So
my workaround code is in the calling sub, just like yours.

Bear
--
Windows XP, Word 2000


"Ed" wrote:

Hi Bear,

Your example code helped solve my problem :-)

It looks like 2000 and 2003 may behave slightly differently. I originally
had the SetFocus in the UserForm_Initialize handler. I tried switching the
focus to another control and then to the textbox but it didn't make any
difference. So I moved the SetFocus out of the handler into the calling
macro. This worked. I then removed the "dummy" SetFocus and just set the
focus to the textbox and this worked as well. So it looks like in 2003 it
just needs to be done by the calling macro rather than by the form.

Thanks for the help.

Regards.

Ed

"Bear" wrote:

Ed:

I had the same problem in Word 2000. After showing the form, I had to set
focus on a different control, then set it back to the control in question (in
this case a combo box. I hope this will work with a text box too. Here's the
code for the subroutine. The workaround is marked.

Sub ReplaceStyles()

' Replaces paragraph or character styles

If Not objReplaceStyles Is Nothing Then
Set objReplaceStyles = Nothing
End If

Set objReplaceStyles = New frmReplaceStyles

objReplaceStyles.Show

' Workaround to set focus in modeless form

objReplaceStyles.cmdCancel.SetFocus
objReplaceStyles.cboFindStyleName.SetFocus

End Sub

Bear
--
Windows XP, Word 2000


"Ed" wrote:

Hello all,

Using Word 2003. I have a VBA userform which contains a textbox
(txtFindText) and some buttons.

When the form is displayed I would like the textbox to have the focus.

I have a line - txtFindText.SetFocus - in the form's initialisation handler
and this seems to work when I invoke the form by "MyForm.Show".

However, if I invoke the form by "MyForm.Show vbModeless" the textbox does
not seem to get the focus and I seem to need to press the Enter key to give
the box the focus.

Is there a way to give the textbox the focus when the form is displayed as
modeless?

Thanks.

Ed.
.



Relevant Pages

  • Re: DataGridView bound to BindingList -- how to update immediately when they click a
    ... As far as I know, to push the values from the control down into the data source, I have to do an EndEditon the binding source, at which point mine actually saves the data. ... I tried casting bs.Current to the object and calling Save, that didn't work. ... And if I click on the same checkbox repeatedly, it only works the first time. ... I'm not sure what you would do for a textbox, ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Cursor no appare
    ... the cursor, I try with the property "myTextBox.forcus" but nothing. ... you calling the textbox' 'Focus' method? ... Note that the control must be visible and enabled in order to get the focus. ...
    (microsoft.public.dotnet.languages.vb)
  • CTreeCtrl / DeleteAllItems / Goes Invisible
    ... added may not appear in the control. ... There is a simple workaround. ... Instead of calling that function, ... HTREEITEM hItem; ...
    (microsoft.public.vc.mfc)
  • Re: CTreeCtrl / DeleteAllItems / Goes Invisible
    ... added may not appear in the control. ... There is a simple workaround. ... Instead of calling that function, ... HTREEITEM hItem; ...
    (microsoft.public.vc.mfc)
  • Re: Problem optaining focus when calling select in UserControl
    ... > that this is a bug, which I'm not quite sure of, then you can file a bug ... >> UserControl in the GotFocus-event of a TextBox. ... >>> The Control class uses a selection scheme which tries to find the ...
    (microsoft.public.dotnet.framework.windowsforms)