none
Selecting a Label and make changes to the text. RRS feed

  • Question

  • Hope this can be done.  

    I have on my app a list of DataBound Labels created in Runtime, I want to be able to click on eny one of them and have the text show in a TexBox where changes can be made to it and saved back to the DataBase.

    Wednesday, February 19, 2020 3:18 PM

Answers

  • I have tryed the code you posted, it seems to work.  

    Once I implemented it in my app I got an error.  

    Hi

    That suggests that you are defining the Labels at run time, and your declaration doesn't have the WithEvents.

    You can either use an event handler with a Handles clause and manually add the Labels to it, or, if you have a large number of Labels, use a loop and a Sub WITHOUT any Handles clause and add the Handler in code..

    Here is an example that adds 9 Labels and adds their handler in code (rather than adding manually)

    ' Form1 with TextBox1 Option Strict On Option Explicit On Public Class Form1 Dim currentLabel As New Label Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' add run time labels (9) Dim x As Integer = 20 Dim y As Integer = 80 For i As Integer = 1 To 9 Dim lab As New Label With lab .Name = "Lab" & i.ToString .Text = .Name .Location = New Point(x, y) .AutoSize = False .Size = New Size(100, 26) .BackColor = Color.Pink .BorderStyle = BorderStyle.FixedSingle End With x += 110 If x > 300 Then x = 20 y += 30 End If Controls.Add(lab)

    ' add ALL Lab to same Handler AddHandler lab.Click, AddressOf Lab_Click Next End Sub Private Sub Lab_Click(sender As Object, e As EventArgs) currentLabel = DirectCast(sender, Label) Label111.Text = "Editing: " & currentLabel.Name & " (ENTER to finish)" TextBox1.Text = currentLabel.Text End Sub Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated currentLabel.Text = TextBox1.Text End Sub Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter Then currentLabel.Text = TextBox1.Text e.SuppressKeyPress = True End If End Sub End Class



    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, February 19, 2020 7:34 PM
    • Marked as answer by VB Novice Hendri Thursday, February 20, 2020 10:48 AM
    Wednesday, February 19, 2020 7:28 PM

All replies

  • Hi

    The simplest way, example using Label1 and TextBox1

    	Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
    		TextBox1.Text = Label1.Text
    	End Sub
    
    	Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
    		Label1.Text = TextBox1.Text
    	End Sub

    EDIT:   If you want the ENTER key to update the Label then add this

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
    		If e.KeyCode = Keys.Enter Then
    			Label1.Text = TextBox1.Text
    			e.SuppressKeyPress = True
    		End If
    	End Sub

    Regards Les, Livingston, Scotland

    • Edited by leshay Wednesday, February 19, 2020 4:12 PM
    Wednesday, February 19, 2020 4:03 PM
  • This is the kind of code I'm looking for. 

    Private Sub Selected_Label_Click(sender As Object, e As EventArgs) Handles Selected.Label.Click
            TextBox1.Text = Selected.Label.Text
        End Sub
    I know the code is bull ###. It is just an way of showing you what I mean.

    Wednesday, February 19, 2020 4:15 PM
  • This is the kind of code I'm looking for. 

    Private Sub Selected_Label_Click(sender As Object, e As EventArgs) Handles Selected.Label.Click
            TextBox1.Text = Selected.Label.Text
        End Sub
    I know the code is bull ###. It is just an way of showing you what I mean.

    Hi

    There is no question in ypur reply, but I *think* you are wanting to deal with multiple Labels. If so, then see code below

    NOTE: I have only used 2 Labels in the excample but same code can handle as many Labels as needed - just need to add them to the Handle clause as commented.

    ' Form1 with TextBox1
    ' Label(s) as needed
    Option Strict On
    Option Explicit On
    Public Class Form1
        ' hold onto which Label is being editted
        Dim currentLabel As New Label
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' need to ensure the Label(s) don't
            ' disappear from view
            Label1.AutoSize = False
            Label2.AutoSize = False
    
            Label1.Width = 100
            Label2.Width = 100
    
            Label1.Height = 24
            Label2.Height = 24
    
            Label1.BorderStyle = BorderStyle.FixedSingle
            Label2.BorderStyle = BorderStyle.FixedSingle
    
            Label1.BackColor = Color.Pink
            Label2.BackColor = Color.Pink
        End Sub
    
        ' each Label to be included needs to be
        ' added to the Handles clause
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click, Label2.Click
    
            currentLabel = DirectCast(sender, Label)
            Label111.Text = "Editting: " & currentLabel.Name & " (ENTER to finish)"
            TextBox1.Text = currentLabel.Text
        End Sub
        Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
            currentLabel.Text = TextBox1.Text
        End Sub
        Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
            If e.KeyCode = Keys.Enter Then
                currentLabel.Text = TextBox1.Text
                e.SuppressKeyPress = True
            End If
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, February 19, 2020 5:16 PM
    Wednesday, February 19, 2020 5:15 PM
  • I have tryed the code you posted, it seems to work.  

    Once I implemented it in my app I got an error.  

    Wednesday, February 19, 2020 6:56 PM
  • I have tryed the code you posted, it seems to work.  

    Once I implemented it in my app I got an error.  

    Hi

    That suggests that you are defining the Labels at run time, and your declaration doesn't have the WithEvents.

    You can either use an event handler with a Handles clause and manually add the Labels to it, or, if you have a large number of Labels, use a loop and a Sub WITHOUT any Handles clause and add the Handler in code..

    Here is an example that adds 9 Labels and adds their handler in code (rather than adding manually)

    ' Form1 with TextBox1 Option Strict On Option Explicit On Public Class Form1 Dim currentLabel As New Label Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' add run time labels (9) Dim x As Integer = 20 Dim y As Integer = 80 For i As Integer = 1 To 9 Dim lab As New Label With lab .Name = "Lab" & i.ToString .Text = .Name .Location = New Point(x, y) .AutoSize = False .Size = New Size(100, 26) .BackColor = Color.Pink .BorderStyle = BorderStyle.FixedSingle End With x += 110 If x > 300 Then x = 20 y += 30 End If Controls.Add(lab)

    ' add ALL Lab to same Handler AddHandler lab.Click, AddressOf Lab_Click Next End Sub Private Sub Lab_Click(sender As Object, e As EventArgs) currentLabel = DirectCast(sender, Label) Label111.Text = "Editing: " & currentLabel.Name & " (ENTER to finish)" TextBox1.Text = currentLabel.Text End Sub Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated currentLabel.Text = TextBox1.Text End Sub Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter Then currentLabel.Text = TextBox1.Text e.SuppressKeyPress = True End If End Sub End Class



    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, February 19, 2020 7:34 PM
    • Marked as answer by VB Novice Hendri Thursday, February 20, 2020 10:48 AM
    Wednesday, February 19, 2020 7:28 PM
  • Thanks Verry Verry mutsh, it works like a charm.
    Thursday, February 20, 2020 10:48 AM