• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1117
  • Last Modified:

UserControl Setfocus

I have a usercontrol with one textbox.  I would like a Setfocus method for this usercontrol to set the focus to the textbox, but am under the impression that this method that it is controlled by the extender object.

Is the setfocus method handled by the extender object or can I just create a public method to set focus to the textbox?
0
thatsthefactsjac
Asked:
thatsthefactsjac
  • 3
  • 3
  • 3
  • +2
1 Solution
 
Suat OzgurWeb / Application DeveloperCommented:
Try using this code in User Control General Declaration

Public Event SetFocus()

And put this code into UserControl_GotFocus

txtName.SetFocus

To click on the control or using SetFocus Event of control focus on text box.

I hope i understand your question.

Suat
0
 
Valliappan ANSenior Tech ConsultantCommented:
I dont know if I understood your question.

Is there not a Setfocus event already in a usercontrol. You need to set the tabindex of textbox to 0 in usercontrol.

0
 
ruchir_khanduriCommented:
Hi,

You cannot use the SetFocus method of the text box in this case.

An Extender object holds properties of the control that are actually controlled by the container of the control rather than by the control itself. The Extender Object's SetFocus method is a method to set the focus to the control.

I hope it will help you.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
thatsthefactsjacAuthor Commented:
How do I add a SetFocus method to my UserControl?  When using the ActiveX wizard, it says the method name Setfocus is in use by the extender object.  

Do I create a public method or use the extender object?
0
 
JoebobCommented:
Private Sub UserControl_GotFocus()
  Text1.SetFocus
End Sub
0
 
thatsthefactsjacAuthor Commented:
If I am not mistaken, using the gotfocus event will only allow me to set focus to the textbox when the usercontrol receives the focus by tabbing into the control or the user places focus in one of its child controls.  I would like to explicitly set focus to the control like this:

' input in the usercontrol is not valid
    msgbox "bad input"
    UserControl1.SetFocus

When using the ActiveX wizard, it says the method
name Setfocus is in use by the extender object.  

Do I simply create a public method named 'SetFocus' or use the extender object?

Sorry about any confusion
0
 
JoebobCommented:
Let me re-iterate my very simple post.  Do it just like I showed above and it will work.  Here is my exact code for standard EXE and the UserControl.  You don't need to overload (not possible) the SetFocus method or deal with the Extender object.  The below code will work if the person sets focus to the usercontrol with the mouse, tab, or via code.

STANDARD EXE
Private Sub Form_Click()
  UserControl11.SetFocus
End Sub

USERCONTROL OCX
Private Sub UserControl_GotFocus()
  Text1.SetFocus
End Sub
0
 
Suat OzgurWeb / Application DeveloperCommented:
Joebob, i have already suggested txtname.SetFocus in UserControl_Gotfocus but unfortunately thatsthefactsjac has no comment for my comment. I think we have something wrong.

Regards.
suat
0
 
JoebobCommented:
smozgur, I read your comment above and ask exactly why you have declared a Public Event SetFocus().  This line has no function regarding setting the focus to the textbox contained within the usercontrol.

thatsthefactsjac, the reason that the ActiveX Wizard says the SetFocus method is in use is because VB will handle firing the UserControl_GotFocus event for you.  You do not need to declare a SetFocus method because VB Automatically calls UserControl_GotFocus absolutely anytime that the SetFocus method is called.  FYI, the SetFocus method is called behind the scenes when a user clicks in the area or uses tab, it is also called (obviously) whenever you explicitly called.
0
 
thatsthefactsjacAuthor Commented:
Sorry guys, im not sure what I was thinking.....

smozgur, you provided the answer some time ago, but I was confused as to what the public event was for...


Joebob, thanks for your comment regarding the extender object.  That is part of what I was looking for...I have created a question to award you point as well

sincerely sorry about the trouble,
jac
0
 
Suat OzgurWeb / Application DeveloperCommented:
Joebob, yes you are right. No need to set an Event for SetFocus. It is my mistake. Thanks for warning.

thatsthefactsjac, thanks for grading second correct part of my comment.

with my best regards.
Suat
0
 
Valliappan ANSenior Tech ConsultantCommented:
I hope when you set the tabindex of textbox to 0, it should set focus to textbox, whenever you call Usercontrol1.Setfocus, no need to set it manually in Gotfocus event.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now