Showing posts with label Windows Forms. Show all posts
Showing posts with label Windows Forms. Show all posts

How to open ComboBox (DropDown it) hosted in DataGridView with single mouse click.

The usage of ComboBox in a DataGridView delivers some limitations of its usage. Very common problem is opening the ComboBox with single click of the mouse. By default double mouse click is needed because the first click selects the cell and the second click enters into Edit mode where the ComboBox fires its DropDown event.

We can do this using DataGridView.CellClick event. The following code is enough to open the ComboBox and fire its DropDown event.


Private Sub DataGridView1_CellClick(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
Handles DataGridView1.CellClick
'enters into edit mode
DataGridView1.BeginEdit(True)

'checking if the editing control is a combobox
If TypeOf (DataGridView1.EditingControl) Is ComboBox Then

'casting the editing control and fire DropDown event
CType(DataGridView1.EditingControl, ComboBox).DroppedDown = True

End If
End Sub


DataGridView.EditingControl represents the control hosted by the current cell.

Way for localizing MessageBox in Windows Forms

Very good article in code project, where you can see how the buttons of MessageBox can use custom text labels. The natural language of .NET platform will be overridden and only your labels will be shown.

http://www.codeproject.com/KB/miscctrl/Localizing_MessageBox.aspx

Actually if you use MessageBoxOptions in the parameters of MessageBox.Show(..) only the original labels are shown. But I don't think this is big problem.

VB.NET: General Exception Handling in Windows Forms

VB.NET

My.Application.UnhandledException Event is the way for handling all unhandled exceptions in an windows forms application. The application raises this event when it encounters unhandled exception.
For using the event editing ApplicationEvents.vb file is needed. By default ApplicationEvents.vb is hidden. For openning (unhiding) it use Project Properties -> Application tab -> View Appication Events.
Then the handler can be added/edited as in the example below:

Private Sub MyApplication_UnhandledException( _
ByVal sender As Object, _
ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs _
) Handles Me.UnhandledException

Try
//write log, which is nessted in Application Data folder
My.Application.Log.WriteException( _
e.Exception, _
TraceEventType.Critical, _
"Unhandled Exception!")

//check up if the Event folder exists.
//if not - create new one
If Not EventLog.SourceExists("My software") Then
EventLog.CreateEventSource("My software", "My Software")
End If

//write event log, can be seen by Event Viewer
EventLog.WriteEntry("My software", _
e.Exception.ToString(), _
EventLogEntryType.Error)
Catch ex As Exception

End Try

// code for notifying the user with user friendly message
...................

//set the default application action (closing) not to be performed.
e.ExitApplication = False
End Sub
And from now on all unhandled exception in the application will be stored as exception information and stack trace in the application log file or in the Windows Event log and the user will receive friendly message.


C#

Of course most of my posts give examples with VB and C# when they are different. Now I will give up this pleasure to R. Newman with his great post "Top-level Exception Handling in Windows Forms Applications"