Select text from the screen and write to the textbox

Select text from the screen and write to the textbox

ferhatttcoskun
Enthusiast Enthusiast
2,540 Views
2 Replies
Message 1 of 3

Select text from the screen and write to the textbox

ferhatttcoskun
Enthusiast
Enthusiast
Let the text or mtext objects I select from the screen write to texbox1.text

write next selection to textbox2

write next selection to textbox3

write next selection to textbox4

If I click in an empty place, it will beep.

if I choose another object, msgbox"please select text or mtext object"

I want to do like this.
Can you help me, please?

Thank you.

0 Likes
Accepted solutions (1)
2,541 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor
Accepted solution

Showing MessageBox, or making AutoCAD beep isn't AutoCAD's style of feeding back to user action (selecting), and is not what AutoCAD user expected/ is used to. Showing messagebox while asking user to re-select is particular annoying, because user needs an extra click to dismiss the message box.

 

You would use AcadUtility.GetEntity() in a loop, to make sure only Text/MText is selected. The code would look like:

 

Private Function PickText() As String

    Dim ent As AcadEntity
    Dim pt As Variant
    Dim txtEnt As AcadText
    Dim mtxtEnt As AcadMText
    Dim go As Boolean
    
    Dim val As String
    
    On Error Resume Next
    
    Do
        go = False
        ThisDrawing.Utility.GetEntity ent, pt, vbCrLf & "Select a TEX/MText object:"
        
        If Err.Number <> 0 Then
            '' user pressed Esc, or clicked an empty location
            PickText = ""
            Exit Function
        End If
        
        If TypeOf ent Is AcadText Then
            Set txtEnt = ent
            val = txtEnt.TextString
        ElseIf TypeOf ent Is AcadMText Then
            Set mtxtEnt = ent
            val = mtxtEnt.TextString
        Else
            go = True
            ThisDrawing.Utility.Prompt vbCrLf & _
                "invalid selection: must be Text/MText object!" & vbCrLf
        End If
    
    Loop While go
    
    PickText = val
    
End Function

Then, in your form, for each TextBox where you expect text value from user picking, you would place a command button beside it. so that when user click the button, the code hide the form to allow user to pick. the code for the button would look like:

 

Private Sub Command1_Click()
    Me.Hide
    Dim txt As String
    txt = PickText()
    If Len(txt) > 0 Then TextBox1.Value = txt
    Me.show
End Sub

However, with the limited AcadUtility.GetEntity() functionality, there is no easy way to distinguish if user presses Ecs to cancal the pick, or user clicks an empty place, because in both case, AutoCAD VBA would raise error with the same error number/description.

 

If you move to AutoCAD .NET API programming, you would have more control to user action when picking entity is expected.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

ferhatttcoskun
Enthusiast
Enthusiast

Thank you for giving a time.Commands are running.

0 Likes