Solved

Component question

Posted on 2006-06-21
7
343 Views
Last Modified: 2008-01-09
Hi,

I try to inherit a class from datagridview.

Then, I want to show a toolstrip when the mouse is over the inherited grid. (I handled mouse Hover event)
And I want to hide the toolstrip when the mouse left the grid. (I handled mouse leave event)
However, when te user moves to the toolstrip to the mouse leave event of the datagridview works and toolstrip becomes invisible
toolstrip is physically placed inside the datagrid boundaries.

Here is the code sample:

Public Class MyDataGridView
    Inherits System.Windows.Forms.DataGridView
    Private WithEvents ToolStrip1 As System.Windows.Forms.ToolStrip
    Private WithEvents ToolStripButton1 As System.Windows.Forms.ToolStripButton

    Private Sub DataGridView_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseHover        
        Me.ToolStrip1 = New System.Windows.Forms.ToolStrip
        Me.ToolStripButton1 = New System.Windows.Forms.ToolStripButton
        Me.ToolStrip1.SuspendLayout()
        '
        'ToolStrip1
        '
        Me.ToolStrip1.Dock = System.Windows.Forms.DockStyle.None
        Me.ToolStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripButton1})
        Me.ToolStrip1.Location = New System.Drawing.Point(101, 183)
        Me.ToolStrip1.Name = "ToolStrip1"
        Me.ToolStrip1.Size = New System.Drawing.Size(66, 25)
        Me.ToolStrip1.TabIndex = 2
        Me.ToolStrip1.Text = "ToolStrip1"
        Me.ToolStrip1.Parent = Me
        '
        'ToolStripButton1
        '
        Me.ToolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
        Me.ToolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta
        Me.ToolStripButton1.Name = "ToolStripButton1"
        Me.ToolStripButton1.Size = New System.Drawing.Size(23, 22)
        Me.ToolStripButton1.Text = "ToolStripButton1"
        Me.Controls.Add(Me.ToolStrip1)
        Me.ToolStrip1.ResumeLayout(False)
        Me.ToolStrip1.PerformLayout()
        ToolStrip1.Location = New Drawing.Point(25, 25)
        ToolStrip1.Show()
    End Sub
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        MessageBox.Show("Hello There")
    End Sub

    Private Sub DataGridView_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
        ToolStrip1.Hide()
    End Sub
end class


As a result: I want to click the buttons on the toolstrip
0
Comment
Question by:tolgaong
  • 5
  • 2
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16953555
Instead of using the MouseLeave event, you could monitor the MouseMove event, and check when the mouse is outside of the DataGridView's boundaries, and set the ToolStrip visibility.  Another comment is that you should create the ToolStrip once at form creation time, and only control the visibility in the MouseMove event.

Bob
0
 
LVL 9

Author Comment

by:tolgaong
ID: 16955271
You are absolutely right on the initialization. Thanks ;)

However, I couldn't use the mousemove event because it does not work outside the grid.
I should handle the containers mousemove but I couldn't do it

I declared a class-level variable

Private WithEvents obj As ContainerControl

    Public Sub New()
        obj = Me.Parent
        AddHandler obj.MouseMove, AddressOf MyDataGridView_MouseMove
    End Sub

and tried to handle it's mousemove event.

Private Sub MyDataGridView_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Me.Parent.Text = e.X
    End Sub


Unfortunately the event is not firing :(

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16960923
I set up a test form like this:

formDataGridView.designer:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class formDataGridView
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.DataGridView1 = New System.Windows.Forms.DataGridView
        Me.ToolStrip1 = New System.Windows.Forms.ToolStrip
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.TextBox1 = New System.Windows.Forms.TextBox
        CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.DataGridView1.SuspendLayout()
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'DataGridView1
        '
        Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        Me.DataGridView1.Controls.Add(Me.ToolStrip1)
        Me.DataGridView1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.DataGridView1.Location = New System.Drawing.Point(0, 0)
        Me.DataGridView1.Name = "DataGridView1"
        Me.DataGridView1.Size = New System.Drawing.Size(615, 432)
        Me.DataGridView1.TabIndex = 0
        '
        'ToolStrip1
        '
        Me.ToolStrip1.Location = New System.Drawing.Point(0, 0)
        Me.ToolStrip1.Name = "ToolStrip1"
        Me.ToolStrip1.Size = New System.Drawing.Size(615, 25)
        Me.ToolStrip1.TabIndex = 1
        Me.ToolStrip1.Text = "ToolStrip1"
        Me.ToolStrip1.Visible = False
        '
        'Panel1
        '
        Me.Panel1.Controls.Add(Me.TextBox1)
        Me.Panel1.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.Panel1.Location = New System.Drawing.Point(0, 432)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(615, 54)
        Me.Panel1.TabIndex = 1
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(12, 16)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(100, 20)
        Me.TextBox1.TabIndex = 0
        '
        'formDataGridView
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(615, 486)
        Me.Controls.Add(Me.DataGridView1)
        Me.Controls.Add(Me.Panel1)
        Me.Name = "formDataGridView"
        Me.Text = "DataGridView Sample"
        CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.DataGridView1.ResumeLayout(False)
        Me.DataGridView1.PerformLayout()
        Me.Panel1.ResumeLayout(False)
        Me.Panel1.PerformLayout()
        Me.ResumeLayout(False)

    End Sub
    Private WithEvents DataGridView1 As System.Windows.Forms.DataGridView
    Private WithEvents ToolStrip1 As System.Windows.Forms.ToolStrip
    Private WithEvents Panel1 As System.Windows.Forms.Panel
    Private WithEvents TextBox1 As System.Windows.Forms.TextBox
End Class
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 16960935
formDataGridView.vb:

Public Class formDataGridView

    Private Sub DataGridView1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.MouseEnter
        Me.ToolStrip1.Visible = True
    End Sub

    Private Sub DataGridView1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.MouseLeave
        Me.ToolStrip1.Visible = False
    End Sub

    Private Sub formDataGridView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ToolStrip1.Visible = False
    End Sub

End Class
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16960941
It seems to do what you are asking for.

Bob
0
 
LVL 9

Author Comment

by:tolgaong
ID: 17035742
I was on vocation, so I couldn't send a post here.
I wrote the below code, it is working. I'm not sure if this is the best way.
I used a timer, and it closes the toolbar

    Public Sub New()
        timer1 = New Timer()
        timer1.Interval = 100
        timer1.Enabled = False
        Init() 'This has some initialization codes.
    End Sub
    Private Sub DataGridView_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseHover
        ToolStrip1.Show()
    End Sub
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        MessageBox.Show("Hello There")
    End Sub

    Private Sub DataGridView_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
        timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles timer1.Tick
        ToolStrip1.Hide()
        timer1.Enabled = False
    End Sub

    Private Sub ToolStrip1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStrip1.MouseHover
        timer1.Enabled = False
    End Sub
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17042199
I told you that I got it to work without a timer, as I imagined that you needed it.  Did you try it my way?

Bob
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help parsing JSON in my VB.Net application 4 96
Allow User To Arrange Columns At Datagridview 3 26
Vb. Net application freezes 9 47
Disable extension 8 38
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question