C# Cat 2
C# Cat 2
In Windows Forms, the ToolTip represents a tiny pop-up box which appears when you place
your pointer or cursor on the control and the purpose of this control is it provides a brief
description about the control present in the windows form. The ToolTip class is used to create
ToolTip control and also provide different types of properties, methods, events and also
provides run time status of the controls.
You are allowed to use a ToolTip class in any container or control. With the help of a single
ToolTip component, you are allowed to create multiple tooltips for multiple controls. the
ToolTip class defined under System.Windows.Forms namespace. In C# you can create a
ToolTip in the windows form by using two different ways:
1. Design-Time: It is the easiest way to create a ToolTip as shown in 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 ToolTip from the ToolBox and drop it on the form. When you drag
and drop this ToolTip on the form it will automatically add to the properties(named
as ToolTip on ToolTip1) of every controls present in the current windows from.
From viki
• Step 3: After drag and drop you will go to the properties of the ToolTip control to
modify ToolTip according to your requirement.
Output:
2. Run-Time: It is a little bit trickier than the above method. In this method, you can create a
ToolTip control programmatically with the help of syntax provided by the ToolTip class. The
following steps show how to set the create ToolTip dynamically:
• Step 1: Create a ToolTip control using the ToolTip() constructor is provided by the
ToolTip class.
• // Creating a ToolTip control
• ToolTip t_Tip = new ToolTip();
• Step 2: After creating ToolTip control, set the property of the ToolTip control
provided by the ToolTip class.
• // Seting the properties of ToolTip
• t_Tip.Active = true;
• t_Tip.AutoPopDelay = 4000;
• t_Tip.InitialDelay = 600;
• t_Tip.IsBalloon = true;
From viki
• t_Tip.ToolTipIcon = ToolTipIcon.Info;
• t_Tip.SetToolTip(box1, "Name should start with Capital letter");
• t_Tip.SetToolTip(box2, "Password should be greater than 8 words");
Example:
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 WindowsFormsApp34 {
public Form1()
{
InitializeComponent();
}
From viki
// Adding this Label
// control to the form
this.Controls.Add(l2);
Output:
From viki
C# - Polymorphism
The word polymorphism means having many forms. In object-oriented programming paradigm,
polymorphism is often expressed as 'one interface, multiple functions'.
Polymorphism can be static or dynamic. In static polymorphism, the response to a function is
determined at the compile time. In dynamic polymorphism, it is decided at run-time.
Static Polymorphism
The mechanism of linking a function with an object during compile time is called early binding. It is
also called static binding. C# provides two techniques to implement static polymorphism. They are
−
• Function overloading
• Operator overloading
We discuss operator overloading in next chapter.
Function Overloading
You can have multiple definitions for the same function name in the same scope. The definition of
the function must differ from each other by the types and/or the number of arguments in the
argument list. You cannot overload function declarations that differ only by return type.
The following example shows using function print() to print different data types −
using System;
namespace PolymorphismApplication {
class Printdata {
void print(int i) {
Console.WriteLine("Printing int: {0}", i );
}
void print(double f) {
Console.WriteLine("Printing float: {0}" , f);
}
void print(string s) {
Console.WriteLine("Printing string: {0}", s);
}
static void Main(string[] args) {
Printdata p = new Printdata();
When the above code is compiled and executed, it produces the following result −
From viki
Printing int: 5
Printing float: 500.263
Printing string: Hello C++
Dynamic Polymorphism
C# allows you to create abstract classes that are used to provide partial class implementation of
an interface. Implementation is completed when a derived class inherits from it. Abstract classes
contain abstract methods, which are implemented by the derived class. The derived classes have
more specialized functionality.
Here are the rules about abstract classes −
• You cannot create an instance of an abstract class
• You cannot declare an abstract method outside an abstract class
• When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared
sealed.
The following program for dynamic polymorphism
1. using System;
2. public class Shape{
3. public virtual void draw(){
4. Console.WriteLine("drawing...");
5. }
6. }
7. public class Rectangle: Shape
8. {
9. public override void draw()
10. {
11. Console.WriteLine("drawing rectangle...");
12. }
13.
14. }
15. public class Circle : Shape
16. {
17. public override void draw()
18. {
19. Console.WriteLine("drawing circle...");
20. }
21.
22. }
23. public class TestPolymorphism
24. {
25. public static void Main()
From viki
26. {
27. Shape s;
28. s = new Shape();
29. s.draw();
30. s = new Rectangle();
31. s.draw();
32. s = new Circle();
33. s.draw();
34.
35. }
36. }
Output:
drawing...
drawing rectangle...
drawing circle...
When you have a function defined in a class that you want to be implemented in an inherited
class(es), you use virtual functions. The virtual functions could be implemented differently in
different inherited class and the call to these functions will be decided at runtime.
Dynamic polymorphism is implemented by abstract classes and virtual functions.
From viki
C# Multithreading
• Multitasking is the simultaneous execution of multiple tasks or processes over a certain
time interval.
• Every program that executes on your system is a process and to run the code inside the
application a process uses a term known as a thread.
A thread is a lightweight process, or in other words, a thread is a unit which executes
the code under the program. So every program has logic and a thread is responsible for
executing this logic. Every program by default carries one thread to executes the logic of
the program and the thread is known as the Main Thread
• Every program or application is by default single-threaded model.
• This single-threaded model has a drawback. The single thread runs all the process
present in the program in synchronizing manner, means one after another. So, the
second process waits until the first process completes its execution, it consumes more
time in processing.
For example, we have a class named as Geek and this class contains two different methods,
i.e method1, method2. Now the main thread is responsible for executing all these methods, so
the main thread executes all these methods one by one.
Example:
// C# program to illustrate the
// concept of single threaded model
using System;
using System.Threading;
From viki
}
}
}
// Driver Class
public class GFG {
// Main Method
static public void Main()
{
Output:
Method1 is : 0
Method1 is : 1
Method1 is : 2
Method1 is : 3
Method1 is : 4
Method1 is : 5
Method1 is : 6
Method1 is : 7
Method1 is : 8
Method1 is : 9
Method1 is : 10
Method2 is : 0
Method2 is : 1
Method2 is : 2
Method2 is : 3
From viki
Method2 is : 4
Method2 is : 5
Method2 is : 6
Method2 is : 7
Method2 is : 8
Method2 is : 9
Method2 is : 10
Advantages of Multithreading:
• It executes multiple process simultaneously.
• Maximize the utilization of CPU resources.
• Time sharing between multiple process.
C# | Encapsulation
• Difficulty Level : Easy
• Last Updated : 23 Jan, 2019
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism
that binds together code and the data it manipulates. In a different way, encapsulation is a
protective shield that prevents the data from being accessed by the code outside this shield.
• Technically in encapsulation, the variables or data of a class are hidden from any
other class and can be accessed only through any member function of own class in
which they are declared.
• As in encapsulation, the data in a class is hidden from other classes, so it is also
known as data-hiding.
• Encapsulation can be achieved by: Declaring all the variables in the class as
private and using C# Properties in the class to set and get the values of variables.
Example:
// C# program to illustrate encapsulation
using System;
From viki
// these can only be accessed by
// public methods of class
private String studentName;
private int studentAge;
get
{
return studentName;
}
set
{
studentName = value;
}
get
{
return studentAge;
}
set
{
studentAge = value;
}
// Driver Class
class GFG {
// Main Method
static public void Main()
{
// creating object
DemoEncap obj = new DemoEncap();
From viki
// and pass "VIKI" as value of the
// standard field 'value'
obj.Name = "VIKI";
Output:
Name: VIKI
Age: 21
Explanation: In the above program the class DemoEncap is encapsulated as the variables
are declared as private. To access these private variables we are using the Name and Age
accessors which contains the get and set method to retrieve and set the values of private
fields. Accessors are defined as public so that they can access in other class.
Advantages of Encapsulation:
• Data Hiding: The user will have no idea about the inner implementation of the class.
It will not be visible to the user that how the class is stored values in the variables.
He only knows that we are passing the values to accessors and variables are getting
initialized to that value.
• Increased Flexibility: We can make the variables of the class as read-only or write-
only depending on our requirement. If we wish to make the variables as read-only
then we have to only use Get Accessor in the code. If we wish to make the variables
as write-only then we have to only use Set Accessor.
• Reusability: Encapsulation also improves the re-usability and easy to change with
new requirements.
• Testing code is easy: Encapsulated code is easy to test for unit testing
GDI + 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
From viki
3. Typography
4. Printing
5. Design
namespace WindowsFormsApp2
From viki
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
From viki