Author Topic: Example code for retrieving TextBox text  (Read 6956 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Example code for retrieving TextBox text
« on: October 17, 2006, 02:13:06 PM »
Does anyone have some code for retrieving TextBox text?

thanks
TheSwamp.org  (serving the CAD community since 2003)

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Example code for retrieving TextBox text
« Reply #1 on: October 17, 2006, 02:14:35 PM »
x = textbox1.value

Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Example code for retrieving TextBox text
« Reply #2 on: October 17, 2006, 02:15:06 PM »
you can also use textbox1.text




Greg B

  • Seagull
  • Posts: 12417
  • Tell me a Joke!
Re: Example code for retrieving TextBox text
« Reply #3 on: October 17, 2006, 02:29:04 PM »
forgot to mention...

I'm assuming the textbox is in a userform..


ie

userform1.textbox1.text = userform2.textbox4.text


or as stated above substitute .text with .value


Come to think of it, .value might be better, but I'm not really sure if it matters.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Example code for retrieving TextBox text
« Reply #4 on: October 17, 2006, 02:37:17 PM »
I'm assuming the textbox is in a userform..

You'd be assuming correctly too. :-)
TheSwamp.org  (serving the CAD community since 2003)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Example code for retrieving TextBox text
« Reply #5 on: October 17, 2006, 02:45:59 PM »
dont forget to
Dim x as String
so that x can take the value
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Example code for retrieving TextBox text
« Reply #6 on: October 17, 2006, 03:16:22 PM »
Let me rephrase the question. How would you get TextBox.Text from within a Public Sub?

Code: [Select]
Public Sub RunMe()
    '
    ' test module
    '
    Dim MsgText As String
    Dim myFormInstance As MyForm
    Set myFormInstance = New MyForm
   
    With myFormInstance
        .Label1.Caption = "A caption."
        .TextBoxMsg.Text = "Enter some text"
    End With
   
    myFormInstance.Show
    Unload myFormInstance
     
End Sub
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Example code for retrieving TextBox text
« Reply #7 on: October 17, 2006, 03:22:06 PM »
Code: [Select]
Public Sub RunMe()
    '
    ' test module
    '
    Dim MsgText As String
    Dim myFormInstance As MyForm
    Set myFormInstance = New MyForm
   
    With myFormInstance
        .Label1.Caption = "A caption."
        .TextBoxMsg.Text = "Enter some text"
    End With
   
    myFormInstance.Show
   
    dim text as string
    text = myFormInstance.textboxmsg.text
    msgbox text

    Unload myFormInstance
    
End Sub

Coded blind but should work. The preferred way (IMO) would be to expose a property explicity for the purposes of granting access to said text but that might be another topic.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Example code for retrieving TextBox text
« Reply #8 on: October 17, 2006, 03:29:41 PM »
Thanks Michael, I was close .... real close! Missed the myFormInstance.textboxmsg.text part. :-)
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Example code for retrieving TextBox text
« Reply #9 on: October 17, 2006, 04:42:17 PM »
Hey Mark, I quickly bashed out a small example AutoCAD VBA Project (ha, my first ever!) for you. Far from perfect but maybe it will help a bit.

See attached zip file. There is code in the form (frmMain) as well as the module (mdlMain).

Have fun.

:)
« Last Edit: October 17, 2006, 08:19:27 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
Re: Example code for retrieving TextBox text
« Reply #10 on: October 17, 2006, 05:35:32 PM »
Thanks Michael.

I'll give it a go in the morning. :-)
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Example code for retrieving TextBox text
« Reply #11 on: October 17, 2006, 05:39:18 PM »
My pleasure Mark, hope it helps.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Example code for retrieving TextBox text
« Reply #12 on: October 17, 2006, 07:12:55 PM »
For those that cannot download due to security settings at their place of employ here is the code.

First, the form, named frmMain has 3 controls:

    CommandButton, named btnOk
    CommandButton, named btnCancel
    TextBox,       named tbxMain


frmMain code --

Code: [Select]
Option Explicit

Private myExitState As Boolean

Public Property Get ExitState() As Boolean

    ''  Return the value hosted our private member variable.
    ''  Since we have no corresponding Let Property statement
    ''  it's ostensibly read-only.

    ExitState = myExitState

End Property

Public Property Get UserText() As String

    ''  Return the current value of the text in the tbxMain
    ''  object. While the way VB[A] is structured it allows
    ''  access to the object directly we should not go that
    ''  route [IMO]. A future implementation may use a
    ''  different object, or otherwise employ a different
    ''  implementation. Such changes should not affect clients
    ''  or consumers of the form. Also, this gives us the
    ''  opportunity to perform other activities before we
    ''  return the value, like qualifying, logging, whatever.

    UserText = tbxMain.text

End Property

Public Property Let UserText(value As String)

    ''  Allow the client set the value of the txbMain object,
    ''  BUT NOT DIRECTLY. See balance of comments in the Get
    ''  UserText Property.

    tbxMain.text = value

End Property

Private Sub btnOk_Click()

    ''  Set the state and then hide the form.

    IndicateAccept
    Me.Hide

End Sub

Private Sub btnCancel_Click()

    ''  Set the state and then hide the form.
   
    IndicateCancel
    Me.Hide

End Sub

Private Sub IndicateAccept()

    ''  As there may be more than one way this is triggered
    ''  let's have one procedure have responsibility for
    ''  setting the state. While this seems overkill because
    ''  we have but one little variable representing state,
    ''  in a big application there could be many things that
    ''  have to be done aside from merely setting said variable.
    ''  This gives us one entry point, and thus, only one area
    ''  to edit when things change, and more often than not
    ''  they will.
   
    myExitState = True

End Sub

Private Sub IndicateCancel()
   
    ''  See comment in Sub IndicateAccept.

    myExitState = False

End Sub

Private Sub UserForm_Activate()
   
    ''  Start with state indicating cancel.

    IndicateCancel

End Sub

Private Sub UserForm_Initialize()

    ''  Start with state indicating cancel.

    IndicateCancel

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

    ''  If the user closed the form via the control
    ''  box instead of the [Ok] or [Cancel] buttons ...
   
    If CloseMode = VbQueryClose.vbFormControlMenu Then
        IndicateCancel
    End If

End Sub

And then the module code, in a module named mdlMain --

Code: [Select]
Option Explicit

Public Sub Main()
   
    Dim myForm As frmMain
    Set myForm = New frmMain
   
    ''  Access and use the property(s) we exposed.

    myForm.UserText = "Mark's great adventure."
   
    ''  Alrighty, let's show it.
   
    myForm.Show
   
    ''  Program execution will not end up here until the
    ''  user closes the form, either by clicking the [Ok]
    ''  or [Cancel] buttons or via the control box (the
    ''  [x] in the top right corner).   
   
    If Not (myForm Is Nothing) Then
   
        ''  An error may be thrown if the user closes the
        ''  form via the control box because it will have
        ''  been unloaded but not yet nothing (let's trap
        ''  that possibility).
       
        On Error GoTo ErrHandler
   
        If myForm.ExitState Then
       
            ''  access the UserText Property we exposed
           
            MsgBox _
                "User pressed [Ok]." & vbCrLf & vbCrLf & _
                "UserText = <" & _
                myForm.UserText & ">"
               
        Else
       
            MsgBox "User pressed [Cancel]."
           
        End If
       
        Unload myForm
       
        Set myForm = Nothing
       
        Exit Sub
       
ErrHandler:

        If Err.Number = -2147418105 Then
       
            MsgBox _
                "The user closed the form via the " & _
                "form's control box."
               
        Else
       
            ''  Uhhh, what the h3ll? An error we didn't
            ''  anticipate. Let's display the error
            ''  description.
           
            MsgBox Err.Description
           
        End If

        Err.Clear
       
        Resume OutOfHere
       
    End If
   
OutOfHere:
   
    Set myForm = Nothing
       
End Sub

See a problem, see bad advice etc? Let me / us know. Thanks!
« Last Edit: October 17, 2006, 08:15:18 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: Example code for retrieving TextBox text
« Reply #13 on: October 17, 2006, 07:39:04 PM »
And in one fell swoop MP reminds JM that he really is just a hack....

Looks like all great advice Michael! Now if only I could learn to incorporate that advice into the stuff I write......

Thanks for posting the code because I wasn't going to download the project (I've got enough things going now...) but Wow! what an eye opener.

* Jeff_M goes back to writing code with hammer & chisel.....

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Example code for retrieving TextBox text
« Reply #14 on: October 17, 2006, 08:24:58 PM »
And in one fell swoop MP reminds JM that he really is just a hack ...

It's true, I am a hack <not deserving of your praise> -- but I honestly try to get a little better each day! If you see anything wrong with that code or commentary, a distinct possibility, please let me know!

Thanks Jeff!

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst