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    Else        ignoreClick = False        Exit Sub    End If        ' normal code for the OK button would go here...End Sub
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: