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

Code

Uploaded by

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

Code

Uploaded by

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

Visual Programming using C#

Q. Choose one of the class and mark from the semester checkbox
and show the subjects about that class of semester

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

namespace WF01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void setItems()
{
listBox1.Items.Clear();

MCS (3rd Semester ) Page 1


Visual Programming using C#

if (radioButton1.Checked)
{
if (checkBox1.Checked == true)
{
listBox1.Items.Add("Subjects of MCS Semester-I");
listBox1.Items.Add("Programming Fundamentals");
listBox1.Items.Add("DLD");
listBox1.Items.Add("OS");
listBox1.Items.Add("DBMS");
listBox1.Items.Add("BC");
listBox1.Items.Add("Islamiat");
}
if (checkBox2.Checked == true)
{
listBox1.Items.Add("Subjects of MCS Semester-II");
listBox1.Items.Add("SE-I");
listBox1.Items.Add("Java");
listBox1.Items.Add("Web");
listBox1.Items.Add("DDBMS");
listBox1.Items.Add("DS");
listBox1.Items.Add("DCN");
}

}
else if (radioButton2.Checked)
{
if (checkBox1.Checked == true)
{
listBox1.Items.Add("Subjects of BSCS Semester-I");
listBox1.Items.Add("Intro to Computing");
listBox1.Items.Add("Fundamentals of Algorithm");
listBox1.Items.Add("English Comprehension");
listBox1.Items.Add("Calculus-I");
listBox1.Items.Add("Accounting");
listBox1.Items.Add("Islamiat & Pak Studies");
}
if (checkBox2.Checked == true)
{
listBox1.Items.Add("Subjects of BSCS Semester-II");
listBox1.Items.Add("Programming Fundamentals");
listBox1.Items.Add("DCN");
listBox1.Items.Add("Calculus-II");
listBox1.Items.Add("BC");
listBox1.Items.Add("DBMS");
listBox1.Items.Add("Web");
}

MCS (3rd Semester ) Page 2


Visual Programming using C#

}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
setItems();
}

private void checkBox2_CheckedChanged(object sender, EventArgs e)


{
setItems();
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)


{
setItems();
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)


{
setItems();
}
}
}

MCS (3rd Semester ) Page 3


Visual Programming using C#

Q. Write text in the textbox that will add text into listbox on clicking
the Add item button, count item in the listbox on clicking the count
button, Show Item names that in listbox on the MessageBox by
clicking on the Show Item button.

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

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

private void button1_Click(object sender, EventArgs e)


{

MCS (3rd Semester ) Page 4


Visual Programming using C#

listBox1.Items.Add(textBox1.Text);
textBox1.Clear();
}

private void button2_Click(object sender, EventArgs e)


{
MessageBox.Show("Count Items: "+listBox1.Items.Count.ToString(),"Current
Value");
}

private void button5_Click(object sender, EventArgs e)


{
Application.Exit();
}

private void button3_Click(object sender, EventArgs e)


{
listBox1.Items.Clear();
}

private void button4_Click(object sender, EventArgs e)


{
string items = "";
foreach (var item in listBox1.Items)
{
items += item.ToString() + ", ";
}
MessageBox.Show(items);
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)


{
for (int i = listBox1.SelectedItems.Count - 1; i >= 0; i--)
{
listBox1.Items.Remove(listBox1.SelectedItems[i]);
}
}
}
}

MCS (3rd Semester ) Page 5


Visual Programming using C#

Q. Write Number in the textbox and show the even number in the
listbox and the odd number in the (multiline textbox) textbox.

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

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

private void button1_Click(object sender, EventArgs e)


{
int a;
a = int.Parse(textBox1.Text);
textBox1.Clear();
if (a % 2 == 0)
{
listBox1.Items.Add(a);

MCS (3rd Semester ) Page 6


Visual Programming using C#

}
else if(a % 2==1)
{
textBox2.AppendText(a+"\n");
}
}
}
}
Q. How to make a textbox to MultiLine

1-Place Textbox on the form


2-Click Textbox where this sign show on textbox and the option will appear and then
choose the checkbox of MultiLine option as shown in the above figure.

MCS (3rd Semester ) Page 7


Visual Programming using C#

Q. Write number in the textbox and show their table in the listbox and
the textbox.

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

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

private void button1_Click(object sender, EventArgs e)


