Friday, December 26, 2014

Windows Controls

1. How can we auto size a button to fit its text?
The Button control has the AutoSize property, which can be set to true or false. If we set the value of theAutoSize property to true, then the button control automatically alters its size according to the content displayed on it.
2. How can we display an icon or a bitmap image on the Button control?
The Button class contains the Image property, which is used to set an image on the Button control. We can also set the alignment of the image by using the ImageAlign property of the Button class.
3. Which method is used to generate the click event of the Control class for the Button control in C#?
The PerformClick() method of the Button class is used to generate the Click event of theSystem.Windows.Forms.Control class.
4. A Windows Form will not show the Minimize, Maximize, and Close buttons, if the ControlBox property of the form is set to False. (True/False)
True.
5. How is anchoring different from docking?
Docking refers to attaching a control to either an edge (top, right, bottom, or left) or the client area of the parent control. On the other hand, anchoring is a process in which you need to specify the distance that each edge of your control maintains from the edges of the parent control.
6. How can you display a default value in the text box of an input box?
You can display a default value in the text box of an input box by using the DefaultResponse argument of theInputBox() function.
7. How will you pick a color from the ColorDialog box?
To pick a color from the color dialog box, you need to create an instance of the ColorDialog box and invoke to the ShowDialog() method. The code to display the color dialog box and set the BackColor property of the Label control similar to the color selected in the color dialog box control is:
private void button1_Click(object sender, EventArgs e)
{
 if (colorDialog1.ShowDialog() != DialogResult.Cancel)
 {
 label1.Text = "Here's my new color!";
 label1.BackColor = colorDialog1.Color; 
 }
}
8. How can you get or set the time between Timer ticks?
There is an Interval property, which is responsible to get and set the time in milliseconds.
9. How can you programmatically position the cursor on a given line or on a character in the RichTextBoxcontrol in C#?
The RichTextBox control contains the Lines array property, which displays one item of an array in a separate line. Each line entry has a Length property, which can be used to accurately position the cursor at a character, as shown in the following code snippet:
private void GoToLineAndColumn(RichTextBox RTB, int Line, int Column)
{
 int offset = 0;
 for(int i = 0; i < Line -1 && i < RTB.Lines.Length; i++)
 {
 offset += RTB.Lines[i].Length + 1;
 }
 RTB.Focus();
 RTB.Select(offset + Column, 0);
}
10. What is the difference between the WindowsDefaultLocation and WindowsDefaultBounds properties?
The WindowsDefaultLocation property makes the form to start up at a location selected by the operating system, but with internally specified size. The WindowsDefaultBounds property delegates both size and starting position choices to the operating system.

No comments: