Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 16th, 2012, 03:53 AM
1DMF's Avatar
1DMF 1DMF is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 320 1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 3 Days 16 h 57 m 54 sec
Reputation Power: 227
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
Quote:
inputTextBox

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.
__________________
Free MP3 Dance Music Downloads

To err is human; To really balls things up you need Microsoft!

Reply With Quote
  #2  
Old May 16th, 2012, 02:33 PM
medialint's Avatar
medialint medialint is offline
Type Cast Exception
Dev Shed God 20th Plane (14500 - 14999 posts)
 
Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
Posts: 14,938 medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)  Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Months 2 Weeks 2 Days 26 m 49 sec
Reputation Power: 8490
Facebook
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

Reply With Quote
  #3  
Old May 16th, 2012, 09:39 PM
couttsj couttsj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 167 couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 17 h 26 m 18 sec
Reputation Power: 43
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

Reply With Quote
  #4  
Old May 17th, 2012, 04:02 AM
1DMF's Avatar
1DMF 1DMF is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 320 1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level)1DMF User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 3 Days 16 h 57 m 54 sec
Reputation Power: 227
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.

Reply With Quote
  #5  
Old May 17th, 2012, 10:23 AM
couttsj couttsj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 167 couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level)couttsj User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 17 h 26 m 18 sec
Reputation Power: 43
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Cannot focus a field or set 'SelStart'

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap