0% found this document useful (0 votes)
20 views

C# Unit - IV

Uploaded by

btechcse21052
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C# Unit - IV

Uploaded by

btechcse21052
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

UNIT IV

2 Marks

1. What is Toolbox ?

Toolbox is very-very important building block of . NET Framework. Toolbox is an


area where all the controls existed. It helps the developer to develop any application
very quickly, only drag the control from the toolbox and drop it on the form.

2. What is Container Control ? (May-22)


A ContainerControl represents a control that can function as a container for
other controls and provides focus management. Controls that inherit from this class can
track the active control they contain, even when the focus moves somewhere within a
different container.

3. List out examples for Container


Control.
The examples are:
 Form
 Panel
 GroupBox
 TabControl

4. What is Windows forms ?


Windows Forms is a Graphical User Interface(GUI) class library which is
bundled in .Net Framework. Its main purpose is to provide an easier interface to
develop the applications for desktop, tablet, PCs. It is also termed as the WinForms.

5. What is a TextBox ?
TextBox is a graphical control element that allows the user to enter and edit
text. A TextBox control can be placed on a form, and its appearance and behavior canbe
customized using various properties, such as font size, and more. By default, a
TextBox control can accept single-line text input, but it can also be set to accept
multiline input.

6. What is a ComboBox ?
ComboBox is a graphical control element that provides a dropdown list of
items from which the user can select. It is commonly used to provide a list of options
for the user to choose from. The items in the dropdown list can be added at design
time or runtime, and can be populated from a database or other data source.

7. What is ListBox ?

1
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

ListBox is a graphical control element that displays a list of items to the user.
It allows the user to select one or more items from the list.. The items in the ListBox
can be added at design time or runtime, and can be populated from a database or other
data source.

8. What is the difference between ListBox and ComboBox ?

ListBox ComboBox
The items are displayed in a scrollable The items are displayed in a dropdown
list for the user to select. menu.
ListBox can display multiple items at ComboBox can display only one item at
once. a time.
ListBox is suitable when the user needs omboBox is more suitable when the user
to select multiple items from a list. needs to select a single item from a list.

9. What is MenuStrip ?
MenuStrip is a graphical control element that provides a menu bar at the top of
a form. It allows the user to access various commands or features of the application.
The items in the MenuStrip are typically organized into menus and submenus, with
each item representing a command or feature of the application.

10. What is ToolStrip?


ToolStrip is a graphical control element that provides a toolbar at the top or
bottom of a form. It allows the user to access various commands or features of the
application.The items in the ToolStrip are typically organized into buttons, dropdowns,
text boxes, and other controls.

11. Difference between MenuStrip and ToolStrip

MenuStrip ToolStrip
MenuStrip is used to create menus with ToolStrip is used to create toolbars with
items . buttons and other controls.
MenuStrip is typically displayed at the ToolStrip is typically displayed below the
top of a form MenuStrip or in some other location on
the form.
MenuStrip usually contains text-based ToolStrip can contain a mix of text and
menu items image-based controls.

MenuStrip is often used to create the ToolStrip is often used to provide quick
main menu of an application access to frequently used commands or
tools.

12. What is Form Designer ?


Form Designer is a visual design tool that allows developers to create and
design the user interface of the form in a WYSIWYG (What You See Is What You
Get) manner. It is typically integrated into the IDE and provides an intuitive way to
create and edit the form's layout, properties, and controls.

2
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

13. What are the layouts provided by Form Layout ?


Some of the layouts provided are :
 Flow Layout
 Grid Layout
 Anchor Layout
 Dock Layout
 Table Layout

14. What is Anchor Layout ?


Anchor layout is a type of layout that allows the controls on a form to resize
and reposition dynamically as the form is resized. This can be useful for ensuring that
the user interface of the application remains usable and readable on different screen
sizes and resolutions.

15. What is Dock Layout ?


Dock layout is a type of layout that allows the controls on a form to fill the
available space in the form and resize dynamically as the form is resized. This can be
useful for creating user interfaces that adjust to different screen sizes and resolutions.

16. What are some of the anchor properties of a control ?


 Anchor.None
 Anchor.Top
 Anchor.Left
 Anchor.Right
 Anchor.Top|Anchor.Right
 Anchor.Top|Anchor.Left
 Anchor.Bottom|Anchor.Right
 Anchor.Bottom|Anchor.Left

17. What are some of the Dock properties ?


 Dock.None
 Dock.Top
 Dock.Bottom
 Dock.Right
 Dock.Left
 Dock.Fill

18. What is Menu ?


Menu is a graphical control element that provides a menu bar at the top of the
form for the user to access various commands or features of the application. The
menu bar typically contains one or more top-level menu items, which can be
expanded to show a list of sub-menu items.

19. What is ToolTip Controls ?


ToolTip controls provide a way to display helpful information or a description
of a control when the user hovers the mouse over the control. IIt can be added to a

3
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

form and associated with specific controls, such as buttons or textboxes, during design
time using the Properties window.

20. List the properties of ToolTip


Controls
Some of the common properties are :
 InitialDelay
 AutoPopDelay
 ReshowDelay
 ToolTipTitle

