The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Visual Basic Programming
|
Cannot focus a field or set 'SelStart'
Discuss Cannot focus a field or set 'SelStart' in the Visual Basic Programming forum on Dev Shed. Cannot focus a field or set 'SelStart' Visual Basic Programming forum discussing VB specific programming information. Quickly prototype and build applications with this robust and simple language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 16th, 2012, 03:53 AM
|
 |
Contributing User
|
|
|
|
|
Cannot focus a field or set 'SelStart'
Hi,
I seem to be banging my head on a wall that is focusing a field and placing the carret at the end of the line.
I have the following code in a click event
Code:
=inputZoom("Notes")
the function is this
Code:
Public Function inputZoom(sTitle As String, Optional ByRef CTRL As Variant)
On Error Resume Next
Dim ctl As Access.Control
If Not IsMissing(CTRL) Then
Set ctl = CTRL
Else
Set ctl = Screen.ActiveControl
End If
If ctl.Parent.Recordset.Updatable Then
DoCmd.RunCommand acCmdSaveRecord
End If
DoCmd.OpenForm "inputPopUp", acNormal, , , , , sTitle
Forms("inputPopUp").BindControl ctl
End Function
the BindControl sub does this
Code:
Public Sub BindControl(BoundControl As Access.TextBox)
On Error GoTo errlbl
Dim zoomCtl As Access.TextBox
Dim ctl As Object
Set zoomCtl = Me.inputTextBox
Set ctl = BoundControl.Parent
Do While Not TypeOf ctl Is Access.Form
Set ctl = ctl.Parent
Loop
Set oControl = BoundControl
Set Me.Recordset = ctl.Recordset
With zoomCtl
.ControlSource = BoundControl.ControlSource
.Locked = BoundControl.Locked
.ValidationRule = BoundControl.ValidationRule
.ValidationText = BoundControl.ValidationText
.Format = BoundControl.Format
.ControlTipText = BoundControl.ControlTipText
.ForeColor = BoundControl.ForeColor
End With
Me.inputTextBox.SetFocus
If Not IsNull(Me.inputTextBox) Then
Me.inputTextBox.SelStart = Len(Me.inputTextBox)
Else
Me.inputTextBox.SelStart = 0
End If
Exit Sub
errlbl:
MsgBox err.Number & " " & err.Description
Exit Sub
End Sub
as you can see by the highlighted code it should focus the field and set the carret position , only it doesn't?
Why?
If I place this at the end of the inputZoom function
Code:
msgbox Screen.ActiveControl.Name
I get
so the correct field is active, and what's even more bizzare to me is when I click 'OK' the field then has the correct focus and the carret is at the end of the line.
Take away that debug statement, it doesn't work, keep the debug statement, it works?
I'm baffled as to why it is behaving like this, can any one help explain why and how I fix it?
Thanks,
1DMF.
|

May 16th, 2012, 02:33 PM
|
 |
Type Cast Exception
|
|
Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
|
|
Quote: | Take away that debug statement, it doesn't work, keep the debug statement, it works? |
Based on that I'd try tossing in a Form.Repaint where your debug is.
__________________
medialint.com
“Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
|

May 16th, 2012, 09:39 PM
|
|
|
I believe the problem you are experiencing is a screen update issue. Your code is asking to set the focus on the text box and then checking the results. A screen refresh occurs only when the system has time, and when you insert the debug code it gives the screen a chance to refresh. I suggest using the GotFocus event:
Code:
Private Sub CustText_GotFocus()
CustText.SelStart = 0
CustText.SelLength = Len(CustText)
End Sub
In the above code, I highlight the entire text and place the input carrot at the end of the field. Pressing any key (other than a cursor control key) replaces the contents of the text box with the key pressed. Using the cursor right key removes the highlight. Using this technique allows you to respond no matter how the focus was achieved. For example, using the cursor down key shifts the focus to the next field:
Code:
Private Sub CustText_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case Key_Down
CustList.Visible = True
CustList.SetFocus
KeyCode = 0
End Select
End Sub
|

May 17th, 2012, 04:02 AM
|
 |
Contributing User
|
|
|
|
Can't get either to work.
I certainly don't want all the text highlighted, one accidental press of a key and all the exisitng content is wiped out.
I even added a msgbox in the 'gotfocus' it triggered 3 times, but after clearing them, still the caret was not at the end of the text.
i cannot get the field to be focused and the cursor at the end, i don't know why, It just wont do it?
EDIT: -> Yup I'm completely stumped by this?
MsgBox Screen.ActiveControl.Name
Displays the correct textbox and when OK is clicked the carret flashes nicely at the end of the content.
However, If I don't use that debug statement, the textbox isn't focued, i know this because, if you press any key, you get that alert donk error sound, when something isn't permitted, the same sound when you try to move focus away from a dialog/modal form!
So why is the code when asked telling me a control has focus, when it doesn't because pressing a key causes an error donk? 
Last edited by 1DMF : May 17th, 2012 at 04:39 AM.
|

May 17th, 2012, 10:23 AM
|
|
|
Try this. Comment out any code that attempts to control the cursor, and then add the following code to the control in question:
Code:
Private Sub Text1_GotFocus()
Text1.SelStart = Len(Text1.Text)
End Sub
Make sure the focus is not on the control in question, and then click on the control in question. The cursor should move to the end of the text.
I added a command button and a text box (in that order) to a new project and added some text to the text box. The command button has a TabIndex=0, so it receives the focus when the program starts. When I tab to the text box, the cursor is at the start of the text. When I add the above code, the cursor moves to the end of text.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|