devxlogo

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

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Five Early Architecture Decisions That Quietly Get Expensive

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.