How can I get WinForms to stop silently ignoring unhandled exceptions?

In your Program.cs’ Main function you should also ensure that you’ve wrapped your call to open the form in a try/catch. Additionally use the AppDomain.UnhandledException to catch exceptions. We also add Application.ThreadException too. I believe the following will give you hooks into all the exceptions that can be thrown… static void Main() { try { … Read more

MVVM for winforms [duplicate]

I think that there are two answers here… really just one answer to “Should I” and one answer to “Could I”. As far as “Could I”, it is certainly possible. MVVM really just relies on a view that can bind to a view model. Since WinForms supports binding, this certainly is possible. You may need … Read more

Close button in tabControl

Without deriving a class, here is a neat snippet: Set the DrawMode property of the Tab Control to OwnerDrawFixed. This property decides whether system or developer can paint the captions. Add the code in the DrawItem event of the Tab Control – This event will be invoked for painting each Tab Page. //This code … Read more

How to find the main() entry point in a VB.Net winforms app?

In VB you’ll need to create your sub main by hand as it were so what I generally do is create a new Module named Program. Within that as a very basic setup you’ll need to add the following code. Public Sub Main() Application.EnableVisualStyles() Application.SetCompatibleTextRenderingDefault(False) Application.Run(New Form1) End Sub Once you’ve done that go to … Read more

Changing datagridview cell color based on condition

I may suggest NOT looping over each rows EACH time CellFormating is called, because it is called everytime A SINGLE ROW need to be refreshed. Private Sub dgv_DisplayData_Vertical_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgv_DisplayData_Vertical.CellFormatting Try If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells(“LevelID”).Value.ToString() = “6” Then e.CellStyle.BackColor = Color.DimGray End If If dgv_DisplayData_Vertical.Rows(e.RowIndex).Cells(“LevelID”).Value.ToString() = “5” Then e.CellStyle.BackColor = Color.DarkSlateGray End … Read more

Popup window in winform c#

Just create another form (let’s call it formPopup) using Visual Studio. In a button handler write the following code: var formPopup = new Form(); formPopup.Show(this); // if you need non-modal window If you need a non-modal window use: formPopup.Show();. If you need a dialog (so your code will hang on this invocation until you close … Read more

Populating a ComboBox using C#

Define a class public class Language { public string Name { get; set; } public string Value { get; set; } } then… //Build a list var dataSource = new List<Language>(); dataSource.Add(new Language() { Name = “blah”, Value = “blah” }); dataSource.Add(new Language() { Name = “blah”, Value = “blah” }); dataSource.Add(new Language() { Name … Read more

Abstract UserControl inheritance in Visual Studio designer

What we want First, let’s define the final class and the base abstract class. public class MyControl : AbstractControl … public abstract class AbstractControl : UserControl // Also works for Form … Now all we need is a Description provider. public class AbstractControlDescriptionProvider<TAbstract, TBase> : TypeDescriptionProvider { public AbstractControlDescriptionProvider() : base(TypeDescriptor.GetProvider(typeof(TAbstract))) { } public override … Read more