0% found this document useful (0 votes)
21 views33 pages

C# Windows Forms UI Examples

The document contains multiple examples of C# Windows Forms applications, demonstrating various controls such as MessageBox, ContextMenuStrip, RadioButton, CheckBox, ListBox, ComboBox, Dialog, ProgressBar, ScrollBar, and Timer. Each example includes code snippets that illustrate how to implement these controls and their functionalities. The document is prepared by Bhartiba Parmar from Swami Vivekanand College.

Uploaded by

A Azher Khan
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)
21 views33 pages

C# Windows Forms UI Examples

The document contains multiple examples of C# Windows Forms applications, demonstrating various controls such as MessageBox, ContextMenuStrip, RadioButton, CheckBox, ListBox, ComboBox, Dialog, ProgressBar, ScrollBar, and Timer. Each example includes code snippets that illustrate how to implement these controls and their functionalities. The document is prepared by Bhartiba Parmar from Swami Vivekanand College.

Uploaded by

A Azher Khan
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

1.

Messagebox example

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace msgboxex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnclick_Click(object sender, EventArgs e)


{
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
[Link]("welcome", "project", [Link],
[Link]);
}
}

[Link] example

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace contextmenuexample
{
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

private void oddEvenToolStripMenuItem_Click(object sender, EventArgs e)


{
int no;
no = [Link]([Link]);
if(no%2==0)
{
[Link] = "No Is EVEN";
}
else
{
[Link] = "No Is odd";
}

private void positiveNegativeToolStripMenuItem_Click(object sender, EventArgs e)


{
int no;
no = [Link]([Link]);
if (no > 0)
{
[Link] = "No Is posive";
}
else
{
[Link] = "No Is negative";
}

}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
[Link] Substraction Multiplication Division
example.

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace Additionex
{
public partial class Form1 : Form
{
public Form1()
{
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)


{
int no1, no2;

no1 = Convert.ToInt16([Link]);
no2 = Convert.ToInt16([Link]);

[Link] = (no1 * no2).ToString();

private void button3_Click(object sender, EventArgs e)


{
int no1, no2;

no1 = Convert.ToInt16([Link]);
no2 = Convert.ToInt16([Link]);

[Link] = (no1 / no2).ToString();

private void btnadd_Click(object sender, EventArgs e)


{
int no1, no2;

no1 = Convert.ToInt16([Link]);
no2 = Convert.ToInt16([Link]);

[Link] = (no1 + no2).ToString();

private void button4_Click(object sender, EventArgs e)


{
int no1, no2;

no1 = Convert.ToInt16([Link]);
no2 = Convert.ToInt16([Link]);

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
[Link] = (no1 - no2).ToString();

private void button5_Click(object sender, EventArgs e)


{
[Link] = "";
[Link] = "";
[Link] = "";
[Link]();
}
}
}

4. RadioButton example.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace radiobuttonex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)


{
[Link] = [Link]("C:\\Users\\Lenovo\\Pictures\\Saved
Pictures\\[Link]");

private void radioButton2_CheckedChanged(object sender, EventArgs e)


{
[Link] = [Link]("C:\\Users\\Lenovo\\Pictures\\Saved
Pictures\\[Link]");
}

private void button1_Click(object sender, EventArgs e)


{
string name, gen;
name = [Link];
if([Link]== true)
{
gen = "Male";

}
else
{

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
gen = "Female";
}
[Link]("name="+ name+"gender="+gen);
}
}
}

5. CheckBox example.

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace checkboxex
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
if([Link]==true)
{
if([Link]==true)
{
[Link] = "50";
}
else if([Link] == true)
{
[Link] = "30";
}
else if ([Link] == true)
{
[Link] = "15";
}
}
if ([Link] == true)
{
if ([Link] == true)
{
[Link] = "35";
}
else if ([Link] == true)
{
[Link] = "85";
}
else if ([Link] == true)
{
[Link] = "10";
}
}
}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
6. ListBox example.

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace listboxexample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
private void btnclear_Click(object sender, EventArgs e)
{
[Link]();
}

private void Form1_Load(object sender, EventArgs e)


{
[Link]("Hello");
[Link]("How");
[Link]("are");
[Link]("you");
[Link]("sv");
[Link]("students");
}

private void btnRemoveAt_Click(object sender, EventArgs e)


{
int pos = Convert.ToInt16([Link]);
[Link](pos);
}

private void btnremove_Click(object sender, EventArgs e)


{
[Link]([Link]);
}

private void btncontains_Click(object sender, EventArgs e)


{
string str = [Link];
if([Link](str)==true)
{
[Link]("YES IN THE LIST");
}
else
{
[Link]("No IN THE LIST");
}
}

private void btnindex_Click(object sender, EventArgs e)


{
string str = [Link];

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
int pos = [Link](str);
if (pos>-1)
{
[Link]("POS+"+pos);
}
else
{
[Link]("Not FOUND POS =" + pos);
}
}
}
}

7. Example Of combobox Control.

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
namespace comboboxex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void comboBox1_SelectedIndexChanged(object sender,


EventArgs e)
{
int a, b, c;
a = Convert.ToInt16([Link]);
b = Convert.ToInt16([Link]);
if([Link]=="+")
{
c = a + b;
[Link] = [Link](c);
}
else if ([Link] == "-")
{
c = a - b;
[Link] = [Link](c);
}
else if ([Link] == "*")
{
c = a * b;
[Link] = [Link](c);
}
else if ([Link] == "/")
{
c = a / b;
[Link] = [Link](c);
}

}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
8. Example Of Dialog Control.

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace dialogcontrolex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
private void button1_Click(object sender, EventArgs e)
{
if([Link]()==[Link])
{
[Link] = [Link];
}
}

private void button2_Click(object sender, EventArgs e)


{
if ([Link]() == [Link])
{
[Link] = [Link];
}
}

private void button3_Click(object sender, EventArgs e)


{

if ([Link]() == [Link])
{
[Link] = [Link];
[Link] = [Link];
}
}

private void button4_Click(object sender, EventArgs e)


{
[Link] = "Text Files| *.txt|word
file|*.doc";
if ([Link]() == [Link])
{
[Link]([Link],
[Link]);
[Link] = [Link];
}
}

private void button5_Click(object sender, EventArgs e)


{
SaveFileDialog savedialog = new SaveFileDialog();
[Link] = "save";
[Link] = "Text Files( *.txt)|*.txt" + "|"
+ "Image Files(*.png;*.jpg)| *.png;*.jpg"+"|"+"All Files(*.*)|*.*";

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
if([Link]()==[Link])
{
string file = [Link];
}
/*if ([Link]() == [Link])
{
[Link]([Link],
[Link]);
[Link] = [Link];
}*/
}
}
}

9. Example Of listbox Control.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace listbox1ex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnRmov1_Click(object sender, EventArgs e)


{
[Link]([Link]);
[Link]([Link]);

private void btnRselected_Click(object sender, EventArgs e)


{
for(int i=0;i<=[Link];i++)
{
[Link]([Link][i]);
[Link]([Link][i]);
}
}

private void btnRall_Click(object sender, EventArgs e)


{
for (int i = 0; i <= [Link]-1; i++)
{
[Link]([Link][i]);

}
[Link]();

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
private void btnmov1_Click(object sender, EventArgs e)
{
[Link]([Link]);
[Link]([Link]);
}

private void btnselected_Click(object sender, EventArgs e)


{
for (int i = 0; i <= [Link]; i++)
{
[Link]([Link][0]);
[Link]([Link][0]);
}
}

private void btnall_Click(object sender, EventArgs e)


{
for (int i = 0; i <= [Link] - 1; i++)
{
[Link]([Link][i]);

}
[Link]();

}
}
}
10. Example Of Progressbar Control.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace progressbarex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
[Link] = 0;
[Link] = 100;
}

private void timer1_Tick(object sender, EventArgs e)


{
if ([Link] < 100)
{
[Link] = [Link] + 5;
}
}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
11. Example Of scrollbar Control.

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace scrolbarex
{
public partial class Form1 : Form
{
public Form1()
{
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
InitializeComponent();
}

void changecolor()
{
[Link] = [Link]([Link],
[Link], [Link]);
}

private void Form1_Load(object sender, EventArgs e)


{

private void hScrollBar1_Scroll(object sender, ScrollEventArgs


e)
{
changecolor();
}

private void hScrollBar2_Scroll(object sender, ScrollEventArgs


e)
{
changecolor();
}

private void hScrollBar3_Scroll(object sender, ScrollEventArgs


e)
{
changecolor();
}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
12. Example Of Timer Control.

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace timerex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int flag = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if(flag==0)
{
[Link] = true;
[Link] = false;
[Link] = false;

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
flag = 1;
}
else if(flag==1)
{
[Link] = false;
[Link] = true;
[Link] = false;

flag = 2;
}

else
{
[Link] = false;
[Link] = false;
[Link] = true;

flag = 0;
}

}
}
}
13. Example Of Tooltip Control.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace tooltipcontrolex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
[Link](textBox1, "hello");
}
}
}

14. Example Of Treeview Control.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace treeviewexample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnaddroot_Click(object sender, EventArgs e)


{
[Link]([Link]);
}

private void btnchild_Click(object sender, EventArgs e)


{
[Link]([Link]);
}

private void btnclear_Click(object sender, EventArgs e)


{
[Link]();
}

private void btncount_Click(object sender, EventArgs e)


{
[Link]([Link]([Link]));
}

private void btnexpand_Click(object sender, EventArgs e)


{
[Link]();
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
private void btncollapse_Click(object sender, EventArgs e)
{
[Link]();
}

private void btnremove_Click(object sender, EventArgs e)


{
[Link]();
}
}
}

