Solved

Component question

Posted on 2006-06-21
7
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do sunrise and sunset times change with altitude 14 45
VB.NET String Settings and Temp Folder Question 3 68
Open a link in vb.net 2 25
Groupbox Control ? 2 27
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 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