KEMBAR78
Unit 1,2,3 | PDF | Variable (Computer Science) | Method (Computer Programming)
0% found this document useful (0 votes)
31 views61 pages

Unit 1,2,3

The document provides an overview of various user interface controls in Visual Studio, including TextBox, Label, Button, MenuStrip, RichTextBox, FontDialog, and ColorDialog. Each control is explained with its properties, events, and examples of usage in C#. The document serves as a guide for developers to implement these controls in Windows Forms applications.

Uploaded by

atharvatare23820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views61 pages

Unit 1,2,3

The document provides an overview of various user interface controls in Visual Studio, including TextBox, Label, Button, MenuStrip, RichTextBox, FontDialog, and ColorDialog. Each control is explained with its properties, events, and examples of usage in C#. The document serves as a guide for developers to implement these controls in Windows Forms applications.

Uploaded by

atharvatare23820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

Controls

1. Explain TextBox with an example.

In Visual Studio, a TextBox is a common user interface control that allows


users to input and edit text. It's part of the Windows Forms library, which is
a graphical user interface (GUI) framework for building Windows desktop
applications. The TextBox control is often used in forms to collect user
input, such as names, addresses, or other textual data.

Steps to use a textbox :


1) Open Visual Studio and create a new Windows Forms Application
project.
2) In the Form designer, drag and drop a TextBox from the Toolbox onto
your form. You can find the TextBox control in the "Common Controls"
section of the Toolbox.

Properties Description

AutoComple It is used to get or set a value that indicates how the

teMode automatic completion works for the textbox control.

Font It is used to set the font style of the text displayed on a

Windows form.
Lines It is used to set the number of lines in a TextBox control.

Multiline It is used to enter more than one line in a TextBox control,

by changing the Multiline property value from False to True.

ScrollBars It is used to display a scrollbar on a multiline textbox by

setting a value for a Textbox control.

Text It is used to get or set the text associated with the textbox

control.

Visible The Visible property sets a value that indicates whether the

textbox should be displayed on a Windows Form.

WordWrap The WordWrap properties validate whether the multiline

Textbox control automatically wraps words to the

beginning of the next line when necessary.

Events Description
Click When a textbox is clicked, a click event is called in the

textbox control.

FontChanged It occurs when the property of the Font is changed.

Example :
using System;

using System.Windows.Forms;

namespace TextBoxExample

public partial class MainForm : Form

public MainForm()

InitializeComponent();

}
private void button1_Click(object sender, EventArgs e)

// Get the text from the TextBox

string userInput = textBox1.Text;

// Display a message box with the entered text

MessageBox.Show("You entered: " + userInput, "TextBox


Example");

// You can also perform other operations with the entered text as
needed

// For example, you might want to save it to a file, process it, etc.

2. Explain Label with an example.


● In Windows Forms, Label control is used to display text on the form
and it does not take part in user input or in mouse or keyboard
events.
● The Label is a class and it is defined under System.Windows.Forms
namespace.
● Create a Label control using the following steps:
○ Step 1: Create a windows form as shown in the below image:
Visual Studio -> File -> New -> Project ->
WindowsFormApp
○ Step 2: Drag the Label control from the ToolBox and drop it on
the windows form. You are allowed to place a Label control
anywhere on the windows form according to your need.
○ Step 3: After drag and drop you will go to the properties of the
Label control to set the properties of the Label according to
your need.

Property Description

This property is used to set a value indicating whether


AutoSize the Label control is automatically resized to display its
entire contents.

This property is used to set the background color for


BackColor
the Label control.

BackgroundIma This property is used to set the background image for


ge the Label control.

This property is used to set the border style for the


BorderStyle
Label control.
This property is used to set the flat style appearance
FlatStyle
of the label control.

This property is used to set the font of the text


Font
displayed by the Label control.

This property is used to set the height of the font of


FontHeight
the Label control.

This property is used to set the foreground color of


ForeColor
the Label control.

This property is used to set the height of the Label


Height
control.

This property is used to set the image that is


Image
displayed on a Label.

This property is used to set the coordinates of the


Location upper-left corner of the Label control relative to the
upper-left corner of its form.

This property is used to set the name of the Label


Name
control.
This property is used to set padding within the Label
Padding
control.

This property is used to set the height and width of


Size
the Label control.

This property is used to set the text associated with


Text
this Label control.

This property is used to set the alignment of text in


TextAlign
the label.

This property is used to set a value indicating whether


Visible
the control and all its child controls are displayed.

This property is used to set the width of the Label


control.
Width

Events
Init Occurs when the server control is initialized, which is the first step
in its lifecycle.

(Inherited from Control)

Load Occurs when the server control is loaded into the Page object.

(Inherited from Control)

Unload Occurs when the server control is unloaded from memory.

(Inherited from Control)

Label la=new Label();

la.text=”This is the new label”;

la.Location(Point object); //this line is unnecessary here because we can’t


see it so let’s not overcomplicate by putting a Point object.

3. Explain Button with an example.

Ans.
● Button is a commonly used control in C# for creating interactive user
interfaces.
● It is present in toolbox window under ‘components’ tab , you need to
drag and drop onto the design surface of your application and
customize it’s properties.
● Buttons have various properties that can be modified to customize
their appearance and behavior. Some common properties include
Text (to set the button's display text), BackColor (to set the
background color), ForeColor (to set the text color), and Font (to set
the font style).
● They are commonly used in applications to perform tasks such as
submitting a form, saving data, or initiating a process.
● To add functionality to the Button control, you can double-click on it in
the design surface to perform specific actions when the button is
clicked, such as executing a method or displaying a message
● To handle the Click event of a button, you can either double-click on
the button in the visual designer, which will generate an event handler
method in the code-behind file.
● Example:
using System;
using System.Windows.Forms;
namespace MyApplication{
public partial class MainForm : Form{
public MainForm() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
// Code to be executed when the button is clicked
MessageBox.Show("Button clicked!");
}
}
}
In this example, we have a Windows Form named MainForm with a Button
control named button1. When the button is clicked, a message box will
be displayed with the text "Button clicked!".
Properties:

Anchor Gets or sets the edges of the container to


which a control is bound and determines
how a control is resized with its parent.

Cursor Gets or sets the cursor that is displayed


when the mouse pointer is over the
control.

Enabled Gets or sets a value indicating whether


the control can respond to user
interaction.

Image Gets or sets the image that is displayed


on a button control.

Visible Gets or sets a value indicating whether


the control and all its child controls are
displayed.

Events:

Click Occurs when the control is clicked.

EnabledChanged Occurs when the Enabled property


value has changed.
4. Explain menustrip with an example.
● Creating a MenuStrip
○ We can create a MenuStrip control using a Forms designer at
design-time or using the MenuStrip class in code at run-time or
dynamically.
○ To create a MenuStrip control at design-time, you simply drag
and drop a MenuStrip control from Toolbox to a Form in Visual
Studio. After you drag and drop a MenuStrip on a Form, the
MenuStrip1 is added to the Form and looks like Figure 1. Once
a MenuStrip is on the Form, you can add menu items and set
its properties and events.
● Positioning a MenuStrip
○ The Dock property is used to set the position of a MenuStrip. It
is of type DockStyle that can have values Top, Bottom, Left,
Right, and Fill.
● MenuStrip Items
○ The Items property is used to add and work with items in a
MenuStrip. We can add items to a MenuStrip at design-time
from Properties Window by clicking on Items Collection
● Adding Menu Item Click Event Handler
○ The main purpose of a menu item is to add a click event
handler and write code that we need to execute on the menu
item click event handler. For example, on File >> New menu
item click event handler, we may want to create a new file.
○ We can also define and implement an event handler
dynamically. The following code snippet defines and
implements these events and their respective event handlers.

Eg: click event

FileMenu.Click += new System.EventHandler(this.FileMenuItemClick);

private void FileMenuItemClick(object sender, EventArgs e) {

MessageBox.Show("File menu item clicked");


}

Property Description

Anchor Gets or sets the edges of the


container to which a ToolStrip is
bound and determines how a
ToolStrip is resized with its parent.

CanOverflow Gets or sets a value indicating


whether the MenuStrip supports
overflow functionality.

Enabled Gets or sets a value indicating


whether the control can respond to
user interaction.

Items Gets all the items that belong to a


ToolStrip.

ShowItemToolTips Gets or sets a value indicating


whether ToolTips are shown for the
MenuStrip.

Events:

ItemClicked Occurs when the ToolStripItem is


clicked.
EnabledChanged Occurs when the Enabled property
value has changed.
5. Explain RichTextBox with an example
● In Visual Studio, the RichTextBox is another text editing control that
provides more advanced formatting options compared to the standard
TextBox.
● It is part of the Windows Forms library, commonly used for displaying
and editing formatted text.
● The RichTextBox control allows you to work with different fonts,
colors, styles, and even embedded images.

Here's a simple example of using a RichTextBox in Visual Studio with C#:

1. Open Visual Studio and create a new Windows Forms Application


project.
2. In the Form designer, drag and drop a RichTextBox from the Toolbox
onto your form. You can find the RichTextBox control in the
"Common Controls" section of the Toolbox.

Properties:

Property Description

Gets or sets the entire text content of the


Text
RichTextBox.

Gets or sets the font applied to the current text


SelectionFont
selection or insertion point.
Gets or sets the color of the text in the current text
SelectionColor
selection or insertion point.

SelectionAlignm Gets or sets the horizontal alignment of the current

ent text selection or insertion point.

Gets or sets the zoom factor of the RichTextBox


ZoomFactor
content.

Gets or sets a value indicating whether the

DetectUrls RichTextBox should automatically detect and

format URLs.

BorderStyle Gets or sets the border style of the RichTextBox.

Gets or sets a value indicating whether the text in the

RichTextBox control is read-only.


ReadOnly

Events:
TextChanged: This event is triggered whenever the text inside the
RichTextBox changes. It could be due to user input or programmatic
changes.

SelectionChanged: This event occurs when the selection (highlighted text)


within the RichTextBox changes.

Example :

Design:

Names are: richTextBox1 for the rich text box and button1 for the show
button.

Code:

using System;

using System.Collections.Generic;

using System.ComponentModel;
using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace FormPractical

public partial class Form1 : Form

public Form1()

InitializeComponent();

private void button1_Click(object sender, EventArgs e)


{

string a=richTextBox1.Text;

MessageBox.Show(a,"Rich Text Box


Content",MessageBoxButtons.OK,MessageBoxIcon.Informatio
n);

Output:
6. Explain FontDialogBox with an example.
● FontDialog represents a common dialog box that displays a list of
fonts that are currently installed on the system.
● The FontDialog box allows the user to choose attributes like logical
font, font style, point size, effects and Scripts.
● FontDialog presents a Font selection dialog box that allows the user
to select a font from the list of installed fonts.

Select New Project--->Visual C#-->Windows Forms App (.NET


Framework), give your project a name and click OK.

This action creates a WinForms project with a default form and you should
see the Windows designer.

STEP 2: Drag and Drop Control

add a FontDialog control to the form by dragging it from Toolbox and


dropping it to the form. You will see a FontDialog 1 is added to the form.
This control is now available to you in the code behind.

Show FontDialog

Now, let's add another control to the form by dragging another control from
Toolbox to the form. You may also want to change the properties of the
other controls.

We will launch Font Dialog on click on the button, select a font, and the
selected font will be the font of the Label control.
STEP 3: Coding for Button Click Event

You can add a button click event handler by simply double clicking on the
button control. On this button control event handler, we can change the font
of the text displayed in the label.

public partial class Form1 : Form{

public Form1(){

InitializeComponent();

private void button1_Click(object sender, EventArgs e){

DialogResult fontResult = fontDialog1.ShowDialog();

if (fontResult == DialogResult.OK){

label1.Font = fontDialog1.Font;

STEP 4: Compile and Run

Now simply compile and run the application.


Once you click on the Change the Font button, the font dialog will open
where you can select a font. Select a font and click Ok, the selected font
will be updated as the font of the text in the label.

Properties:

Color Gets or sets the selected font color.

Font Gets or sets the selected font.

ShowApply Gets or sets a value indicating whether the dialog box


contains an Apply button.

ShowColor Gets or sets a value indicating whether the dialog box


displays the color choice.

ShowHelp Gets or sets a value indicating whether the dialog box


displays a Help button.

Events:

Apply Occurs when the user clicks the Apply button in the font
dialog box.

HelpRequest Occurs when the user clicks the Help button on a


common dialog box.
7. Explain ColorDialogBox with an example.
● The ColorDialog control class represents a common dialog box
that displays available colors
● The Color Dialog box is used to display the Color dialog box
and the selection of colors on the Microsoft Windows
Application.
● It allows users to set or change the color of an object, such as
the background color of a control or the color used to paint an
object.
● The main property of the ColorDialog control is Color, which
returns a Color object.
● To create a color dialog box

Step 1: Drag the Color Dialog from the toolbox and drop it to
the Windows form

Step 2: Once the Color Dialog is added to the form, we can set
various properties of the Color by clicking on the Color Dialog.

Properties:

AnyColor Gets or sets a value indicating whether the dialog box


displays all available colors in the set of basic colors.

Color Gets or sets the color selected by the user.

SolidColorOnly Gets or sets a value indicating whether the dialog box


will restrict users to selecting solid colors only.

CustomColors Gets or sets the set of custom colors shown in the


dialog box.
DesignMode Gets a value that indicates whether the Component is
currently in design mode.

Events:

Disposed Occurs when the component is disposed of by a call to


the Dispose() method.

HelpRequest Occurs when the user clicks the Help button on a


common dialog box.

Design:
using System;

namespace WindowsFormsApplication1

public partial class Form1 : Form

public Form1()
{

InitializeComponent();

private void button1_Click(object sender,


EventArgs e)

ColorDialog dlg = new ColorDialog();

dlg.ShowDialog();

if (dlg.ShowDialog() == DialogResult.OK)

string str = dlg.Color.Name;

MessageBox.Show (str);

8. Explain the use of reference variable in C# with an example.

Ans.

I] In C#, a reference variable is a variable that holds the memory address of


an object instead of the actual value.
II] It allows multiple variables to refer to the same object, and any changes
made to the object through one variable will be reflected in all other
variables that reference the same object.

III] This means that when you assign a reference type variable to another
variable, both variables will point to the same object in memory.

Here are some key points to understand about reference variables in C#:

● Reference Types: Reference variables are used to hold instances of


reference types, such as classes, interfaces, delegates, and arrays.
These types are allocated on the heap and their memory is managed
by the .NET garbage collector.

● Memory Address: When an object is created, it is stored in memory,


and the reference variable holds the memory address (also known as
a reference) of that object. This allows different variables to refer to
the same object, providing a way to share and manipulate data.

● Assignment: When you assign a reference type variable to another


variable, you are actually copying the memory address, not the object
itself. This means both variables will refer to the same object in
memory.

● Passing by Reference: In C#, you can also pass reference variables to


methods by reference using the ref keyword. This allows the method
to modify the original object, rather than creating a copy of it.
Changes made to the object within the method will be reflected in
the calling code.

● Null References: A reference variable can be assigned a special value


called null, which means it does not refer to any object. Attempting to
access members or methods of a null reference will result in a
NullReferenceException at runtime.

● Dereferencing: To access members or methods of an object through a


reference variable, you can use the dot (.) operator. This allows you to
manipulate the object's state or invoke its behavior.

class Program

static void Main(string[] args)

// Declare and initialize a variable

int number = 10;

// Create a reference variable that refers to the same memory


location as 'number'

ref int refNumber = ref number;

// Modify the value of the variable through the reference variable

refNumber = 20;

// Access the modified value through the original variable

Console.WriteLine(number); // Output: 20

In the above example, we first declare and initialize a variable number with
the value 10. Then, we create a reference variable refNumber using the ref
keyword, which refers to the same memory location as the number
variable.

By modifying the value of refNumber, we are actually modifying the value


of number because they both refer to the same memory location. In this
case, we change the value of refNumber to 20.

When we print the value of a number using the Console.WriteLine


statement, it outputs the modified value 20.
9. Explain message box control with an example.
● A message box is a prefabricated modal dialog box that displays a
text message to a user.
● You show a message box by calling the static Show method of the
MessageBox class.
● The text message that is displayed is the string argument that you
pass to Show.
● There are several overloads of the Show method.
● You can also use a message box to ask a user a question.
● You can determine which button a user clicks by examining the value
that Show returns.
● Message boxes can communicate information and can ask questions
that have varying degrees of importance.
● Message boxes use icons to indicate importance.

Properties:

Show(String) Displays a message box that has a message and


that returns a result.

Show(String, String) Displays a message box that has a message and


title bar caption; and that returns a result.

Show(String, String, Displays a message box that has a message, title


MessageBoxButton) bar caption, and button; and that returns a result.

Show(String, String, Displays a message box that has a message, title


MessageBoxButton, bar caption, button, and icon; and that returns a
MessageBoxImage) result.
Show(String, String, Displays a message box that has a message, title
MessageBoxButton, bar caption, button, and icon; and that accepts a
MessageBoxImage, default message box result and returns a result.
MessageBoxResult)
10. What is a User Control? Explain and write the steps to create a
User Control.

Answer:

● User control is a file which is a combination of HTML controls or Web


server controls.
● It is created for the purpose of the reusability of the code written to
design the control.
● Some of the important features of user controls are given as follows:
○ User control files have .ascx extension.
○ The user control code can be written in the code behind file or
in the source page of user control
○ User controls have @ Control directive.
○ User controls inherit properties from the UserControl class.
○ User controls are specific to the application in which they are
made.
● Create a User Control
○ Open Visual Studio: Launch Visual Studio, and create a new
Class Library project. This project will serve as the User Control
Library.
○ Add User Control: Right-click on the project in the Solution
Explorer, select "Add" > "User Control," and give it a name.
This will create a new user control file with a .cs extension.
○ Design the User Control: Open the user control file in the
designer. Here, you can add and configure various Windows
Forms controls, set their properties, and design the layout of
the user control.
○ Customize Properties and Events: Define any custom
properties or events for the user control, as needed, to make it
more flexible and extensible.
○ Build the User Control Library: Build the project to compile
the user control into a DLL file, which can be referenced in
other projects.
11. What is a User Control Library? Explain with examples.

Answer:

● A User Control Library is a collection of reusable user interface


elements that can be used across different applications.
● It allows developers to create custom controls and components that
can be easily shared and reused in various projects.
● User Control Libraries are particularly useful in graphical user
interface (GUI) development, as they enable the creation of
consistent and modular UI elements.
● A User Control Library in the context of Windows Forms refers to a
collection of Windows Forms controls encapsulated in a common
container, known as a composite control.
● These controls are designed like forms, with a visual designer, and
the contained controls are referred to as constituent controls.
● User controls are created by deriving from the UserControl class and
are used to promote reusability and consistency within or across
applications.
● An example of a User Control in Windows Forms is the creation of a
custom login control.
○ This involves creating a user control with textboxes for
username and password input, along with a login button.
○ This custom control can then be easily reused in different parts
of the application or in other applications within an organization,
promoting consistency in the user interface design and saving
time in laying out and coding the contained elements of the
user control.
○ By creating a separate DLL or control forms library for the user
control, it becomes easier to reuse and maintain the control,
ensuring better maintainability and reusability across different
projects or applications.
12. Write the steps to create and use the User Control Library in
detail.

Answer:

● Creating and using a User Control Library involves several steps,


from designing the user control to integrating it into your application.
Here are the detailed steps to create and use a User Control Library
in Windows Forms:
● Use the User Control Library in a Windows Forms Application
○ Create a Windows Forms Application: Create a new
Windows Forms Application project in Visual Studio, where you
want to use the User Control Library.
○ Add Reference to the User Control Library: Right-click on the
References node in the Solution Explorer of the Windows
Forms Application project, select "Add Reference," and browse
to the location of the User Control Library DLL. Add a reference
to the User Control Library.
○ Use the User Control: Open the form where you want to use
the user control. In the designer, the user control from the
library should appear in the Toolbox. Simply drag and drop the
user control onto the form.
○ Customize and Interact with the User Control: Customize
the properties of the user control as needed, and interact with it
through code by handling its events and accessing its
properties and methods.
○ Build and Run the Application: Build the Windows Forms
Application and run it to see the user control in action within the
application.
13. How to create User Control in Windows form

Answer:

● In Visual Studio, find the Project Explorer window. Right-click on the


project and choose Add > User Control (Windows Forms).
● Set the Name of the control, and press Add. (If not changed it will be
UserControl1.cs by default.)
● With the designer open, the user control design surface should be the
selected object. If it's not, click on the design surface to select it.
● Add the desired properties and controls to your design in the
UserControl1.cs [Design] file.
● Add the functionality of your code in the UserControl1.cs file.
● Now to use the control in a form:
○ Open your toolbox, you will see the ‘FormName Components’
tab where your UserControl1 will be accessible.
○ If the tab isn’t visible, go to ‘Build’ in the menustrip and click on
‘Rebuild Solution’ Check the toolbox again.

Design:

Form1.cs {please note that the rich text box and the button is pre-
existing in the form and has nothing to do with this example}
UserControl1.cs

Code: (UserControl1.cs)
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace FormPractical

public partial class UserControl1 : UserControl

public UserControl1()

InitializeComponent();

}
private void button1_Click(object sender, EventArgs e)

int t = Int32.Parse(textBox1.Text);

if (t % 2 == 0)

label1.Text = t + " is an even number.";

else {

label1.Text = t + " is not an even number.";

14. How to create User Control Library

Answer:

● Click on New in the menu strip and make a new project.


● Select the type of project as: Windows Form Control Library.
● Set the Name of the control file, and press Add. (If not changed it will
be UserControl1.cs by default.)
● With the designer open, the user control design surface should be the
selected object. If it's not, click on the design surface to select it.
● Add the desired properties and controls to your design in the
UserControl1.cs [Design] file.
● Add the functionality of your code in the UserControl1.cs file.
● Now to use the control in a form:
○ Open an existing Form or make a new Windows Form
Application.
○ Go to Toolbox and Right Click -> Click on ‘Add Tab’.
○ Name your tab as your choice, Right click on the Tab name and
choose item.
○ Click on Browse.
○ You will be directed to the projects on your Visual Studio under
the path:
■ Admin>source>repos>
■ Here in the repos you will see all of your files.
■ Click on the UserControl1>bin>Debug
■ In the Debug folder you will see a file with ‘.dll’ extension.
■ Select the file and click Open.
○ If you aren’t able to see the ‘.dll’ file, try rebuilding the solution
of the UserControl1 project.
○ Open your toolbox, you will see the ‘FormName Components’
tab where your UserControl1 will be accessible.
○ If the tab isn’t visible, go to ‘Build’ in the menustrip and click on
‘Rebuild Solution’ Check the toolbox again.

Design:

Form1.cs {please note that the rich text box and the button is pre-
existing in the form and has nothing to do with this example}
UserControl1.cs

Code: UserControl1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;
using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace UserControl2

public partial class UserControl2: UserControl

public UserControl2()

InitializeComponent();

private void button1_Click(object sender, EventArgs e)

int t=Int32.Parse(textBox1.Text);

if (t % 2 == 0) {

label1.Text = t + " is an even number";


}

else

label1.Text = t + " is not an even number";

}
15. Explain the Radio button with an example. -maya

Answer

● In Windows Forms, RadioButton control is used to select a single


option among the group of the options.
● In C#, RadioButton is a class and it is defined under
System.Windows.Forms namespace. In RadioButton, you are
allowed to display text, image, or both and when you select one radio
button in a group other radio buttons automatically clear
● You can create RadioButton in two different ways:
○ Design Time
■ Drag and drop
○ Runtime
■ Hardcoding it.
● A radio button is grouped generally by a group box. Multiple radio
buttons cannot be selected in a single group box.
● It is commonly used in validation of forms while selecting the gender
of an individual.
● Attributes: imp: Checked, AutoCheck

Property Description

This property is used to set a value determining the


Appearance
appearance of the RadioButton.

This property is used to set a value indicating whether


the Checked value and the appearance of the
AutoCheck
RadioButton control automatically change when the
RadioButton control is clicked.
This property is used to set a value that indicates
AutoSize whether the RadioButton control resizes based on its
contents.

This property is used to set the background color of


BackColor
the RadioButton control.

This property is used to set the location of the check


CheckAlign
box portion of the RadioButton.

This property is used to set a value indicating whether


Checked
the RadioButton control is checked.

This property is used to set the font of the text


Font
displayed by the RadioButton control.

This property is used to set the foreground color of the


ForeColor
RadioButton control.

This property is used to set the coordinates of the


Location upper-left corner of the RadioButton control relative
to the upper-left corner of its form.

This property is used to set the name of the


Name
RadioButton control.

This property is used to set padding within the


Padding
RadioButton control.

This property is used to set the text associated with


Text
this RadioButton control.
This property is used to set the alignment of the text
TextAlign
on the RadioButton control.

This property is used to set a value indicating whether


Visible the RadioButton control and all its child controls are
displayed.

● Events handled:

Event Description

This event occurs when the RadioButton


Click
control is clicked.

This event occurs when the value of the


CheckedChanged
Checked property changes.

AppearanceChange This event occurs when the Appearance


d property value changes.

This event occurs when the user double-clicks


DoubleClick
the RadioButton control.

This event occurs when the input focus leaves


Leave
the RadioButton control.
This event occurs when the RadioButton
MouseClick
control is clicked by the mouse.

This event occurs when the user double-clicks


MouseDoubleClick
the RadioButton control with the mouse.

This event occurs when the mouse pointer


MouseHover
rests on the RadioButton control.

This event occurs when the mouse pointer


MouseLeave
leaves the RadioButton control

● Example:

Design: Functionality is the same for the two.

a. Without a groupbox
b. With a groupbox

Code:
using System;

namespace FormPractical{

public partial class Form1 : Form{

public Form1(){

InitializeComponent();

private void button1_Click(object sender,


EventArgs e){

string a=string.Empty;

if (radioButton1.Checked)

a += radioButton1.Text;

}else if (radioButton2.Checked)

a += radioButton2.Text;

}else if(radioButton3.Checked)

a += radioButton3.Text;
}else if (radioButton4.Checked)

a += radioButton4.Text;

}else if(radioButton5.Checked)

a += radioButton5.Text;

else

a += "Not selected";

MessageBox.Show("Gender is " + a, "Gender


selection");

private void radioButton1_CheckedChanged(object


sender, EventArgs e)

}
16. Explain Checkbox with an example.
● The CheckBox control is the part of windows form which is used to
take input from the user.
● Or in other words, CheckBox control allows us to select single or
multiple elements from the given list or it can provide us options like
yes or no, true or false, etc.
● It can be displayed as an image or text or both.
● The CheckBox is a class and defined under System.Windows.Forms
namespace.
Important Properties of CheckBox

Property Description

This property is used to set a value which shows


whether the Checked or CheckState values and the
AutoCheck
CheckBox appearance are automatically changed when
you click the CheckBox.

This property is used to get or set the state of the


CheckState
CheckBox.

This property is used to get or set a value which


Checked determines whether the CheckBox is in the checked
state.
This property is used to get the list of event handlers
Events
that are attached to this Component.

This property is used to get or set the coordinates of the


Location upper-left corner of the CheckBox control relative to the
upper-left corner of its form.

This property is used to get or set the space between


Margin
controls.

This property is used to get or set the name of the


Name
control.

This property is used to get or set the text associated


Text
with this control.

This property is used to get or set a value which


Visible determines whether the control and all its child controls
are displayed.

Important Events on CheckBox

Event Description

This event occurs when the value of the Checked


CheckedChanged
property changes.
CheckStateChang This event occurs when the value of the CheckState
ed property changes.

Click This event occurs when the control is clicked.

This event occurs when the user double-clicks the


DoubleClick
CheckBox control.

This event occurs when the input focus leaves the


Leave
control.

This event occurs when the control is clicked by the


MouseClick
mouse.

This event occurs when the user double-clicks the


MouseDoubleClick
CheckBox control.

This event occurs when the mouse pointer rests on the


MouseHover
control.

//example baad mei daalti hu ya fir chahiye toh assignment group check kar
lo… maam ka example daala hai

using System;

namespace WindowsFormsApplication1
{

public partial class Form1 : Form

public Form1()

InitializeComponent();

private void button1_Click(object sender,


EventArgs e)

string msg = "";

if (checkBox1.Checked == true)

msg = "net-informations.com";

if (checkBox2.Checked == true)

msg = msg + " https://net-


informations.com/vb/default.htm";

}
if (checkBox3.Checked == true)

msg = msg + " csharp.net-


informations.com";

if (msg.Length > 0)

MessageBox.Show (msg + " selected ");

else

MessageBox.Show ("No checkbox selected");

checkBox1.ThreeState = true;

17. Explain Group box with an example. -avinashi


● In Windows form, GroupBox is a container which contains multiple
controls on it and the controls are related to each other.
● Or in other words, GroupBox is a frame display around a group of
controls with a suitable optional title.
● A GroupBox is used to categorize the related controls in a group.
● The GroupBox class is used to represent the windows group box and
also provide different types of properties, methods, and events.
● It is defined under System.Windows.Forms namespace.
● The main use of a group box is to hold a logical group of RadioButton
controls.
● Constructor

This Constructor is used to initialize a new instance of the


GroupBox()
GroupBox class.

● Properties

Property Description

This property is used to get or set a value that indicates


AutoSize
whether the control resizes based on its contents.

This property is used to get or set the coordinates of the


Location upper-left corner of the GroupBox control relative to the
upper-left corner of its form.

Name This property is used to get or set the name of the control.
This property is used to get or set a value that shows
TabStop whether the user can press the TAB key to provide the
focus to the GroupBox.

This property is used to get or set the height and width of


Size
the control.

This property is used to get or set a value indicating


Visible whether the control and all its child controls are
displayed.

using System;
namespace WindowsFormsApp45 {
public partial class Form1 : Form {
public Form1(){
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e){
// Creating and setting properties of the GroupBox
GroupBox box = new GroupBox();
box.Location = new Point(179, 145);
box.Size = new Size(329, 94);
box.Text = "Select Gender";
box.Name = "MyGroupbox";

// Adding group box in the form


this.Controls.Add(box);

// Creating and setting properties of the CheckBox


CheckBox b1 = new CheckBox();
b1.Location = new Point(40, 42);
b1.Size = new Size(49, 20);
b1.Text = "Male";
// Adding this control to the GroupBox
gbox.Controls.Add(b1);

// Creating and setting properties of the CheckBox


CheckBox b2 = new CheckBox();
b2.Location = new Point(183, 39);
b2.Size = new Size(69, 20);
b2.Text = "Female";

// Adding this control to the GroupBox


box.Controls.Add(b2);
}
}
}
Graphics:

1. Explain any 5 functions of graphics class -ananya


● The Graphics class provides methods for drawing objects to the
display device.
● A Graphics object is associated with a specific device context.
● You can obtain a Graphics object by calling the
Control.CreateGraphics method on an object that inherits from
System.Windows.Forms.Control, or by handling a control's
Control.Paint event and accessing the Graphics property of the
System.Windows.Forms.PaintEventArgs class.
● You can draw many different shapes and lines by using a Graphics
object.These methods include DrawLine, DrawArc,
DrawClosedCurve, DrawPolygon, and DrawRectangle.
● You can also draw images and icons by using the DrawImage and
DrawIcon methods, respectively.
● In addition, you can manipulate the coordinate system used by the
Graphics object.

DrawRectangle(Pen, Int32, Draws a rectangle specified by a


Int32, Int32, Int32) coordinate pair, a width, and a height.
DrawLine(Pen, Int32, Int32, Draws a line connecting the two points
Int32, Int32) specified by the coordinate pairs.
DrawLines(Pen, Point[]) Draws a series of line segments that
connect an array of Point structures.
DrawImage(Image, Rectangle) Draws the specified Image at the
specified location and with the specified
size.
DrawEllipse(Pen, Rectangle) Draws an ellipse specified by a bounding
Rectangle structure.
2. Explain linear gradient brush with example -avinashi
● Linear gradient brushes allow you to blend two colors together,
generating an indefinite range of shades.
● In a gradient, we begin with a starting color and shift to an ending
color, with gradual blending in the space between them.
● In addition to the starting and ending colors, we can specify the
direction of the gradient.
● You can also specify a range for pattern repetition.
● The LinearGradientBrush class has eight forms of overloaded
constructors.
● Each constructor takes a starting point, an ending point and two
gradient colors.
● The orientation and linear gradient mode are optional.

ORIENTATION / DIRECTION Description

Specifies a gradient from upper right to lower


BackwardDiagonal
left.
Specifies a gradient from upper left to lower
ForwardDiagonal
right.

Horizontal Specifies a gradient from left to right.

Vertical Specifies a gradient from top to bottom.

● LinearGradientBrush properties
Property Description
Represents the Blend object that specifies gradient
Blend
position and factors.
Represents gamma correction. If it is enabled, the
GammaCorrection
value is true; if not, it is false.

Represents a ColorBlend object that defines a


InterPolationColors
multicolor gradient.

Represents the starting and ending colors of a


LinearColors
gradient.

Returns a rectangle that defines the starting and


Rectangle
ending points of a gradient.

Represents a Matrix object that defines the


Transform
transformation.

Represents a WrapMode enumeration that indicates


WrapMode
the wrap mode.
3. Distinguish between linear gradient brush and solid brush

Linear Gradient brush Solid brush


1. Colors change smoothly from 1. Fills evenly with one uniform
one to another color

2. Requires defining at least two 2. Needs only a single color value


color stops input
3. Blends colors along a linear path 3. Gives a consistent flat color fill
4. Lets you control the direction of 4. No control over color blending
color change available
5. Can simulate lighting and 5. Limited for simulating lighting
shadows effects
6. Appears more realistic than solid 6. Can sometimes look artificial
colors
7. Calculates colors in real-time 7. Very simple and straight-forward
8. Setup requires more time and 8. Extremely quick and easy to use
effort
9. Gives a variable color fill in region 9. Provides a constant single color
fill
10. Better for advanced and 10. Better option for basic shapes
creative use cases and use cases

You might also like