Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Previous control?

Status
Not open for further replies.

Mats

Technical User
Joined
Feb 18, 2000
Messages
83
Location
EU
Hello

Is there a good way to get the previous control on a form? In MS Access this was a property, but VB does not seem to support the same feature.

I need this for a form with several textboxes for dates, and one button that opens a calendar control -> when the user closes the calendar control the date should be returned to the textbox that had the focus before clicking the button.

Mats
 
Right off the top of my head, I would make a control array out of the text boxes. Create an integer type module-level variable called intActiveControl. When the form activates, set the variable =0, and set the focus on the first textbox in the array (index =0). On the GotFocus event of the textboxes, set the value of intActiveControl equal to the index of the control. Then, when the calendar control is closed, you can use the SetFocus method to return focus to the control whose index is equal to intActiveControl.

This should return focus to the control that was active before you selected the calendar control.
 
Yep,

I thought about that kind of approach, maybe I should try it.

Thanks for the input,
Mats
 
I wouldn't change the textboxes to an array, as you would then need to code for every textbox in the single 'procedure'. The global var could just as easily be a string. which holds the NAME of the control. Setfocus works as well there.

Alternatively, place the calendar control in a "Pop-Up". Invoke it from an event in the appropiate textboxes. When the pop-up closes, control will revert to the textbox from whence it was popped.



MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Following skeleton code suggests an approach to your problem. The basic outline is as follows: whenever a textbox gets control it updates a form property LastTextFocus with its reference. This Public Form property is accessible by any other form and will be used to put the focus back.

Program Description:


The first form frmTextBox contains three TextBox controls, appropriately called txtOne, txtTwo an txtThree and a command button cmdDoIt. The latter transfers control to a second form frmDoIt that also contains a command button cmdExit which is supposed to do some usefull work. However, to keep the skeleton as simple as possible, no work is performed. (In your particular case, this button would be replaced by a calendar control).

Whenever a TextBox Gets control, its reference is transferred through the Property Let LastTextFocus to an internal hidden variable m_objLastTextFocus. Which means that the internal variable always will contain a reference to the last active textbox. Of course, all text boxes you want to control must Set this hidden variable. This variable can now be consulted through the Property Get procedure by any external form. This is a standard way to transfer information between forms (cfr: faq222-400 or thread222-37333, thread222-42216, thread222-42637). An ideal place to set the focus back to the right textbox is the Unload event.

Source Listing for frmTextBox

Private m_objLastTextFocus As TextBox

Public Property Get LastTextFocus() As TextBox
Set LastTextFocus = m_objLastTextFocus
End Property

Public Property Let LastTextFocus(objText As TextBox)
Set m_objLastTextFocus = objText
End Property

Private Sub cmdDoIt_Click()
Load frmDoIt
frmDoIt.Show
End Sub

Private Sub txtOne_GotFocus()
LastTextFocus = ActiveControl
End Sub

Private Sub txtTwo_GotFocus()
LastTextFocus = ActiveControl
End Sub

Private Sub txtThree_GotFocus()
LastTextFocus = ActiveControl
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set frmTextBox = Nothing
End Sub

Source Listing for frmDoIt

Private Sub cmdExit_Click()

'Do Your Private Processing

End Sub

Private Sub Form_Unload(Cancel As Integer)
frmTextBox.LastTextFocus.SetFocus
Set frmDoIt = Nothing
End Sub


Note that from within frmDoIt you reference a textbox on another form, therefore you must include its reference frmTextBox




_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Thanks a lot for all your input!

BTW: What is the difference in a property get if I use 'set' or not? I have not used 'set', and it works fine for my needs, but I noticed that rvBasic used 'set'. I'm quite new to VB, so even if it works my way I don't know where I might run into problems.

Mats
 
The Set statement should be used whenever an assignment is made to an object variable. LastTextFocus is a function that returns an object (TextBox) variable, hence the use of Set

Ironically enough in VB.NET, the next major version of VB, everything is an object and as such the Set statement is no longer needed to make the difference.

Until then, I recommend using Set when needed. _________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top