-
-
Save zaagan/e1a87ec3d411ba39f0d7d1e7e52660f8 to your computer and use it in GitHub Desktop.
Handy Winform Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ADDS SHADOW TO FORM | |
private const int CS_DROPSHADOW = 0x00020000; | |
protected override CreateParams CreateParams | |
{ | |
get | |
{ | |
CreateParams cp = base .CreateParams; | |
cp.ClassStyle |= CS_DROPSHADOW; | |
return cp; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ParentForm frmParentForm= (ParentForm)Application.OpenForms["ParentForm"]; | |
frmParentForm.DoSomething(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool ProcessKeyPress(Form callerForm, ref Message msg, Keys keyData) | |
{ | |
if (keyData == (Keys.Alt | Keys.Enter)) | |
{ | |
if (callerForm.WindowState == FormWindowState.Normal) | |
callerForm.WindowState = FormWindowState.Maximized; | |
else | |
callerForm.WindowState = FormWindowState.Normal; | |
return true; | |
} | |
else if (keyData == (Keys.Alt | Keys.Right)) | |
{ | |
GotoNextPage(); | |
return true; | |
} | |
else if (keyData == (Keys.Alt | Keys.Left)) | |
{ | |
GotoPreviousPage(); | |
return true; | |
} | |
else if (keyData == (Keys.Alt | Keys.Up)) | |
{ | |
GotoFirstPage(); | |
return true; | |
} | |
else if (keyData == (Keys.Alt | Keys.Down)) | |
{ | |
GotoLastPage(); | |
return true; | |
} | |
else if (keyData == (Keys.Alt | Keys.F5)) | |
{ | |
CallMethod(_className, "Reset"); | |
return true; | |
} | |
else | |
{ return false; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TYPE 1 | |
#region Form Utilities | |
const int WS_MINIMIZEBOX = 0x20000; | |
const int CS_DBLCLKS = 0x8; | |
//REMOVE FLICKERING OF SCREEN | |
protected override CreateParams CreateParams | |
{ | |
get | |
{ | |
CreateParams cp = base .CreateParams; | |
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED | |
cp.Style |= WS_MINIMIZEBOX; | |
cp.ClassStyle |= CS_DBLCLKS; | |
return cp; | |
} | |
} | |
#endregion | |
// TYPE 2 | |
#region REMOVE FLICKERING OF SCREEN | |
const int WS_MINIMIZEBOX = 0x20000; | |
const int CS_DBLCLKS = 0x8; | |
int intOriginalExStyle = -1; | |
bool bEnableAntiFlicker = true; | |
protected override CreateParams CreateParams | |
{ | |
get | |
{ | |
if (intOriginalExStyle == -1) | |
intOriginalExStyle = base.CreateParams.ExStyle; | |
CreateParams cp = base .CreateParams; | |
if (bEnableAntiFlicker) | |
cp.ExStyle |= WS_MINIMIZEBOX; //WS_EX_COMPOSITED | |
else | |
cp.ExStyle = intOriginalExStyle; | |
return cp; | |
} | |
} | |
private void Form1_ResizeBegin( object sender, EventArgs e) | |
{ ToggleAntiFlicker( true); } | |
private void Form1_ResizeEnd( object sender, EventArgs e) | |
{ ToggleAntiFlicker( false); } | |
private void ToggleAntiFlicker( bool Enable) | |
{ | |
bEnableAntiFlicker = Enable; | |
this.MaximizeBox = true; | |
} | |
#endregion | |
public MainPage() | |
{ | |
ToggleAntiFlicker( false); | |
InitializeComponent(); | |
this.ResizeBegin += Form1_ResizeBegin; | |
this.ResizeEnd += Form1_ResizeEnd; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// EXTENSION CLASS TO HANDLE TEXTBOX VALIDATION | |
public static class TextValidator | |
{ | |
public enum ValidationType | |
{ | |
Amount, | |
Integer, | |
AlphaNumeric | |
} | |
/// <summary> | |
/// Validate a textbox on text change. | |
/// </summary> | |
/// <param name="tbx"></param> | |
/// <param name="validationType"></param> | |
public static void Validate(this TextBox tbx, ValidationType validationType) | |
{ | |
PerformValidation(tbx, validationType); | |
tbx.Select(tbx.Text.Length, 0); | |
} | |
private static void PerformValidation(this TextBox tbx, ValidationType validationType) | |
{ | |
char[] enteredString = tbx.Text.ToCharArray(); | |
switch (validationType) | |
{ | |
case ValidationType.Amount: | |
tbx.Text = AmountValidation(enteredString); | |
break; | |
case ValidationType.Integer: | |
tbx.Text = IntegerValidation(enteredString); | |
break; | |
case ValidationType.AlphaNumeric: | |
tbx.Text = AlphaNumericValidation(enteredString); | |
break; | |
default: | |
break; | |
} | |
tbx.SelectionStart = tbx.Text.Length; | |
} | |
private static string AmountValidation(char[] enteredString) | |
{ | |
string actualString = string.Empty; | |
int count = 0; | |
foreach (char c in enteredString.AsEnumerable()) | |
{ | |
if (count >= 1 && c == '.') | |
{ actualString.Replace(c, ' '); actualString.Trim(); } | |
else | |
{ | |
if (Char.IsDigit(c)) | |
{ | |
actualString = actualString + c; | |
} | |
if (c == '.') | |
{ | |
actualString = actualString + c; count++; | |
} | |
else | |
{ | |
actualString.Replace(c, ' '); | |
actualString.Trim(); | |
} | |
} | |
} | |
return actualString; | |
} | |
private static string IntegerValidation(char[] enteredString) | |
{ | |
string actualString = string.Empty; | |
foreach (char c in enteredString.AsEnumerable()) | |
{ | |
if (Char.IsDigit(c)) | |
{ | |
actualString = actualString + c; | |
} | |
else | |
{ | |
actualString.Replace(c, ' '); | |
actualString.Trim(); | |
} | |
} | |
return actualString; | |
} | |
private static string AlphaNumericValidation(char[] enteredString) | |
{ | |
string actualString = string.Empty; | |
foreach (char c in enteredString.AsEnumerable()) | |
{ | |
if (Char.IsLetterOrDigit(c)) | |
{ actualString = actualString + c; } | |
else | |
{ | |
actualString.Replace(c, ' '); | |
actualString.Trim(); | |
} | |
} | |
return actualString; | |
} | |
/// <summary> | |
/// Limit users from entering an Integer value more than the threshold value. | |
/// </summary> | |
/// <param name="tbx">The Textbox to be validated</param> | |
/// <param name="threshold">Maximum value</param> | |
public static void LimitText(this TextBox tbx, int threshold) | |
{ | |
string enteredString = tbx.Text.Trim(); | |
char[] characters = enteredString.ToCharArray(); | |
string newString = IntegerValidation(characters); | |
if (!string.IsNullOrEmpty(newString)) | |
{ | |
int pageNo = Int32.Parse(newString); | |
if (!(pageNo <= threshold || threshold == 0)) | |
tbx.Text = threshold.ToString(); | |
else | |
tbx.Text = pageNo.ToString(); | |
tbx.SelectionStart = tbx.Text.Length; | |
} | |
else | |
tbx.Text = newString; | |
} | |
} | |
// IMPLEMENTATION | |
// TO BE IMPLEMENTED ON THE TEXT CHANGE EVENT OF A WIN FORM TEXTBOX | |
// SAMPLE CODE | |
private void tbxAmount_TextChanged(object sender, EventArgs e) | |
{ | |
tbxAmount.Validate(TextValidator.ValidationType.Amount); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SOURCE: https://stackoverflow.com/a/661706/3436775 | |
private delegate void SetControlPropertyThreadSafeDelegate( | |
Control control, | |
string propertyName, | |
object propertyValue); | |
public static void SetControlPropertyThreadSafe( | |
Control control, | |
string propertyName, | |
object propertyValue) | |
{ | |
if (control.InvokeRequired) | |
{ | |
control.Invoke(new SetControlPropertyThreadSafeDelegate | |
(SetControlPropertyThreadSafe), | |
new object[] { control, propertyName, propertyValue }); | |
} | |
else | |
{ | |
control.GetType().InvokeMember( | |
propertyName, | |
BindingFlags.SetProperty, | |
null, | |
control, | |
new object[] { propertyValue }); | |
} | |
} | |
// Implementation | |
// thread-safe equivalent of | |
// myLabel.Text = status; | |
SetControlPropertyThreadSafe(myLabel, "Text", status); | |
// For .Net 3.0 and above, Using Extensions | |
myLabel.SetPropertyThreadSafe("Text", status); | |
// FOR .NET 3.0 And above | |
private delegate void SetPropertyThreadSafeDelegate<TResult>( | |
Control @this, | |
Expression<Func<TResult>> property, | |
TResult value); | |
public static void SetPropertyThreadSafe<TResult>( | |
this Control @this, | |
Expression<Func<TResult>> property, | |
TResult value) | |
{ | |
var propertyInfo = (property.Body as MemberExpression).Member | |
as PropertyInfo; | |
if (propertyInfo == null || | |
!@this.GetType().IsSubclassOf(propertyInfo.ReflectedType) || | |
@this.GetType().GetProperty( | |
propertyInfo.Name, | |
propertyInfo.PropertyType) == null) | |
{ | |
throw new ArgumentException("The lambda expression 'property' must reference a valid property on this Control."); | |
} | |
if (@this.InvokeRequired) | |
{ | |
@this.Invoke(new SetPropertyThreadSafeDelegate<TResult> | |
(SetPropertyThreadSafe), | |
new object[] { @this, property, value }); | |
} | |
else | |
{ | |
@this.GetType().InvokeMember( | |
propertyInfo.Name, | |
BindingFlags.SetProperty, | |
null, | |
@this, | |
new object[] { value }); | |
} | |
} | |
// Implementation | |
myLabel.SetPropertyThreadSafe(() => myLabel.Text, status); // status has to be a string or this will fail to compile | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Bitmap b = new Bitmap (control.Width, control.Height); | |
control.DrawToBitmap(b, new Rectangle(0, 0, b.Width, b.Height)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment