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)
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:
<span class="keyword" style="color: #0000ff;">private</span> <span class="keyword" style="color: #0000ff;">void</span> button1_Click(<span class="keyword" style="color: #0000ff;">object</span> sender, EventArgs e)
<span class="keyword" style="color: #0000ff;">if</span> (colorDialog1.ShowDialog() != DialogResult.Cancel)
label1.Text = <span class="string" style="color: #ee3b3b;">"Here's my new color!"</span>;
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, <span class="keyword" style="color: #0000ff;">int</span> Line, <span class="keyword" style="color: #0000ff;">int</span> <span class="keyword" style="color: #0000ff;">Column</span>)
<span class="keyword" style="color: #0000ff;">int</span> offset = <span class="number">0</span>;
<span class="keyword" style="color: #0000ff;">for</span>(<span class="keyword" style="color: #0000ff;">int</span> i = <span class="number">0</span>; i < Line -<span class="number">1</span> && i < RTB.Lines.Length; i++)
offset += RTB.Lines[i].Length + <span class="number">1</span>;
RTB.<span class="keyword" style="color: #0000ff;">Select</span>(offset + <span class="keyword" style="color: #0000ff;">Column</span>, <span class="number">0</span>);
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.
11. Where does an ImageList control appear when you add it at the design time?
The ImageList control is a component; therefore, it appears in the component tray at the design time.
12. How can you programmattically prevent a Combobox from dropping, in .NET 4.0?
To avoid dropping of a Combobox, you need to override the WndProc() method and ignore WM_LBUTTONDOWNand WM_LBUTTONDBLCLK events.
13. What is the function of the CheckState property of the CheckBox control?
The CheckState property gets or sets the state of CheckBox.
If the ThreeState property is set to false, the CheckState property value can only be set toCheckState.Indeterminate in code and not by user interaction.
Checked – The CheckBox displays a check mark. The control appears sunken.
Unchecked – The CheckBox is empty. The control appears raised.
Indeterminate – The CheckBox displays a check mark and is shaded.
14. Write a code to select an item in the ListView control programmatically in C#?
To select an item from the ListView control, you can use the following code snippet:
<span class="comment" style="color: #009900;">//Make sure the listview has focus </span>
listview1.Items[i].Selected = <span class="keyword" style="color: #0000ff;">true</span>;
15. Differentiate between a TextBox control and RichTextBox control.
The TextBox control is an input control, which allows a user to enter text to an application at runtime. By default, it allows only single line text; however, you can change its property to accept the multiline text as well as scroll bar also.
The RichTextBox control is similar to the TextBox control with the difference that it allows the user to format its text also. You can format the text in various ways, such as bold, italic, and underlined as well as change its color and font. You can save your RichTextBox value to a RTF (Rich Text Format) file and load value of RTF file to the RichTextBox control.
16. Describe the ToolTip control. How can you associate it with other controls?
The ToolTip control generates a small pop-up window with explanatory text for an element It is displayed when the user pauses the mouse for a certain period over an element/control. Tool tips provide a quick help to user to understand about that element. To associate a tool tip with other control, you need to implement theSetToolTip() method.
17. What does the DialogResult property of a Button control do?
The DialogResult property retrieves or sets a value that is returned to the parent form when the button is clicked.
18. How do you create a separator in the Menu Designer?
You can use hyphen (-) to create a separator.
19. Define the TrackBar control.
The TrackBar control, also known as the slider control, works as a navigator to display a large amount of information or for visual adjustment of numeric setting. There are two parts in a TrackBar control – thumb (also known as slider) and tick marks. The thumb part acts as a slider. You can adjust the thumb part using the Value property. The tick marks are visual indicators that are spaced at regular intervals.
20. How does an MDI form differ from a standard form?
An MDI form closely resembles a standard form with one major difference-the client area of an MDI form acts as a container for other forms. It means that an MDI form, also known as an MDI parent form, can display MDI child forms inside it.
21. Which method provides the functionality to display a dialog box at runtime?
The ShowDialog() method is used to display the dialog box at run time.
22. What does the PerformStep() method do?
The PerformStep() method increases the value of Progress bar according to the amount set by the Stepproperty.
23. Write a method to get only the name of a file from the complete path string in C#.
Use a FileInfo class and instantiate its object with the full path as the constructor argument and then simply call the FileInfo.Name file and you will get just the name of the file.
24. What does the OpenFile() method of the OpenFileDialog control do?
The OpenFile() method opens the file selected by the user with read-only permission. The file is specified by the FileName property.
25. How do you retrieve the customized properties of a .NET application from the XML .config file?
Initialize an instance of the AppSettingsReader class. Call the GetValue() method of theAppSettingsReader class, passing in the name of the property and the type expected. Finally, assign the result to the appropriate variable.
26. What is the difference between a toolstrip drop-down button and a toolstrip split button?
The difference between a toolstrip drop-down button and a toolstrip split button is that a toolstrip split button is a combination of two controls – a push button and a drop-down button; whereas, a toolstrip drop-down button is a single control.
27. Which event of a TextBox control helps in restricting a text box from accepting numeric digits in .NET 4.0?
The KeyPress event of a text box is used to restrict it from accepting numeric digits or any other character.
28. How would you create an ellipse, which is a non- rectangular window?
Open a new Windows form, which is by default rectangular in design and then set the TransparencyKeyproperty to the same value as BackColor, which will effectively make the background of the form transparent. Then, set the FormBorderStyle property to FormBorderStyle.None, which removes the contour and contents of the form.
29. What does the Checked property of the DateTimePicker control do?
The Checked property holds either true or false value. It holds true, when the Value property hold a valid date-time value and is updatable; otherwise, false.
30. Name the classes used to handle standard menu in a MenuStrip control.
The two main classes used to handle standard menu in a MenuStrip control are:
- MenuStrip – Acts as a container for the menu structure of a form.
- ToolStripMenuItem – Supports the items in a menu system (including the menus, such as File and Edit).
31. How can you attach a horizontal scroll bar with the ListBox control?
You need to set the the MultiColumn property of the ListBox control to True to attach a horizontal scroll bar with it.
32. What is the difference between the Add() and Insert() methods of a ListBox control?
The Add() method simply adds an item into the list box; whereas, the Insert() method inserts an item at the specified index.
33. Consider a situation where you have added panels in a StatusBar control; however, they are not displayed at run time. What could be the reason for this?
To display panels in the StatusBar control, the ShowPanels property needs to be set to true.
34. What is the function of the SizeMode property of the PictureBox control?
The SizeMode property determines how the picture will be displayed in the PictureBox control. The following five enumerations are used to set the value of the SizeMode property:
- Normal – Represents Standard picture box behavior (the upper-left corner of the image is placed at upper-left in the picture box)
- StretchImage – Displays image according the PictureBox size
- AutoSize – Increases or decreases the picture size automatically as per the actual size of thePictureBox control.
- CenterImage – Displays the image in the center if it is smaller than the PictureBox control; otherwise, the center part of the image is placed in the PictureBox control and its outside edges are clipped
- Zoom – Helps in stretching or shrinking the image so that it fits the PictureBox control, by maintaining the aspect ratio of the image
35. How can you prevent users of an application from editing the text in the ComboBox controls in .NET 4.0?
The ComboBox class contains the DropDownStyle property, which is used to define the display style of the items in the ComboBox control. The DropDownStyle property accepts a value from the ComboBoxStyleenumeration, which contains three members to define the styles for the items: Simple, DropDownList, andDropDown. The DropDownList value of the ComboBoxStyle enumeration is selected to set a ComboBox control as non-editable by users, as shown in the following code snippets:
Code for VB:
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
Code for C#:
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
36. Which class manages the event and layout of all ToolStrip elements?
The ToolStripItem class manages the event and layout of all elements that the ToolStrip control contains.
37. How can you place a border around a picture box?
The PictureBox control offers the BorderStyle property, which can be set to define the style of its border. This property can accept any of the three values from Fixed3D, FixedSingle, or None. These properties can be easily set through code or through the Properties window of the Visual Studio IDE.
38. How do we format numbers, dates, and currencies in a text box?
Each type has a ToString() method that can used to format date, currencies, and numbers. You can also use the String.Format() method to format these things as well. To format dates, use the ToString() member of the DateTime type.
39. What is the use of the Panel control? Does it display at runtime?
Panels acts as a container to group other controls. It is an important control, when you want to show/hide a group of controls and relocate a number of controls simultaneously.
When you generate a new control at runtime, it works as a container control. As we know, it is a container control; therefore, it is not displayed at runtime.
40. Is it possible to add an image on the RadioButton control?
Yes, you can add an image on the RadioButton control by setting the Image property.
41. What is the use of a toolstrip container?
A toolstrip container is used to contain controls, such as ToolStrip, MenuStrip, and StatusStrip, so that these controls can be docked and moved at the run time.
42. Name the methods, available in .NET 4.0, that are used to add and delete items from a ListBox control?
The following methods can be used to add and delete items from a ListBox control. The Items.Add() andItems.Insert() methods are used to add items; whereas, the Items.Remove(), Items.RemoveAt(), andItems.Clear() methods are used to delete items from a ListBox control.
43. What is the importance of a Button control?
A Button control is an important Windows control, which provides the most common way of creating and handling an event in the code with the help of its Click event.
44. How can you unselect the selected items in a ListView control programmatically in .NET 4.0?
The syntax to unselect the selected items in the ListView control is shown in the following code snippets:
Code for VB:
Code for C#:
45. How can you get the text of the RichTextBox control, including all rich text format strings in .NET 4.0?
The Rtf property of the RichTextBox control is used to set or get texts, including the RTF format code.
46. What is the use of a Timer control? Can a Timer control pause?
The Timer control is a mechanism to perform an iterative task at a specified time interval. You cannot pause it because it can only start and stop.
47. What is die difference between a CheckBox control and a RadioButton control?
A CheckBox control is square shaped; whereas, a RadioButton control is round in shape. Moreover, you can select more than one CheckBox control from a group of CheckBox controls; whereas, you can select only a single RadioButton control from a group of RadioButton controls.
48. Can you write a class without specifying a namespace? Which namespace does it belong to by default?
Yes, we can write a dass without specifying namespace and that class belongs to a global namespace that has no name.
49. What are the three states set in the CheckState property of CheckBox?
50. How can you display an icon at runtime on the StatusStrip control?
The following code snippet shows the code to display an icon at runtime on the StatusStrip control:
toolStripStatusLabel2.Image = Bitmap.FromFile(“D:\\Indiabix\\Images\\1.bmp”);
51. Can you add more than one item simultaneously in the ListBox control?
Yes, You can add more than one item simultaneously in the ListBox control by using the AddRange() method.
52. What is the difference between a MenuStrip control and a ContextMenuStrip control?
The difference between a MenuStrip control and a ContextMenuStrip control is that a MenuStrip control is associated with the Windows Form; whereas, a ContextMenuStrip control is associated with a control, which is added to the Windows Form.
53. What are the values that can be assigned to the DialogResult property of a Button control?
The DialogResult property of a Button control can be assigned a value from the DialogResult enumerations, which are as follows:
- Abort-Returns Abort
- Cancel-Returns Cancel
- Ignore-Returns Ignore
- No-Returns No
- None-Nothing is returned from the dialog box
- OK-Returns OK
- Retry-Returns Retry
- Yes-Returns Yes
54. Why do you require user-defined controls?
User-defined controls are particularly useful in situations where you need to enhance the functionality of an existing control.
55. Is it possible to enter more than one line in a TextBox control?
Yes, it is possible to enter more than one line in a TextBox control. To do this, you need to set the Multilineproperty of the TextBox control to True. You can set this property at design time as well as runtime. The syntax to set this property at runtime is as follows:
Textbox1.Multiline = true;
56. How can you enable a text box to change its characters format, so that users can enter password?
You can set the PasswordChar property of the TextBox class to True to enable it to accept passwords. The code to change the PasswordChar property of the TextBox class is given as follows:
textBox1.PasswordChar = ‘*’;
57. What does the TickFrequency property of the TrackBar control do?
The TickFrequency property gets or sets a value that specifies the distance between ticks. By default, the distance between ticks is 1.
58. Is it possible to associate a control with more than one ContextMenu control?
No, we cannot associate a control with more than one ContextMenu control.
59. What is the difference between the Panel and GroupBox control?
The Panel and GroupBox controls both can be used as a container for other controls, such as radio buttons and check box. The main differences between a Panel and a GroupBox control are as follows:
- Panel does not display captions, while GroupBox do
- Panel has scrollbar, while GroupBox does not
60. Does a Timer control appear at run time?
Timer is a component; therefore, it does not appear at run time.
61. What is the difference between a ListBox control and a ComboBox control?
With a ListBox control, the user can only make a selection from a list of items; whereas, with a ComboBoxcontrol, the user can make a selection from the list of items as well as can add custom entry and select the same.
62. What is the function of MinDate and MaxDate properties of the MonthCalender control?
The MinDate and MaxDate properties allow users to get and set the minimum and maximum allowable date.
63. Name the parent class for all Windows controls.
The Control class or System.Windows.Forms.Control class is the parent class for all Window controls.
64. What is the MaskedTextBox control? What does the Mask property do?
The MaskedTextBox control is an improvement of the TextBox control. It forces the user to provide the proper input, which is specified by the Mask property. In other words, it prevents the user to provide any invalid input to an application. The Mask property gets or sets the input type to the MaskedTextBox control. There are many built-in formats for the Mask property, such as phone no., short date, time, zip code, and custom.
65. How can you adjust the height of a combo box drop-down list?
You can control the height of a combo box drop-down list by setting the MaxDropDownItems property of the combo box. The MaxDropDownItems property sets the maximum number of entries that will be displayed by the drop-down list.
66. How can you enforce a text box to display characters in uppercase?
The TextBox class contains the CharacterCasing property, which is used to specify the case of the content for a text box. This property accepts a value from the CharacterCasing enumeration of .NET Framework. The members specified in the CharacterCasing enumeration are Lower, Upper, and Normal. You can select any one of these enumerations as a value for the CharacterCasing property of a specified text box, as shown in the following code snippet:
textBox1.CharacterCasing = CharacterCasing.Upper;
67. Is it possible to associate a control with more than one ContextMenu?
No, we cannot associate a control with more than one ContextMenu.
68. How can you check/uncheck all items in the CheckedListBox control in .NET 4.0?
To check all items in .NET, you can use the following code snippet:
Code for VB:
Dim i <span class="keyword" style="color: #0000ff;">as</span> <span class="keyword" style="color: #0000ff;">Integer</span>
<span class="keyword" style="color: #0000ff;">For</span> i = <span class="number">0</span> <span class="keyword" style="color: #0000ff;">To</span> myCheckedListBox.Items.<span class="aggregate" style="color: #ee3b3b;">Count</span> - <span class="number">1</span>
myCheckedListBox.SetItemChecked(i, <span class="keyword" style="color: #0000ff;">True</span>)
<span class="keyword" style="color: #0000ff;">Next</span>
Code for C#:
<span class="keyword" style="color: #0000ff;">for</span>( <span class="keyword" style="color: #0000ff;">int</span> i = <span class="number">0</span>; i < myCheckedListBox.Items.<span class="aggregate" style="color: #ee3b3b;">Count</span>; i++ )
myCheckedListBox.SetItemChecked(i, <span class="keyword" style="color: #0000ff;">true</span>);
69. How can we disable the context menu for a TextBox control?
The TextBox class contains the ContextMenuStrip property. When we set this property to a dummy instance of the ContextMenu class, the TextBox control is unable to provide any context menu on the right-click of the mouse.
70. How can you move and resize a control on a Windows form?
You can make use of the SetBounds() method to move as well as resize the control on a Windows form.
71. What is use of the DropDownStyle property of the ComboBox control?
The DropDownStyle property changes the style of the ComboBox control. It consists of Simple, DropDown, andDropDownList as its values. When you select Simple, the list of items are displayed as a ListBox control. When you select DropDown, the list is displayed in a drop down style. When you select DropDownList, the list displayed in a drop down style and you cannot edit its text.
72. What is the difference between pixels, points, and em‘s when fonts are displayed?
A pixel is the lowest-resolution dot that the computer monitor supports. Its size depends on user’s settings and the size of the monitor. A point is always 1/72 of an inch. An em is the number of pixels it takes to display the letter M.