Vladut
Vladut

Reputation: 657

How do i give a value a TextBox create dynamically when i click and doubleClick? vb.net

I have text box create like that:

Dim Result1 As New TextBox
Result1.ID = "BOX_Result" & a & "_" & i

I want when i click on that textbox to write "OK" and when i double click in cell to put NOT/OK

Important! The TextBox is created Dynamically if i try Result.Click doesn't work, get ne that error: "Result1.Click display error: "Click is not an event of 'System.Web.UI.WebControls.TextBox' "

I try like that but doesn't work:

AddHandler Result1.Click, AddressOf Me.Result1_Click

Private Sub Result1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    Result1.Text = "OK"
End Sub

I want when a person click on that textbox created dynamically but doesn't work click. Thanks for help

Upvotes: 0

Views: 270

Answers (3)

Martin Parenteau
Martin Parenteau

Reputation: 73791

You can add these lines to your TextBox definition:

Result1.Attributes.Add("onclick", "this.value = 'OK';")
Result1.Attributes.Add("ondblclick", "this.value = 'NOT/OK';")

In this code, the text "NOT/OK" is displayed when the user double-clicks in the TextBox. In your question, you talk about a double-click "in cell". If that "cell" is not the TextBox, please give some indication of what kind of control it is.

Upvotes: 2

Beldi Anouar
Beldi Anouar

Reputation: 2180

I have create a simple exemple for you :

Public Class Form1



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Result1 As New TextBox
            Result1.Text = "BOX_Result"

            Dim loc As New Point With {.Y = 117, .X = 111}
            Result1.Location = loc

            Me.Controls.Add(Result1)
            AddHandler Result1.Click, AddressOf Me.Result1_Click
        End Sub

        Private Sub Result1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

            Dim txt As TextBox = sender
            sender.Text = "OK"
        End Sub
    End Class

Hope that you want

Upvotes: 1

Jande
Jande

Reputation: 1705

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim tb As New TextBox 'Create the new TextBox

        AddHandler tb.DoubleClick, AddressOf TB_DoubleClick 'Add a handler to the textbox`s DoubleClick event
        AddHandler tb.Click, AddressOf TB_Click


        'Set any other properties of textbox you want here....

        Me.Controls.Add(tb) 'Add the textbox to the forms controls

    End Sub


'This is the textbox Click event handler sub

 Private Sub TB_Click(ByVal sender As Object, ByVal e As System.EventArgs)

            Dim tb As TextBox = DirectCast(sender, TextBox) 'Cast the (sender) into a textbox to get access to the textbox`s properties

           Result1.Text = "OK"
        End Sub


    'This is the textbox DoubleClick event handler sub

    Private Sub TB_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim tb As TextBox = DirectCast(sender, TextBox) 'Cast the (sender) into a textbox to get access to the textbox`s properties

       Result1.Text = "NOT OK"
    End Sub

Upvotes: 1

Related Questions