Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I add some richtextboxes dynamically in a tabcontrol and now i want to check when mouse enter or leave the richtextboxes. iam unable to handel the mouse events here is my code

VB
For Each rtb As RichTextBox In TabControl1.Controls.OfType(Of RichTextBox)()
    AddHandler rtb.MouseLeave, AddressOf rtb_MouseLeave
Next


VB
Private Sub rtb_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
    frmclose = True
    Label1.Text = "leave"
End Sub



Handles Me.MouseLeave is just for form not for richtextboxes, how to add handels for richtexboxes
Posted
Updated 8-May-12 21:34pm
v2
Comments
Sandeep Mewara 9-May-12 1:50am    
Code looks fine. What issue you are facing? What do you mean, 'i am unable to handle'?
hackerspk 9-May-12 2:09am    
it dose not give any response whether mouse enters or leave.
Maciej Los 9-May-12 6:08am    
When you run your code (in which event)? Try to remove heandler before you add.
hackerspk 9-May-12 6:39am    
Actually its a dictionary program and it show meanings of the word in sys try. After shown the meanings form that appears in sys try closed so i don't think there is any need to remove handler. because at every run it creat new controls. It is not working even in first run.
Maciej Los 9-May-12 7:33am    
Ok, try to add handler in the same time when you add a RichTextBox, not after in a for ... next loop.

1 solution

Go through the following instructions:
1) Create new "Windows application" project
2) Copy code below
3) Paste it between Public Class Form1 ... End Class tags

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


Private Sub CreateTabControlWithRichTextBoxOnEachPage(Optional ByVal iCountOfPages As Integer = 3)
    Dim tc As TabControl = Nothing, tp As TabPage = Nothing, rtb As RichTextBox = Nothing
    Dim i As Integer = 0

    Try
        tc = New TabControl()
        With tc
            .Parent = Me
            .Dock = DockStyle.Fill
        End With

        For i = 1 To iCountOfPages
            tp = New TabPage
            With tp
                .Name = "TabPage" & i.ToString
                .Text = .Name
            End With
            rtb = New RichTextBox
            With rtb
                .Parent = tp
                .Dock = DockStyle.Fill
                .Name = "RichTextBox" & i.ToString
                AddHandler rtb.MouseEnter, AddressOf rtb_MouseEnter
                AddHandler rtb.MouseLeave, AddressOf rtb_MouseLeave
            End With
            tc.TabPages.Add(tp)
        Next

        Me.Refresh()

    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")

    Finally
        rtb = Nothing
        tp = Nothing
        tc = Nothing

    End Try


End Sub

Private Sub rtb_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
    Me.Text = "MouseEnter event in " & sender.Name
End Sub


Private Sub rtb_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
    Me.Text = "MouseLeave event in " & sender.Name
End Sub


All controls are added in run-time. All working great!
Test it and try to use in your application.

More about events for RichTextBox control:
OnMouseEnter[^]
OnMouseLeave[^]
 
Share this answer
 
Comments
hackerspk 9-May-12 10:33am    
Thanks that work like a charm, i just replace these two lines
AddHandler rtb.MouseEnter, AddressOf rtb_MouseEnter
AddHandler rtb.MouseLeave, AddressOf rtb_MouseLeave

and

Private Sub rtb_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
Me.Text = "MouseEnter event in " & sender.Name
End Sub


Private Sub rtb_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
Me.Text = "MouseLeave event in " & sender.Name
End Sub


it worked but i need to find where i was wrong, thanks buddy, have a grt time
Maciej Los 9-May-12 10:49am    
If my answer was helpful, rate it, OK?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900