Question How can i make labels editable at runtime just like text box?

ashu2k6k

Member
Joined
May 20, 2008
Messages
8
Programming Experience
Beginner
I want that I should be able to enter text in label just like we can enter text in textbox.

Thanks.
 
You can't, use a Textbox. For example have a hidden textbox that you only display on top of the label when in "edit mode". Here is a code sample that might get you some ideas. One event handler handles the MouseDoubleClick of some labels and puts the before hidden textbox there, the Textbox is hidden again and text transferred to the label when Enter key is pressed.
VB.NET:
Private currentLabel As Label
 
Private Sub labels_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Label2.MouseDoubleClick, Label1.MouseDoubleClick
    currentLabel = sender
    Me.TextBox1.Text = currentLabel.Text
    Me.TextBox1.Bounds = currentLabel.Bounds
    Me.TextBox1.Visible = True
    Me.TextBox1.BringToFront()
    Me.TextBox1.Select()
End Sub

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles TextBox1.KeyUp
    If e.KeyCode = Keys.Enter Then
        currentLabel.Text = Me.TextBox1.Text
        Me.TextBox1.Visible = False
    End If
End Sub
 
Thanks John for the reply. Can't we make a custom label control which we can edit.
Why would you want to? Such a control already exists. It's called a TextBox. What you're asking for is like saying you want to get a car but you want to get one that goes on water. That's called a boat, so use a boat. What you want is a TextBox so use a TextBox.

Why is it that you think you don't want a TextBox? Is it the border and the background colour? Fine. Remove the border and change the background colour. The TextBox has properties to do that.
 
Sorry John, if my answer has annoyed you. My only concern is can we make size of textbox small with its border property set.
 
Sorry John, if my answer has annoyed you. My only concern is can we make size of textbox small with its border property set.
The Height of a TextBox is controlled by its font so you cannot change it without changing the Font property.

If your issue was that you wanted to change the border then that's something you should have said. Just remove the TextBox's border, put it on a Panel and then set the Border of the Panel.
 
Back
Top