Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

C# | ToolTip Class

• Last Updated : 05 Sep, 2019


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.

• 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;
• 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 partial class Form1 : Form {

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
// Creating and setting the
// properties of the Label
Label l1 = new Label();
l1.Location = new Point(140, 122);
l1.Text = "Name";

// Adding this Label


// control to the form
this.Controls.Add(l1);
// Creating and setting the
// properties of the TextBox
TextBox box1 = new TextBox();
box1.Location = new Point(248, 119);
box1.BorderStyle = BorderStyle.FixedSingle;

// Adding this TextBox


// control to the form
this.Controls.Add(box1);

// Creating and setting the


// properties of Label
Label l2 = new Label();
l2.Location = new Point(140, 152);
l2.Text = "Password";

// Adding this Label


// control to the form
this.Controls.Add(l2);

// Creating and setting the


// properties of the TextBox
TextBox box2 = new TextBox();
box2.Location = new Point(248, 145);
box2.BorderStyle = BorderStyle.FixedSingle;

// Adding this TextBox


// control to the form
this.Controls.Add(box2);

// Creating and setting the


// properties of the ToolTip
ToolTip t_Tip = new ToolTip();
t_Tip.Active = true;
t_Tip.AutoPopDelay = 4000;
t_Tip.InitialDelay = 600;
t_Tip.IsBalloon = true;
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");
}
}
}

Output:
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 −
Live Demo

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();

// Call print to print integer


p.print(5);

// Call print to print float


p.print(500.263);

// Call print to print string


p.print("Hello C++");
Console.ReadKey();
}
}
}
When the above code is compiled and executed, it produces the following result −
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 demonstrates an abstract class −
Live Demo

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()
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.
C# Multithreading
• Difficulty Level : Medium
• Last Updated : 22 Apr, 2020
Multitasking is the simultaneous execution of multiple tasks or processes over a certain time
interval. Windows operating system is an example of multitasking because it is capable of
running more than one process at a time like running Google Chrome, Notepad, VLC player,
etc. at the same time. The operating system uses a term known as a process to execute all these
applications at the same time. A process is a part of an operating system that is responsible for
executing an application. 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, so 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;

public class Geek {

// static method one


public static void method1()
{

// It prints numbers from 0 to 10


for (int I = 0; I <= 10; I++) {

Console.WriteLine("Method1 is : {0}", I);

// When the value of I is equal to


// 5 then this method sleeps for
// 6 seconds and after 6 seconds
// it resumes its working
if (I == 5) {
Thread.Sleep(6000);
}
}
}

// static method two


public static void method2()
{

// It prints numbers from 0 to 10


for (int J = 0; J <= 10; J++) {

Console.WriteLine("Method2 is : {0}", J);


}
}
}

// Driver Class
public class GFG {

// Main Method
static public void Main()
{

// Calling static methods


Geek.method1();
Geek.method2();
}
}

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
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;

public class DemoEncap {

// private variables declared


// these can only be accessed by
// public methods of class
private String studentName;
private int studentAge;

// using accessors to get and


// set the value of studentName
public String Name
{

get
{
return studentName;
}

set
{
studentName = value;
}

// using accessors to get and


// set the value of studentAge
public int Age
{

get
{
return studentAge;
}

set
{
studentAge = value;
}

// Driver Class
class GFG {

// Main Method
static public void Main()
{

// creating object
DemoEncap obj = new DemoEncap();

// calls set accessor of the property Name,


// and pass "Kavitha" as value of the
// standard field 'value'
obj.Name = "Kavitha";

// calls set accessor of the property Age,


// and pass "21" as value of the
// standard field 'value'
obj.Age = 21;

// Displaying values of the variables


Console.WriteLine("Name: " + obj.Name);
Console.WriteLine("Age: " + obj.Age);
}
}

Output:
Name: Kavitha
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
Code for creating menu bar
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 WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)


{
MessageBox.Show("Selected menu item 1");
}
}
}

You might also like