21. How to associate ToolTip Control with a specific control ?


 Select the control on the form
 Set its ToolTip property to name of its ToolTip control
 Once a ToolTip control is associated with a control, you can set the text of the
ToolTip by setting the control's ToolTipText property

22. What is RunTime ?


It refers to the period of time during which a program is executing. The
runtime environment for C# is provided by the .NET Framework.
To measure the runtime of a specific section of code in C#, you can use the Stopwatch
class.

23. What is stopwatch ?


Stopwatch class is used to measure the runtime of the code between the Start()
and Stop() method calls. The elapsed time is then printed to the console using the
Elapsed property of the Stopwatch class.

24. What is Graphics Programming ?


Graphics programming is the process of creating visual representations of data
and information using computer software. It involves creating images, animations,
and other visual effects for a wide range of applications, including video games, films,
simulations, and scientific visualization.

25. What is graphics APIs ?


The graphics APIs provide a set of functions and tools that programmers can
use to create complex graphical effects and optimize the performance of their
applications. Some of the most popular graphics APIs include OpenGL, DirectX, and
Vulkan.

26. How to create graphic object ?


Graphics object can be created by associating it with a Control object, such as
a Form, PictureBox, or Panel. The Graphics object provides methods and properties
for drawing lines, shapes, and text on the associated Control.

27. List the components of Graphics object.


 Drawing surface
 Drawing methods
 Colors
4
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

 Pens
 Brushes
 Fonts
 Transformations

28. What is a PenClass ?


A pen draws a line of specified width and style. You always use Pen
constructor to create a pen. The constructor initializes a new instance of the Pen class.
You can initialize it with a color or brush.

29. What is a Brush Class ?


The Brush class is an abstract base class and cannot be instantiated. We
always use its derived classes to instantiate a brush object, such as SolidBrush,
TextureBrush, RectangleGradientBrush, and LinearGradientBrush.

30. What is a ToolTip ?


A tooltip is a small pop-up window that displays some information when you
rollover on a control. Tooltip class represents a tooltip control. Once a Tooltip object
is created, we need to call SetToolTip method and pass a control and text.

31. What is RunTime ?


Runtime refers to the period of time during which a program is executing or
running. The runtime environment is responsible for managing the execution of a
program, including loading and executing the program's code, allocating memory,
managing resources, and handling errors and exceptions that occur during execution.

32. What is GroupBox ?


A GroupBox is a container control that provides a border around a group of
related controls. GroupBoxes are often used to visually separate different sections of a
form or dialog.

33. What is Panel Control ?


Panel control is a container control that provides a rectangular area for other
controls to be placed. Panels are often used to group related controls together, or to
provide a background or border for other controls.

34. List out the properties of Panel Control .


Some of the properties if panel control are:
 Backcolor
 Border Style
 Auto scroll
 Dock
 Height and width
 Padding
 Anchor
 Visible
 Location
 TabIndex

35. What is TabControl ?

5
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

TabControl is a container control that allows the user to switch between


multiple pages or tabs of content. Each page in the control is represented by a separate
tab that the user can click on to switch between views.
The TabControl is a useful control for organizing and presenting large amounts of
information or functionality in a compact and organized manner, and is commonly
used in Windows Forms applications.

36. List out the properties of TabControl.


Some of the important properties of TabControl:
 Appearance
 BackColor
 DockFont
 ImageList
 ImageSize
 Padding
 SelectedIndex
 TabCount
 TabPages
 TextAlign

37. Write the syntac to create Graphics


Object.
38. The syntax to create Graphics Object is,
Graphics graphicsObject = controlObject.CreateGraphics();
where controlObject refers to the control on your form that you want to draw on
graphicsObject is the Graphics object that you will use to draw on the control.

39. How to free up the resources used by Graphics Object ?


When you are finished drawing, be sure to release the Graphics object by
calling its Dispose method.
Syntax: g.Dispose();
This will free up the resources used by GraphicsObject.

40. What is BackgroundWorker ?


BackgroundWorker provides a simple way to run lengthy operations on a
separate thread, without blocking the user interface of your Windows Forms
application. By running these operations on a background thread, your application
remains responsive and the user can continue to interact with other controls while the
operation is running.

41. What is DoWork event handler ?


It is a method that contains the code that you want to run on a separate thread
when using the BackgroundWorker class. This method is called automatically by the
BackgroundWorker when you call its RunWorkerAsync method, which starts the
operation on a separate thread.

6
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

5 Marks

1. Explain about windows forms?


Windows Forms Basics
• A Windows forms application is one that runs on the desktop computer.
• A Windows forms application will normally have a collection of controls such as labels,
textboxes, list boxes, etc.
Creating Forms
• Step 1) The first step involves the creation of a new project in Visual Studio. After
launching Visual Studio, you need to choose the menu option New->Project.
• Step 2) The next step is to choose the project type as a Windows Forms application. Here
we also need to mention the name and location of our project.
• Step 3) Add Controls to the Form

• Step 4) Write code


• Step 5) Run the program

• Windows Forms in Visual Studio has four important components that you'll interact with
as you create an app:
Solution Explorer
• All of your project files, code, forms, resources, will appear in this pane.
Properties

7
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

• This pane shows property settings you can configure based on the item selected.
• For example, if you select an item from Solution Explorer, you'll see property settings
related to the file.
• If you select an object in the Designer, you'll see settings for the control or form.

Form Designer

• This is the designer for the form. It's interactive and you can drag-and-drop objects from
the Toolbox. By selecting and moving items in the designer, you can visually compose the
user interface (UI) for your app.

Toolbox
• The toolbox contains all of the controls you can add to a form. To add a control to the
current form, double-click a control or drag-and-drop the control.

2. How to add controls to the form and handle events?


Add controls to the form
• With the Form1 form designer open, use the Toolbox pane to add the following controls to
the form:
• Label

• Button
• Listbox
• Textbox

• If you double click the control it will be added into your form.
• You can position and size the controls according to your requirement

• click on each control and configure the settings in the Properties pane.
• You can also click on the form title area to select the form

8
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

• Text property of Label, Form and Button are changed. Also name property of listbox is
changed to Name.
• You should have a form in the designer that looks similar to the following:

Handle events

• Now that the form has all of its controls laid out, you need to handle the events of the
controls to respond to user input. With the form designer still open, perform the following
steps:

• Select the button control on the form.


• In the Properties pane, click on the events icon to list the events of the button.
• Find the Click event and double-click it to generate an event handler.
• This action adds the following code to the the form:
private void button1_Click(object sender, EventArgs e)

{
}
Add the following code
private void button1_Click(object sender, EventArgs e) {
if (!string.IsNullOrWhiteSpace(textBox1.Text) && !lstNames.Items.Contains(textBox1.Text))
lstNames.Items.Add(textBox1.Text);
}

• Run the app. (press F5 or Debug -> start without debugging) Enter some name in textbox
and click the button. The name will be added in the text box

9
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

3. Write notes on message box.


Messagebox
• C# MessageBox in Windows Forms displays a message with the given text and action
buttons.
• Show method used to display a message box with a message and action buttons.

• Various forms of Messagebox


• MessageBox.Show(“Welcome”);
• MessageBox.Show(“Welcome”,”Title”);
• MessageBox.Show(“Welcome”,”Title”,MessageBoxButtons.YesNo);
• MessageBox.Show(“Welcome”,”Title”,MessageBoxButtons.YesNo,M essageBoxIcon.Error);

Displaying Messagebox
• Design a form with a button.
• Add the following code to its click event.
• If you run this app it will show this button, click it and it will show a dialog box.
private void button1_Click(object sender, EventArgs e)
{

MessageBox.Show("welcome","Title",MessageBoxButtons.YesNo,Mess ageBoxIcon.Error);
}

4. List the controls in the toolbox.


Toolbox
• The Toolbox window displays controls that you can add to Visual Studio projects.

• To open Toolbox, choose View > Toolbox from the menu bar, or press Ctrl+Alt+X.
Controls
• Button Allows the user to run a separate subroutine.

10
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

• CheckBox Allows the user to either select or deselect an option.


• CheckedListBox Allows the user to select multiple items from a list of items.
• ComboBox Allows the user to either select an item from a dropdown box or enter a
different item.
• DataGridView Allows the user to display (and edit) a table of information.

• DateTimePicker Allows the user to select a specific date and/or time.


• DHTML Editing Control Allows the user to display a web page (not supported).
• FolderBrowseDialog Allows the user to browse, create and select folders (not files).
• GroupBox Allows you to group other controls together.
• ImageList Allows you to have a repository of images in your assembly.

• Label Allows you to display text to the user.


• LinkLabel Allows you to include hyperlinks in your text.
• ListBox Allows the user to select an item from a list of items.
• ListView Allows the user to select a single (or multiple) items from a list with multiple
columns.
• MenuStrip Allows the user to group commands by a common theme.

• NumericUpDown Allows the user to display numeric values.


• Panel Allows the user an area for grouping controls and scrolling.
• PictureBox(Image) Allows you to display a graphic or picture.
• ProgressBar Allows the user to indicate progress using a graphical bar.
• RadioButton(OptionButton) Allows the user to select from a list of possible distinct
options.
• RefEdit RichTextBox Allows the user to display text with formatting.

• SplitContainer Allows you to split an area into two resizeable panels.


• StatusStrip Allows you to add a status bar to your window.
• TabControl Allows you to display multiple tabs of the same controls.
• TableLayoutPanel TextBox(EditBox) Allows you to display text and to allow the user to
enter information.
• ToolTip Allows you to display text when you hover over other controls.
• ToolStrip Represents a panel in a StatusStrip control. • ToolStripButton Represents a
selectable item for a ToolStrip control.

11
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

• TreeView Allows you to display a hierarchy of nodes with parents and child. • VScrollBar
Allows the user to drag a button in order to select a value. • WebBrowser Allows the user to
include a web browser on a form
Often used controls are

• Button
– Text property, -> Displays as caption

– Click Event
• Checkbox
– Text property -> Text displayed with Checkbox
– Checked property -> to select or deselect this property will be used. At runtime
– (CheckBox1.Checked) returns true when it is selected.

• RadioButton
– Text property -> Text displayed with Checkbox
– Checked property -> to select or deselect this property will be used. At runtime
– (radioButton1.Checked) returns true when it is selected.
• Combobox

– Text property -> display as tcaption


– Items property -> To add items at design time
– At run time Combobox1.Items.Add(“Value”);
• Listbox
– Text Property -> Displays as caption

– Items property -> To add items at design time


– At run time Combobox1.Items.Add(“Value”)
• Textbox
• Text Property -> value at the textbox.
• RichTextBox

• Text Property -> Value in the control. At design time for new line press enter to accept
press ctrl+enter.

5. Design a simple form.


private void button2_Click(object sender, EventArgs e)

12
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

{
textBox1.Text = "";
textBox2.Text = "";

checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;

checkBox4.Checked = false;
radioButton1.Checked = false;
radioButton2.Checked = false;

comboBox1.Text = "CSE";
}
private void button1_Click(object sender, EventArgs e)
{

String name;
name = textBox1.Text;
MessageBox.Show("Hi " + name + " you have registered successfully");
}

13
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

6. Write short notes on the Groupbox control and Panel control.


Container control
Container controls are specialized controls that serve as a customizable container for other
controls. Examples of container controls include the Panel, FlowLayoutPanel and
SplitContainer controls. Container controls give the form logical and physical subdivisions
that can group other controls into consistent Ut subunits. For example, you might contain a
set of related radiobutton controls in a Groupies control. The use of container controls helps
create a sense of style or information flow in your us and allows you to manipulate
contained controls in a consistent fashion.
When a container control holds other controls, changes to the properties of the container
control can affect the contained controls. For example, if the Enabled property of a panel is
set to False, all of the controls contained in the panel are disabled. Likewise, changes to
properties related to the UI, such as BackColor, Visible, or Font, are also applied to the
contained controls. Note that you can still manually change any property inside a contained
control, but if the container is disabled, all controls inside that container will be inaccessible
regardless of their individual property settings.
The GroupBox control
The Groupbox control is a container control that appears as a subdivision of the form
surrounded by a border, it does not provide scrollbars, like the Panel control, nor does it
provide any kind of specialized layout capabilities. A Groupbox can have caption, which is set
by the Text property, or it can appear without a caption when the Text property is set to an
empty string.

14
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

The Panel control


The Panel control creates a subsection of a form that can host other controls. The Panel can
be indistinguishable from the rest of the surrounding form, or it can be surrounded by a
border as determined by the BorderStyle property. A Panel can have a BorderStyle property
of None, which indicates no border: FixedSingle, which indicates a single edge around the
Panel; or Fixed 30, which represents a border with a three- dimensional appearance.
The Panel control is a scrollable control, which means that it supports horizontal and vertical
scroll bars. Controls can be hosted in the Panel outside of its visible bounds When the
AutoScroll property is set to True, scroll bars will automatically be available if any controls
are placed outside of the visible bounds of the control: If the AutoScroll property is set to
False, controls outside the visible bounds of the Panel are accessible Important properties of
the Panel control are shown in Table 2-1.

Important Properties of panel control

The TableLayout Panel control

The TableLayoutPanel control is a specialised panel that aids in the design and layout of the
ui. The Tablelayout Panel is essentially a table that provides cells for the individual hosting of
controls. Like other panels, it is a scrollable container that provides scroll bars when the
AutoScroll property is set to True.

15
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

7. Write about the TabControl control.


The TabControl Control
The TabControl control enables you to group sets of controls in tabs, rather like files in a
filing cabinet or dividers in a notebook. For example, you might create property pages for an
application in which each page represents the properties of a specific component. The
TabControl serves as a host for one or more TabPage controls, which themselves contain
controls. The user can switch between tab pages (and the controls contained therein) by
clicking the tabs on the TabControl.

The most important property of the TabControl is the TabPages property. TabPage controls
are specialized container controls that are hosted only inside TabControl controls. Each
TabPage has its own set of properties, and you can access these properties by editing the
TabPages property at design time. This launches the TabPage Collection Editor, as shown in
the following Figure:

16
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

Individual TabPage controls are a lot like Panel controls. They are scrollable controls and will
generate scroll bars as needed if the AutoScroll property is set to True. Individual TabPage
controls also have a Text property, which represents the text shown in the tab that
represents this page in the TabControl. Also, like Panel controls,

TabPages have a BorderStyle property that can be set to None, FixedSingle, or Fixed3D, with
results similar to those in the Panel control.
The TabControl has several properties that you can use to customize the look and feel of the
control. The Appearance property controls how the tabs look. This property can be set to
Normal, Buttons, or FlatButtons, each of which generates a different visual style. The
Alignment property determines whether the tabs appear on the Top, Bottom, Left, or Right
of the TabControl. The TabControl also has a property called Multiline, which indicates if
more than one row of tabs is allowed. When it is set to True, multiple rows of tabs are
supported. When it is set to False, only a single row of tabs is allowed. Important properties
of the TabControl control and TabPage control are shown in Table 2-4 and Table 2-5,
respectively.
Important properties of TabControl control

Program for tab control


• Open a new project
• Drag and drop tab control.
• PROPERTIES OF TAB CONTROL
• Right-click on the tab control and select properties. You can see the tab page called
"Collection Editor".
• To add an additional tab, please click the add button.

• To remove a tab, select it and click remove.


• You can change the background color of tabs, the font
• Drag and drop the button control on both tab pages from the toolbox onto the form
CODING
private void button1_Click(object sender, EventArgs e) {
MessageBox.Show("Button from view 1 "); }

17
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

private void button2_Click(object sender, EventArgs e) {


MessageBox.Show("Button from view 2 ");
}

8. Write short notes on Menu control.(May-22)


• A Menu on a Windows Form is created with a MainMenu object, which is a collection
of MenuItem objects.
• MainMenu is the container for the Menu structure of the form and menus are made
of MenuItem objects that represent individual parts of a menu.
• You can add menus to Windows Forms at design time by adding the MainMenu
component and then appending menu items to it using the Menu Designer.

• After drag the Menustrip on your form you can directly create the menu items by
type a value into the "Type Here" box on the menubar part of your form.
• From the following picture you can understand how to create each menu items on
mainmenu Object.

18
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

• If you need a seperator bar, right click on your menu then go to insert->Seperator.

• After creating the Menu on the form, you have to double click on each menu item
and write the programs there depending on your requirements.
• The following C# program shows how to show a messagebox when clicking a Menu
item.
using System;

using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form

{
public Form1() {
InitializeComponent();
}
private void menu1ToolStripMenuItem_Click(object sender, EventArgs e) {

MessageBox.Show("You are selected MenuItem_1");

}
}
}

19
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

9. How to set up toolstrip control? (May-22)


Toolbar
• ToolStrip control provides functionality of Windows toolbar controls in forms

• To create a ToolStrip control at design-time, simply drag and drop a ToolStrip control
from Toolbox onto a Form.

By default, a ToolStrip control is docked at the top of the Form.

• The next step is to set ToolStrip control properties.


• Set BackColor to green and GripStyle to Visible.

• A ToolStrip control is nothing but a container without adding its child controls.
• A ToolStrip control is capable of hosting Button, Label, SplitButton, DropDownButton,
Separator, ComboBox, TextBox and ProgressBar controls.
• Add controls to ToolStrip control.
• If you click on a little dropdown handle on a ToolStrip control in designer, you will see
a list of possible controls that can be added to a ToolStrip.

20
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

• To add a control, simply select that control and it will be added to the ToolStrip
control.
• Another way to add controls to a ToolStrip control is by using the Items property.

• Click on the Items property and you will see Item Collection Editor
• We can add a Button, a TextBox, and a Label control to ToolStrip with a few
separators.
• Once these controls are added to a ToolStrip control, they all act as individual
controls and you can access them by using their Name property anywhere in the
code.
• Add three Button controls, three separators and a Label control.

• we are going to set an icon for the Button controls by setting Image property of
Button.

• Once you click on the browse option to browse an image, Resource editor window
pops up where you can import an image. Click on Import button and browse an
image file.

21
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

• Next step is to set the button click event handler. It is easy. Just double click or use
Events window and set click event handlers for all three buttons.
• private void toolStripButton1_Click_1(object sender, EventArgs e) {

• toolStripLabel1.Text = "Button1 is clicked"; }


• private void toolStripButton2_Click_1(object sender, EventArgs e) {

• toolStripLabel1.Text = "Button2 is clicked"; }


• private void toolStripButton3_Click_1(object sender, EventArgs e) {
• toolStripLabel1.Text = "Button3 is clicked"; }
• Now if you run the project and click on Buttons, you will see something like this.

10. How to add ToolTip control at design time and run time? (May-22)
Tool Tip control

• It represents a tiny pop-up box which appears when you place your pointer or cursor
on the control

• The purpose of this control is it provides a brief description about the control present
in the windows form.

• In ToolTip, you can check the ToolTip is active or not by using Active Property.
Add tooltip at design time

22
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

• Add the ToolTip control on to the form

• Then drag and drop a label control and we will find the tooltip property
• Write something and then run the application

Add tool tip dynamically (Run Time)


• Add a label control to the form. Then in the form load event write the below code
private void Form1_Load(object sender, EventArgs e)
{

System.Windows.Forms.ToolTip tooltip = new System.Windows.Forms.ToolTip();


tooltip.IsBalloon = true;
tooltip.ShowAlways = true;
tooltip.SetToolTip(label2, "This is a dynamic tooltip");
}

This will display a balloon tooptip on the label2 whose content is set dynamically.

23
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

11. What is the Backgroundworker thread?


• BackgroundWorker component is the first in the list
• We often use background threads when a time-consuming process needed to be
executed in the background without affecting the responsiveness of the user
interface. This is where a BackgroundWorker component comes into play.
• A BackgroundWorker component executes code in a separate dedicated secondary
thread.
• BackgroundWorker has two methods, RunWorkerAsync and CancelAsync. The
RunWorkerAsync starts the thread and the CancelAsync stops the thread.
Creating a BackgroundWorker

We can create a BackgroundWorker at design-time by dragging onto a Form or at run-time


using the BackgroundWorker class.
The following code is added when you drag a BackgroundWorker component onto a Form.

private System.ComponentModel.BackgroundWorker backgroundWorker1;


this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
BackgroundWorker Properties
Here is a list of BackgroundWorker class properties.
 CancellationPending - Indicates if an application has requested cancellation of a
BackgroundWorker.
 IsBusy - Indicates if a BackgroundWorker is running an asynchronous operation.
 WorkerReportsProgress - Indicates of a BackgroundWorker can report progress
updates.
 WorkerSupportsCancellation - Indicates if a BackgroundWorker supports
asynchronous cancellation.

24
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
10 marks

1. Explain in detail about tool box controls

The Toolbox window displays controls that you can add to Visual Studio projects. To
open Toolbox, choose View > Toolbox from the menu bar, or press Ctrl+Alt+X.

You can drag and drop different controls onto the surface of the designer you are using, and resize and
position the controls. Toolbox appears in conjunction with designer views, such as the designer view of
a XAML file or a Windows Forms App project. Toolbox displays only those controls that can be used
in the current designer. You can search within Toolbox to further filter the items that appear.

The .NET version that your project targets also affects the set of controls visible in Toolbox. You can
change the target framework version from the project's property pages, if necessary. Select the project
node in Solution Explorer, and then on the menu bar, choose Project > projectname Properties. On
the Application tab, use the Target framework drop-down.

25
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

Manage the toolbox windows and controls :

By default, Toolbox is collapsed along the left side of the Visual Studio IDE and appears when the
cursor is moved over it. You can pin Toolbox (by clicking the Pin icon on its toolbar) so that it remains
open when you move the cursor. You can also undock the Toolbox window and drag it anywhere on
your screen. You can dock, undock, and hide Toolbox by right-clicking its toolbar and selecting one of
the options.

If the Toolbox no longer appears as collapsed along the left side of the Visual Studio IDE, you can
add it back by choosing Window > Reset Window Layout from the menu bar.
You can rearrange the items in a Toolbox tab or add custom tabs and items by using the following
commands on the right-click context menu:
 Rename Item - Renames the selected item.
 List View - Shows the controls in a vertical list. If unchecked, the controls appear horizontally.
 Show All - Shows all possible controls (not just the ones that apply to the current designer).
 Choose Items - Opens the Choose Toolbox Items dialog box so that you can specify the items that
appear in the Toolbox. You can show or hide an item by selecting or clearing its check box.
 Sort Items Alphabetically - Sorts the items by name.
 Reset Toolbar - Restores the default Toolbox settings and items.
 Add Tab - Adds a new Toolbox tab.
 Move Up - Moves the selected item up.
 Move Down - Moves the selected item down.

Create and Distribute custom toolbox controls :

You can create custom Toolbox controls, starting either with a project template that's based
on Windows Presentation Foundation or on Windows Forms. You can then distribute your custom control
to your teammates, or publish it on the web by using the Toolbox Controls Installer

Components Tab :

BackgroundWorker - Creates a BackgroundWorker component instance that can run an operation on


a separate, dedicated thread. For more information, see BackgroundWorker component.

26
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
DirectoryEntry - Creates a DirectoryEntry component instance that encapsulates a node or object in
the Active Directory hierarchy and can be used to interact with Active Directory service providers.

DirectorySearcher - Creates a DirectorySearcher component instance that you can use to perform
queries against the Active Directory.

ErrorProvider - Creates a ErrorProvider component instance, which indicates to the end user that a
control on a form has an error associated with it. For more information, see ErrorProvider component.

EventLog - Creates an EventLog component instance you can use to interact with system and custom
event logs, including writing events to a log and reading log data.

FileSystemWatcher - Creates a FileSystemWatcher component instance that you can use to monitor
for changes to any directory or file to which you have access.

HelpProvider - Creates a HelpProvider component instance that provides pop-up or online help for
controls.

ImageList - Creates a ImageList component instance that provides methods to manage a collection
of Image objects.

MessageQueue - Creates a MessageQueue component instance that you can use to interact with
message queues, including reading messages from and writing messages to queues, processing
transactions, and performing queue administration tasks.

Data tab :
DataGridView Provides a powerful and flexible way to display data in a tabular
format.
Displays data objects you can add to a forms and components. The Data tab of the Toolbox appears
when you create a project that has an associated designer. The Toolbox appears by default in the Visual
Studio integrated development environment; if you need to display the Toolbox, select Toolbox from
the View menu.

-> UI Element List :

Name Description
DataSet Adds an instance of a typed or untyped dataset to the form or
component. When you drag this object onto a designer, it displays
a dialog box that allows you to select an existing typed dataset
class or specify that you want to create a new, blank, untyped
dataset. Note: You do not use the DataSet object on
the Toolbox to create a new typed dataset schema and class. For
more information, see Create and configure datasets.

BindingSource Simplifies the process of binding controls to an underlying data


source.
27
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
BindingNavigator Represents the navigation and manipulation user interface (UI) for
controls on a form that are bound to data.

2. Explain in detail about Container Controls (May-22)

A ContainerControl represents a control that can function as a container for other controls and
provides focus management. Controls that inherit from this class can track the active control they contain,
even when the focus moves somewhere within a different container.

ContainerControl objects provide a logical boundary for contained controls. The container control
can capture the TAB key press and move focus to the next control in the collection.

The container control does not receive focus; the focus is always set to the first child control in the
collection of contained controls. You do not typically inherit directly from
the ContainerControl class. Form, UserControl, and UpDownBase classes inherit from ContainerControl.

Provides focus-management functionality for controls that can function as a container for other
controls.
public class ContainerControl : System.Windows.Forms.ScrollableControl,
System.Windows.Forms.IContainerControl

ContainerClass Constructor :

Initializes a new instance of the ContainerControl class.

public ContainerControl ();

For example , The following code example inherits from the ScrollableControl class and
implements the IContainerControl interface. Implementation is added to the ActiveControl property and
the ActivateControl method.

using System;
using System.Windows.Forms;
using System.Drawing;

public class MyContainer : ScrollableControl, IContainerControl


{
private Control activeControl;
public MyContainer()
{
// Make the container control Blue so it can be distinguished on the form.
this.BackColor = Color.Blue;

// Make the container scrollable.


this.AutoScroll = true;
}

// Add implementation to the IContainerControl.ActiveControl property.


public Control ActiveControl
28
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
{
get
{
return activeControl;
}

set
{
// Make sure the control is a member of the ControlCollection.
if(this.Controls.Contains(value))
{
activeControl = value;
}
}
}

// Add implementations to the IContainerControl.ActivateControl(Control) method.


public bool ActivateControl(Control active)
{
if(this.Controls.Contains(active))
{
// Select the control and scroll the control into view if needed.
active.Select();

this.ScrollControlIntoView(active);
this.activeControl = active;
return true;
}
return false;
}
}

Properties :

1. ActiveControl - Gets or sets the active control on the container control.

[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.Control? ActiveControl { get; set; }

Property value - control

2. AutoScaleDimensions - Gets or sets the dimensions that the control was designed to.

[System.ComponentModel.Browsable(false)]
public System.Drawing.SizeF AutoScaleDimensions { get; set; }

Property value - SizeF (A SizeF containing the dots per inch (DPI) or Font size that the control was
designed to.)

Exceptions - ArgumentOutOfRangeException

The AutoScaleDimensions property represents the DPI or font setting of the screen that the control was

29
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
scaled to or designed for. Specifically, at design time this property will be set by the Windows Forms
designer to the value your monitor is currently using. Then, when the form loads at run time, if
the CurrentAutoScaleDimensions property is different from the AutoScaleDimensions,
the PerformAutoScale method will be called to perform scaling of the control and all of its children.
Afterwards, AutoScaleDimensions will be updated to reflect the new scaling size.

3. AutoValidate - Gets or sets a value that indicates whether controls in this container will be
automatically validated when
the focus changes.

[System.ComponentModel.Browsable(false)]
public virtual System.Windows.Forms.AutoValidate AutoValidate { get; set; }

Property value - AutoValidate ( An AutoValidate enumerated value that indicates whether contained
controls are implicitly validated on focus change. The default is Inherit.)

Exception - InvalidEnumArgumentException

4. AutoScaleMode - Gets or sets the automatic scaling mode of the control.

[System.ComponentModel.Browsable(false)]
public System.Windows.Forms.AutoScaleMode AutoScaleMode { get; set; }

Property value - AutoScaleMode ( An AutoScaleMode that represents the current scaling mode. The
default is None.)

Exception - InvalidEnumArgumentException

An AutoScaleMode value that is not valid was used to set this property.

The AutoScaleMode property specifies the current automatic scaling mode of this control. Scaling
by Font is useful if you want to have a control or form stretch or shrink according to the size of the fonts
in the operating system, and should be used when the absolute size of the control or form does not matter.
Scaling by Dpi is useful when you want to size a control or form relative to the screen. For example, you
may want to use dots per inch (DPI) scaling on a control displaying a chart or other graphic so that it
always occupies a certain percentage of the screen.

Methods :

I) OnCreateControl() - Raises the CreateControl() method.

protected override void OnCreateControl ();

The OnCreateControl method is called when the control is first created.


The OnCreateControl method also allows derived classes to handle the event without attaching a delegate.
This is the preferred technique for handling the event in a derived class.
When overriding OnCreateControl() in a derived class, be sure to call the base
class's OnCreateControl() method so that registered delegates receive the event.

II) OnFontChanged() - Raises the FontChanged event.

protected override void OnFontChanged (EventArgs e);

30
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
An EventArgs that contains the event data.

Changing the font used by a ContainerControl will cause the layout to be recalculated and reapplied to the
contained controls if the AutoScaleMode property has a value of Font. Raising an event invokes the event
handler through a delegate.The OnFontChanged method also allows derived classes to handle the event
without attaching a delegate. This is the preferred technique for handling the event in a derived class.

When overriding OnFontChanged(EventArgs) in a derived class, be sure to call the base


class' OnFontChanged(EventArgs) method so that registered delegates receive the event.

III ) OnAutoValidateChanged() - Raises the AutoValidateChanged event.

protected virtual void OnAutoValidateChanged (EventArgs e);

An EventArgs that contains the event data.

The OnAutoValidateChanged method also allows derived classes to handle the event without attaching a
delegate. This is the preferred technique for handling the event in a derived class.
When overriding OnAutoValidateChanged(EventArgs) in a derived class, be sure to call the base
class' OnAutoValidateChanged(EventArgs) method so that registered delegates receive the event.

3. Write a simple calculator program.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CalculatorApp {
public partial class Form1 : Form {
double FirstNumber;
string Operation;
public Form1() {
InitializeComponent();
}

private void n1_Click(object sender, EventArgs e) {


if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "1";
} else {
textBox1.Text = textBox1.Text + "1";
}}
private void n2_Click(object sender, EventArgs e) {
if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "2";
} else {
textBox1.Text = textBox1.Text + "2";
}}

31
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
private void n3_Click(object sender, EventArgs e) {
if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "3";
} else {
textBox1.Text = textBox1.Text + "3";
}}
private void n4_Click(object sender, EventArgs e) {
if (textBox1.Text == "0" && textBox1.Text != null) {
textBox1.Text = "4";
} else {
textBox1.Text = textBox1.Text + "4";
}}
private void n9_Click(object sender, EventArgs e) {
if (textBox1.Text == "" && textBox1.Text != null) {
textBox1.Text = "9";
} else {
textBox1.Text = textBox1.Text + "9";
}}
private void n0_Click(object sender, EventArgs e) {
textBox1.Text = textBox1.Text + "0";
}
private void bad_Click(object sender, EventArgs e) {
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0"; Operation = "+";
}
private void bsub_Click(object sender, EventArgs e) {
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0"; Operation = "-";
}
private void bmul_Click(object sender, EventArgs e) {
FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0"; Operation = "*";
}

private void bdiv_Click(object sender, EventArgs e) {


FirstNumber = Convert.ToDouble(textBox1.Text);
textBox1.Text = "0"; Operation = "/";
}
private void bc_Click(object sender, EventArgs e) {
textBox1.Text = "0";
}
private void ndot_Click(object sender, EventArgs e) {
textBox1.Text = textBox1.Text + ".";
}
private void nequal_Click(object sender, EventArgs e) {
double SecondNumber; double Result;
SecondNumber = Convert.ToDouble(textBox1.Text);
if (Operation == "+") {
Result = (FirstNumber + SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}
32
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE
if (Operation == "-") {
Result = (FirstNumber - SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result; }
if (Operation == "*") {
Result = (FirstNumber * SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result; }
if (Operation == "/") {
if (SecondNumber == 0) {
textBox1.Text = "Cannot divide by zero";
} else {
Result = (FirstNumber / SecondNumber);
textBox1.Text = Convert.ToString(Result);
FirstNumber = Result;
}}}}

4. Explain about Graphic Programming.

GDI+ is a library that provides an interface that allows programmers to write Windows and Web
graphics applications that interact with graphical devices such as printers, monitors, or files.

All graphical user interface (GUI) applications interact with a hardware device (a monitor, printer, or
scanner), that can represent the data in a human-readable form. However, there is no direct
communication between a program and a device; otherwise, you would have to write user.

Microsoft’s managed GDI+ documentation divides its functionality into three categories: 2D vector
graphics, imaging, and typography:
1. 2D vector graphics
2. Imaging
3. Typography
4. Printing
5. Design

GDI+ Namespaces and Classes in .NET

In the .NET Framework library, six namespaces define managed GDI+: System.Drawing,
System.Drawing.Design, System.Drawing.Drawing2D, System.Drawing.Imag ing,
System.Drawing.Printing, and System.Drawing.Text.
Figure 1.3 shows these namespaces. To use any of the classes defined in these namespaces, you must
include them in your application.

33
U20CSCM02 – C# and.Net Programming
SRI MANAKULA VINAYAGAR ENGINEERING COLLEGE Dept of CSE

Creating graphics object


Graphics Objects are created by calling the CreateGraphics() method of the component on

Graphics g=this.CreateGraphics();

Creating Pen
Used to draw shapes

Pen p=new Pen(color,width);


Pen p=new Pen(Color.Red,4);

Creating Brush
Used to display text

Brush b=new Brush(Color, width);

Program for Graphics

using System;
using System.Windows.Forms;
using System.Drawing;

public class Hello:Form {


public Hello() {
this.Paint += new PaintEventHandler(f1_paint); }
private void f1_paint(object sender,PaintEventArgs e) {
// Get Graphics Object
Graphics g = e.Graphics;

// Method under System.Drawing.Graphics


g.DrawString("Welcome C#",new Font("Verdana",20),
new SolidBrush(Color.Tomato),40,40);
}

public static void Main() {


Application.Run(new Hello());
}}

34
U20CSCM02 – C# and.Net Programming

You might also like