15. Example Of Calculator.

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace CaclulatorDemo
{
public partial class Form1 : Form
{
const string divideByZero = "Err!";
const string syntaxErr = "SYNTAX ERROR!";
bool decimalPointActive = false;
public Form1()
{
InitializeComponent();
}

private void BtnCopy_Click(object sender, EventArgs e)


{
if ([Link]([Link])) return;
[Link]([Link]);
}

private void BtnReset_Click(object sender, EventArgs e)


{
decimalPointActive = false;
PreCheck_ButtonClick();
previousOperation = [Link];
[Link]();
}

private void BtnClear_Click(object sender, EventArgs e)


{
decimalPointActive = false;
PreCheck_ButtonClick();
if ([Link] > 0)
{
double d;

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
if
(![Link]([Link][[Link] -
1].ToString(), out d))
{
previousOperation = [Link];
}

[Link] =
[Link]([Link] - 1, 1);
}
if ([Link] == 0)
{
previousOperation = [Link];
}
if(previousOperation != [Link])
{
currentOperation = previousOperation;
}
}

private void BtnDiv_Click(object sender, EventArgs e)


{
if ([Link] == 0) return;
PreCheck_ButtonClick();
currentOperation = [Link];
PerformCalculation(previousOperation);

previousOperation = currentOperation;
EnableOperatorButtons(false);
[Link] += (sender as Button).Text;
}

private void BtnMul_Click(object sender, EventArgs e)


{
if ([Link] == 0) return;
PreCheck_ButtonClick();
currentOperation = [Link];
PerformCalculation(previousOperation);
previousOperation = currentOperation;
EnableOperatorButtons(false);
[Link] += (sender as Button).Text;
}

private void BtnSub_Click(object sender, EventArgs e)


{

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
if ([Link] == 0 || previousOperation ==
[Link]) return;
PreCheck_ButtonClick();
currentOperation = [Link];
PerformCalculation(previousOperation);

previousOperation = currentOperation;
EnableOperatorButtons(false);
[Link] += (sender as Button).Text;
}

private void BtnAdd_Click(object sender, EventArgs e)


{
if ([Link] == 0) return;
PreCheck_ButtonClick();
currentOperation = [Link];
PerformCalculation(previousOperation);

previousOperation = currentOperation;
EnableOperatorButtons(false);
[Link] += (sender as Button).Text;
}

private void PerformCalculation(Operation previousOperation)


{
try
{
if (previousOperation == [Link])
return;
List<double> lstNums = null;

switch (previousOperation)
{
case [Link]:
if (currentOperation == [Link])
{
currentOperation = [Link];
return;
}
lstNums =
[Link]('+').Select([Link]).ToList();
[Link] = (lstNums[0] +
lstNums[1]).ToString();
break;
case [Link]:

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
int idx = [Link]('-'); //
To handle ex: -9-2
if (idx > 0)
{
var op1 =
[Link]([Link](0, idx));
var op2 =
[Link]([Link](idx + 1));
[Link] = (op1 - op2).ToString();
}
break;
case [Link]:
if (currentOperation == [Link])
{
currentOperation = [Link];
return;
}
lstNums =
[Link]('*').Select([Link]).ToList();
[Link] = (lstNums[0] *
lstNums[1]).ToString();
break;
case [Link]:
if (currentOperation == [Link])
{
currentOperation = [Link];
return;
}
try
{
lstNums =
[Link]('/').Select([Link]).ToList();
if (lstNums[1] == 0)
{
throw new DivideByZeroException();
}
[Link] = (lstNums[0] /
lstNums[1]).ToString();
}
catch (DivideByZeroException)
{
[Link] = divideByZero;
}
break;
case [Link]:

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
default:
break;
}
}
catch (Exception)
{
[Link] = syntaxErr;
}
}

private void BtnNum_Click(object btn, EventArgs e)


{
if([Link] == syntaxErr || [Link] ==
divideByZero)
{
[Link] = [Link];
}
EnableOperatorButtons();
PreCheck_ButtonClick();
[Link] += (btn as Button).Text;
}

private void PreCheck_ButtonClick()


{
if ([Link] == divideByZero || [Link] ==
syntaxErr)
[Link]();
if(previousOperation != [Link])
{
EnableOperatorButtons();
}
}

private void EnableOperatorButtons(bool enable = true)


{
[Link] = enable;
[Link] = enable;
[Link] = enable;
if (!enable)
{
decimalPointActive = false;
}
//[Link] = enable;
}
enum Operation

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
{
Add,
Sub,
Mul,
Div,
None
}

Operation previousOperation = [Link];


Operation currentOperation = [Link];

private void BtnRes_Click(object sender, EventArgs e)


{
if ([Link] == 0) return;
if (previousOperation != [Link])
PerformCalculation(previousOperation);

previousOperation = [Link];
}

private void BtnDecimal_Click(object sender, EventArgs e)


{
if (decimalPointActive) return;
if ([Link] == syntaxErr || [Link] ==
divideByZero)
{
[Link] = [Link];
}
EnableOperatorButtons();
PreCheck_ButtonClick();
[Link] += (sender as Button).Text;
decimalPointActive = true;
}

private void tableLayoutPanel2_Paint(object sender,


PaintEventArgs e)
{

}
}
}

-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College
-------------------------------------------------------------------------------------------------------------
Prepared By:- Bhartiba Parmar
Swami Vivekanand College

You might also like