Closing a form during a constructor

Calling Close from the constructor of the Form is not possible, as it will call Dispose on a Form that has not yet been created. To close the Form after construction, assign an anonymous event handler to the Load event that closes your Form before it is displayed for the first time: public partial class … Read more

Find node clicked under context menu

You can add a mouse click event to the TreeView, then select the correct node using GetNodeAt given the mouse coordinates provided by the MouseEventArgs. void treeView1MouseUp(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Right) { // Select the clicked node treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y); if(treeView1.SelectedNode != null) { myContextMenuStrip.Show(treeView1, e.Location); } } }

Prompt Dialog in Windows Forms

You need to create your own Prompt dialog. You could perhaps create a class for this. public static class Prompt { public static string ShowDialog(string text, string caption) { Form prompt = new Form() { Width = 500, Height = 150, FormBorderStyle = FormBorderStyle.FixedDialog, Text = caption, StartPosition = FormStartPosition.CenterScreen }; Label textLabel = new … Read more

Some Alt keys changes my RichTextBox font

This is the default behavior. The RichTextBox Control automatically finds a fallback font to represent characters that the current Font can’t handle. If not otherwise instructed, it changes the Font selection with the fallback Font. Read this rant about the same default behavior of the RichTextBox ancestor (the RichEdit / MsftEdit Control, from which the … Read more

How to apply a fade transition effect to Images using a Timer?

There are some problem to fix here: → fadeTimer.Interval = 10;: You’re (possibly) using the wrong Timer: the System.Timers.Timer‘s Elapsed is raised in a ThreadPool Thread. Unless you have set the SynchronizingObject to a Control object, which is then used to marshal the handler calls, referencing a Control in the handler will cause trouble (cross-thread … Read more

Prerequisites button disabled – MSI installer

Prerequisities in Visual Studio Projects In Configuration at the top of the dialog, did you try to select either Release or Debug? That should enable the Prerequisites… button. Unecessary, outdated prerequisites? One pet-peeve of mine: is it really necessary to include the .NET runtime as a prerequisite when most users have it installed by their … Read more

Validating user input / Give .NET controls status OK or NOK

You have some useful features in windows forms to perform validation and show error messages including: IDataErrorInfo Interface Validating Event of Controls ErrorProvider Component ValidateChildren Method and AutoValidate Property of Form Using above options: You can perform validation when you are using data-binding to model classes. You van perform validation when you don’t use data-binding. … Read more

Adding an event handler for a control in child form from parent form in C#

You can add event in child form and rise it when text changed. Then create event handler in parent form and change text in parent form. In child form: public event EventHandler OnChildTextChanged; private void textBox1_TextChanged(object sender, EventArgs e) { if(OnChildTextChanged != null) OnChildTextChanged(textBox1.Text, null); } In parent form: private void button1_Click(object sender, EventArgs e) … Read more