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:
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:
In this case it could also be delegated to a ThreadPool thread like this:
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.
The mouse events related to the button click happen in this order:
- MouseDown
- CellMouseDown
- Click
- CellClick
- MouseClick
- CellMouseClick
- CellContentClick
- CellMouseUp
- MouseUp
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")
VB.NET:
processStart.BeginInvoke("http://google.com", Nothing, Nothing)
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.