Resolved DataGridViewButtonColumn and Process.Start

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,800
Location
Norway
Programming Experience
10+
I encountered a strange problem with a button column in DataGridView, clicking the buttons calls Process.Start to open an url in default browser. Problem was that until browser application had started, moving the mouse would auto-select other cells, as if the button was not released - the physical mouse button was of course released at this time. If no browser instance is open this could take a few seconds and in the means time having lots of cells getting selected unintentionally is really annoying.

The mouse events related to the button click happen in this order:
  1. MouseDown
  2. CellMouseDown
  3. Click
  4. CellClick
  5. MouseClick
  6. CellMouseClick
  7. CellContentClick
  8. CellMouseUp
  9. MouseUp
The recommended event to handle a button click in DataGridView is CellContentClick, so what appears to be happening is that while Process.Start call is awaiting completion MouseUp event has not yet been raised, and moving mouse during this time causes default cell selection behaviour.

What I did to resolve this was to delegate the Process.Start call to UI thread for after CellContentClick event handler was done like this:
VB.NET:
Dim processStart = Sub(url) Process.Start(url)
Me.BeginInvoke(processStart, "http://google.com")
In this case it could also be delegated to a ThreadPool thread like this:
VB.NET:
processStart.BeginInvoke("http://google.com", Nothing, Nothing)
Though some code that takes 1-2-3 seconds for such handler may involve UI and the first option would apply for that.

I attached a simple repro if you want to see this for yourself. I did a few web searches to see if this was a common or even known issue, but could not find any references to this, thus posting this thread to describe it for others to possibly find if they should do similar.
 

Attachments

  • vbnet40-DGVbuttonRepro.zip
    15.4 KB · Views: 19
Back
Top