|
What does it mean by docking of controls?
|
|
Docking is the property of the control to attach itself to a particular edge of the window (or other containing control). You can either dock a control to an edge or to fill the available space in the parent control or window. Common examples of docking includes menu bar and toolbars which dock themselves at the top of the window so that they may remain at top regardless of the size and of the window.
|
|
|
|
|
|
|
|
What base class do all Winforms inherit from?
|
|
System.Windows.Forms.Form
|
|
|
|
|
|
|
|
How do I prevent the beep when the user presses the Enter key in a TextBox?
|
|
You can prevent the beep when the enter key is pressed in a TextBox by deriving the TextBox and overriding OnKeyPress -
public class CustomTextBox : TextBox {
protected override void OnKeyPress( KeyPressEventArgs e )
{
if ( e.KeyChar == (char) 13 )
e.Handled = true;
else
base.OnKeyPress( e );
} }
|
|
|
|
|
|
|
|
How can I use a TextBox to enter a password?
|
Set the TextBox.PasswordChar property to the character that you want to use for password -
textBox1.PasswordChar = '\u25CF';
|
|
|
|
|
|
|
|
How can I run an EXE from within my application?
|
|
Use the Process class found in the System.Diagnostics namespace.
Process proc = new Process();
proc.StartInfo.FileName = @"Notepad.exe";
proc.StartInfo.Arguments = "";
proc.Start();
|
|
|
|
|
|
|
|
How do I load a picture into the PictureBox control?
|
|
To load a Picture into the PictureBox control drag a PictureBox control and a Command Button from the Toolbox. When you click the Command Button, the picture you specified will be loaded into the Picturebox. The following code is a demonstration - PictureBox1.Image=Image.FromFile("C:\images\image.gif")
//Assuming you have a folder named images in C: drive and a gif image in that
|
|
|
|
|
|
|
|
How do I make a TextBox use all upper-case (or lower-case) characters?
|
|
Use the CharacterCasing property of the TextBox.
textBox1.CharacterCasing = CharacterCasing.Upper;
textBox2.CharacterCasing = CharacterCasing.Lower;
|
|
|
|
|
|
|
|
What is Global Assembly Cache ?
|
|
GAC is an area of memory reserved for storing the assemblies of all .Net applications running on a given computer. The GAC is required by side-by-side execution as well as for sharing assemblies among multiple .Net applications. An assembly must have a strong name and be public—shared assembly—to be installed in the GAC. The Global Assembly Cache Tool adds and removes assemblies from the GAC.
|
|
|
|
|
|
|
|
How do I add custom drawing to a Button?
|
|
Subclass Button and add a custom Paint event handler that does you custom drawing.
public class CustomButton : Button
{
public CustomButton()
{
Paint += new PaintEventHandler( ButtonPaint );
}
private void ButtonPaint( object sender, PaintEventArgs e )
{
Pen pen = new Pen( Color.Red );
pen.Width = 8;
e.Graphics.DrawLine( pen, 7, 4, 7, Height - 4 );
pen.Width = 1;
e.Graphics.DrawEllipse( pen, Width - 16 , 6, 8, 8 );
}
}
|
|
|
|
|
|
|
|
How do I trigger a Button Click event?
|
|
Use the Button's public method PerformClick().
|
|
|
|
|
|
|
|
How do I put a bitmap or icon on a Button?
|
|
You can use the Button.Image property to add an image to a button face. Use the Button.ImageAlign (and possibly Button.TextAlign) to layout your button face.
You can also implement custom drawing on a button face.
|
|
|
|
|
|
|
|
How do I resize a Button to fit its Text?
|
|
Get a Graphics object for the button and use its MeasureString method to compute the width you need.
using ( Graphics g = button1.CreateGraphics() )
float w = g.MeasureString( button1.Text, button1.Font ).Width;
button1.Width = (int) w + 12; // 12 is for the margins
|
|
|
|
|
|
|
How do I disable pasting into a TextBox? OR
How do I prevent the user from being able to paste from the clipboard into a TextBox, e.g., through Ctrl+V and the context menu?
|
|
First set the ContextMenu property of the TextBox to a dummy, empty ContextMenu instance. This will prevent the default context menu from showing. Then, override the ProcessCmdKey method as follows in a TextBox derived class -
protected override bool ProcessCmdKey( ref Message msg, Keys keyData ){
if ( keyData == (Keys.Control | Keys.V) )
return true;
else
return base.ProcessCmdKey( ref msg, keyData );
}
|
|
|
|
|
|
|
|
How to use the Splitter control?
|
|
The Splitter control is used to resize other controls. The purpose of this is to save space on the form. This control can be very useful when you are working with controls both at design time and run time (which are not visible at design time).
|
|
|
|
|
|
|
|
Can you write a class without specifying namespace? Which namespace does it belong to by default?
|
|
Yes, you can. Then the class belongs to global namespace which has no name. For commercial products, naturally, you would not want global namespace.
|
|
|
|
|
|
|