RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB
Expertise: Intermediate
Jan 25, 2006

Using a Default Button and a Multiline Text Box on the Same Form

Suppose you have a button that uses a Default property (set to true) and are editing text in a multiline text box on the same form. When you press the enter, the button's click event fires rather than having a new line added to the text box.

Placing the following code into your click event on the default button on your form enables you to use a default button and a multiline text box on the same form. Basically, this code checks which control has the focus—if it's a multiline textbox, the code cancels the button's click event. You then have to send a newline to the text box manually.

Private Sub cmdOK_Click()
    Static ignoreClick As Boolean
    If ignoreClick = False Then
        If TypeOf ActiveControl Is TextBox Then
            If ActiveControl.MultiLine = True Then
                SendKeys vbCrLf
                ignoreClick = True
                Exit Sub
            End If
        End If
        ignoreClick = False
        Exit Sub
    End If
    ' normal code for the OK button would go here...

End Sub
Valpied Adam
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date