Application.Exit();
Monday, May 21, 2018
How To Terminate or Exit Program in C#
When your application opens another form and you want that the user can terminate the program without going back to the main form, this 1-line code will help you.
Sunday, May 20, 2018
How To Make a Borderless Form Movable in C#
If you made your form borderless, you might be wondering how to move it to different location of the screen, then you are at the right blog. Here's how you do it.
First, make a MouseDown event in your form.
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void LoginForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
First, make a MouseDown event in your form.
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void LoginForm_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
How To Make an Event when Enter Key is Pressed in a TextBox in C#
First, you have to add a KeyPress event just like the picture show below.
private void txtUsername_KeyPress(object sender, KeyPressEventArgs e)
{
if (Convert.ToInt32(e.KeyChar) == 13)
{
//This is where you will put your code if Enter key is pressed
}
}
How To Check if a String or TextBox is Empty or Null in C#
To check if a string or textbox is empty, simply follow the code snippet below.
if (!string.IsNullOrEmpty(textboxName.Text)
{
//This will if a textbox is not empty
}
if (!string.IsNullOrEmpty(stringName)
{
//This will if a string variable is not empty
}
if (!string.IsNullOrEmpty(textboxName.Text)
{
//This will if a textbox is not empty
}
if (!string.IsNullOrEmpty(stringName)
{
//This will if a string variable is not empty
}
Subscribe to:
Posts (Atom)