Solved

UserControl Setfocus

Posted on 2001-09-11
12
1,012 Views
Last Modified: 2007-12-19
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
Comment
Question by:thatsthefactsjac
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 17

Accepted Solution

by:
Suat Ozgur earned 50 total points
ID: 6475438
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
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6475683
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
 

Expert Comment

by:ruchir_khanduri
ID: 6475747
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
 

Author Comment

by:thatsthefactsjac
ID: 6475775
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
 
LVL 3

Expert Comment

by:Joebob
ID: 6476805
Private Sub UserControl_GotFocus()
  Text1.SetFocus
End Sub
0
 

Author Comment

by:thatsthefactsjac
ID: 6477213
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:Joebob
ID: 6477344
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
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6477623
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
 
LVL 3

Expert Comment

by:Joebob
ID: 6477695
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
 

Author Comment

by:thatsthefactsjac
ID: 6477792
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
 
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6477810
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
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6478422
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now