{
int a=0;
a = int.Parse(textBox1.Text);

MCS (3rd Semester ) Page 8


Visual Programming using C#

for (int i = 1; i <= 10; i++)


{
listBox1.Items.Add(a +" X "+ i +" = "+ a*i);
textBox2.AppendText(a + " X " + i + " = " + a * i+"\n");
}
}

private void button2_Click(object sender, EventArgs e)


{
listBox1.Items.Clear();
textBox2.Clear();
textBox1.Clear();
}

private void button3_Click(object sender, EventArgs e)


{
this.Close();
}
}
}

Q.Write Text in the textbox and add that text into the listbox and then
select item from listbox and add it into the multiline textbox

Textbox Scrollbar
For set scrollbar in the textbox then go to in the properties of textbox and change the
scrollbars settings of the textbox as n the picture its set as vertical.

MCS (3rd Semester ) Page 9


Visual Programming using C#

Listbox Scrollbar
For listbox to set scroll bar then go into the properties of listbox and set the
ScrollbarAlwaysVisible =True

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

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

private void button1_Click(object sender, EventArgs e)


{
listBox1.Items.Add(textBox1.Text);

MCS (3rd Semester ) Page 10


Visual Programming using C#

textBox1.Clear();
}

private void button2_Click(object sender, EventArgs e)


{
listBox1.Items.Clear();
textBox2.Clear();
}

private void button3_Click(object sender, EventArgs e)


{
string items = " ";
foreach (var item in listBox1.SelectedItems)
{
items += item.ToString() + "\n";
textBox2.AppendText(items);
}
}

private void button4_Click(object sender, EventArgs e)


{
this.Close();
}
}
}

Q. Program to give user name and password to get login if the user
name and password true then a message box will appear and show
you are granted access and after click on the ok button of the
Message Box a New Form will appear on the screen if the password is
not correct then the Application is exit after 3 tries.

MCS (3rd Semester ) Page 11


Visual Programming using C#

How to save Username and Password in the Tag

For save username and password we use the tag property of the textbox to save the
user name and password in tag property as you see in the above GUI or pictures.

How to Add new Form

For Add new form we do this process as see in the below picture

MCS (3rd Semester ) Page 12


Visual Programming using C#

After doing this see the step in the below picture for to Add form

How to set Timer Property

First we do to true the enabled property of timer and Second we set timer interval
property like in the above picture wee set interval property is 400.

MCS (3rd Semester ) Page 13


Visual Programming using C#

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

namespace ChangeForm
{
public partial class Form1 : Form
{
int atempt;
public Form1()
{
InitializeComponent();
atempt = 1;
}

private void button2_Click(object sender, EventArgs e)


{
this.Close();
}

private void button1_Click(object sender, EventArgs e)


{
if (textBox1.Text.Equals(textBox1.Tag) && textBox2.Text.Equals(textBox2.Tag))
{
MessageBox.Show("Access Granted to " + textBox1.Text + " !!!\nYou are
authorized user", "Authorized");
textBox1.Text = "";
textBox2.Text = "";
Form2 frm = new Form2();
frm.Show();
}
else
{
MessageBox.Show("Invalid User Name or Password Try Again ....", "Error");
atempt++;
if (atempt > 3) Application.Exit();
}
}

private void timer1_Tick(object sender, EventArgs e)


{

MCS (3rd Semester ) Page 14


Visual Programming using C#

if (label1.Visible)
label1.Visible = false;
else
label1.Visible = true;
}
}
}

Q. Program to Show Current Time in the Textbox and Also Show the
Day and Date in the Textbox

Note:Set Timer Property of Interval=1000 and the Enabled =True

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

namespace WindowsFormsApplication1
{
public partial class Form1 : Form

MCS (3rd Semester ) Page 15


Visual Programming using C#

{
public Form1()
{
InitializeComponent();
textBox2.Text = DateTime.Now.ToLongDateString();
textBox3.Text = DateTime.Now.ToShortDateString();
textBox4.Text = DateTime.Now.Day + "-" + DateTime.Now.Month + "-" +
DateTime.Now.Year;
}

private void timer1_Tick(object sender, EventArgs e)


{
textBox1.Text = DateTime.Now.ToLongTimeString () ;
this.Text = "CSIT "+ textBox1.Text;
}
}
}

MCS (3rd Semester ) Page 16

You might also like