Bit of VB .net help

fez

fez

Caporegime
Joined
22 Aug 2008
Posts
28,115
Location
Tunbridge Wells
Bit of a simple one for all the gurus out there. I have a textbox on my form that enlarges when the user enters it using the event ".enter".

I want it to then shrink again after the user clicks outside of the textbox. I have tried numerous events such as the "doubleclick", "lostfocus" etc but the only one that works is "leave".

This works but only when I click on another control on the form; not when I just click off the text box. Any help or guidance would be fantastic and much appreciated.
 
If there's ONLY the textbox on the form, the the lostfocus event won't fire because it's the only control that will have the focus, likewise the leave won't fire because there's no other control to leave from.

One quick easy way of doing it:

Create the textbox.
Set it's width to x-amount when it has the focus or it's entered.
Set it's width to x-amount when it has lost focus or it's been left.

Create a button or some other control that can accept focus.
Set this controls width and height to 0

Now, on the textbox, for the "KeyDown" event, see if the Return key has been pressed and if it has, set the focus to the button you've created.

Sorted.

Code:
     Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
        Me.TextBox1.Width = 300
    End Sub
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Return Then
            Me.Button1.Focus()
        End If
    End Sub
    Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
        Me.TextBox1.Width = 10
    End Sub
[SIZE=2][COLOR=#0000ff][/COLOR][/SIZE]
 
Again a bit ghetto but you can add the control (a picturebox with nothing in it maybe) to the form.
Then you can use the click event of the form itself to do the PictureBox1.Focus()
 
Done both guys, 2 ways of shrinking the box is better than 1. ;-)
 
Last edited:
Back
Top Bottom