How do you resize a form to fit its content automatically?

Use the AutoSize and AutoSizeMode properties. An example: private void Form1_Load(object sender, EventArgs e) { // no smaller than design time size this.MinimumSize = new System.Drawing.Size(this.Width, this.Height); // no larger than screen size this.MaximumSize = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, (int)System.Windows.SystemParameters.PrimaryScreenHeight); this.AutoSize = true; this.AutoSizeMode = AutoSizeMode.GrowAndShrink; // rest of your code here… }

WinForms Global Exception Handling?

If #26 is an exception then you can use AppDomain.CurrentDomain.UnhandledException event. If it’s just a return value, then I don’t see any chance to handle that globally. public static void Main() { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler); // start main thread here } static void MyHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; Console.WriteLine(“MyHandler …

Read more

How to programmatically set cell value in DataGridView?

If the DataGridView is databound, you shouldn’t directly modify the content of the cell. Instead, you should modify the databound object. You can access that object through the DataBoundItem of the DataGridViewRow : MyObject obj = (MyObject)dataGridView.CurrentRow.DataBoundItem; obj.MyProperty = newValue; Note that the bound object should implement INotifyPropertyChanged so that the change is reflected in …

Read more