Académique Documents
Professionnel Documents
Culture Documents
2010
GUI Programming using C#
-
Form control
- A container for Windows Applications
o Text
o Icon
o BackgroundImage
o BackgrounImageLayout
o StartPosition
o WindowState
o Opacity
o MinimizeBox
o MaximizeBox
o FormBorderStyle
o HelpButton
o ControlBox
o ShowInTaskBar
o TopMost
Common Properties on Controls
1. Name
a. Every control must have unique name
2. BackColor
3. ForeColor
4. Font
5. Size
6. Location
7. Enabled=true/false
8. Visible=true/false
9. TabIndex
10. TabStop
11. Dock
Naming a control
- If possible use Hungarian notation given by Charles Simony of Hungary. It states
that use three character prefix to define type of control along with purpose of
control
o Form frm
o Label lbl
o TextBox txt
o Button btn or cmd
o Combo Box cbo
o ListBox lst
o Checkbox chk
o Etc.
Label control
- To provide some text
o Text
- Use & to make the hot key
TextBox control
- To create single line text, multi line text and password field
o Text
o PasswordChar
o Multiline
o Scrollbars
o MaxLength
o ReadOnly
o TextAlign
Button control
-
ToolTip control
-
Test Application 1
Write a Windows Application to input a string and show its reverse.
Test Application 2
Write a program to have an array of 10 numbers. Input the data from a text box and pass
into an array element.
On a label show current number of inputs. If inputs goes 10 print the smallest and biggest
of those numbers on the form.
22.07.2010
Checkbox control
- To select none or all
o Text
o Image
o TextAlign
o ImageAlign
o Checked
Test Case
Create a form having a checkbox [ ] Agree to Terms and Condition and a button having
text and image for Next
When checkbox is checked Enable the button otherwise disable the button
Example
private void chkAgree_CheckedChanged(object sender, EventArgs e)
{
cmdNext.Enabled = chkAgree.Checked;
}
2. Response Dialogs
Example
Write a program to ask as user for being adult. If yes, allow to join else send back
Solution
FontDialog
ColorDialog
OpenFileDialog
SaveFileDialog
FolderBrowserDialog
Sample Code
private void cmdColor_Click(object sender, EventArgs e)
{
colorDialog1.FullOpen = true;
colorDialog1.ShowDialog();
textBox1.ForeColor = colorDialog1.Color;
}
Font Dialog
- To select a font and some colors
o Font
o Color
o ShowColor=true/false
RichTextBox control
- Advance version of TextBox that allows to work with selected portion of text
o Text
o ForeColor
o BackColor
o SelectionColor
o SelectionFont
o Cut()
o Copy()
o Paste()
o SelectAll()
o LoadFile()
o SaveFile()
PictureBox control
- To select an image
o Image
o SizeMode
o BorderStyle
Open File Dialog/Save File Dialog control
- Allows to select a file
o Filter
o FileName
Example
private void cmdBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
openFileDialog1.Filter = "Image Files|
*.jpg;*.gif;*.png;*.bmp";
DialogResult ans=openFileDialog1.ShowDialog();
if (ans == DialogResult.OK)
{
pictureBox1.Image =
Image.FromFile(openFileDialog1.FileName);
}
FolderBrowser Dialog
- Used to select a folder
o SelectedPath
Creating Menus
-
Timer control
- to repeat a process after given interval of time
o Interval=time in milliseconds
o Enabled=true/false
- Provided under Components section
Creating MDI Application
- Allows to create/open Multiple Documents under one Window
- Each of such application must have a MDI Container
- An application can have many normal forms but only one MDI Container
- To make a form as MDI Form set its
o IsMdiContainer =true/false
To set a form as child form use MdiParent property
To set the placement of child forms use LayoutMdi() method with MdiLayout
enumerator
To get reference current form use ActiveMdiChild property
To get reference of all the children inside an MDI form use MdiChildren
collection
To get reference of a control inside a form use Controls collection
Example
colorDialog1.ShowDialog();
RichTextBox t = (RichTextBox) ActiveMdiChild.Controls["txtMain"];
t.SelectionColor = colorDialog1.Color;
27.07.2010
Creating Web Browser
-
Case 1
sender gives reference the current control on which activity has done
e helps in getting information and taking certain instructions from us
Show the current mouse position on a form when we move the mouse pointer on it.
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
string x = "Mouse is on " + e.X + "," + e.Y;
this.Text = x;
}
Case 2
Write an application to ask a dialog when we close a form by clicking on X
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult ans = MessageBox.Show("Quit the application", "Quit",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (ans == DialogResult.No)
e.Cancel = true;
}
Case 3
Create a text box to input a mobile number. Allow to input only digits and backspace.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int num =(int) e.KeyChar;
//this.Text = num.ToString();
if (!((num >= 48 && num <= 57) || num == 8))
e.Handled = true;
}
Case 4
Create a textbox to type a URL. If Enter key is pressed then show that URL in a web
browser. If Ctrl+Enter key is pressed then join http protocol and .com extension
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Enter)
{
string s = "http://" + textBox2.Text + ".com";
textBox2.Text = s;
webBrowser1.Navigate(s);
}
else if (e.KeyCode == Keys.Enter)
{
webBrowser1.Navigate(textBox2.Text);
}
}
Case 5
Refresh the browser when F5 key is pressed
Note: Write the such codes on the Form. Set KeyPreview
property of the form as true.
Using Tabcontrol
- used to hold more controls in less space
- Provided under Containers section
- It contains TabPages collection
Setting Icon of a Project
-
29.07.2010
ADO.NET (ActiveX Data Objects for .NET)
-
2. Sql Server
3. Oracle
Using MS Access as Database
-
Create a database
o 2003 (.mdb)
o 2007 & 2010 (.accdb)
o Example
B25db.mdb
Now create your tables as per your project
o Relations table
rcode AutoNumber Auto Generated - PK
rname Text 20
o Members table
mcode AutoNumber Auto Generated PK
mname Text 50
email Text 50
mobile Text 10
bloodgroup Text -3
rcode Number FK
Setting relationship among the tables as Primary and Foreign key in MS Access
Select Tools Relationship
Add both the tables and Drag and drop the field for setting the relationship as one-tomany relationship
Create the Forms as front-end
1. Relations Master
2. Member Master
Step 3
Create an object of OdbcCommand and execute the SQL
statement using ExecuteNonQuery() method
30.07.2010
Using MS Access with OleDb Technology
-
Import System.Data.OleDb
Create the SQL Statement. Use String.Format() method to create the SQL statement
Example
MessageBox.Show("Record Saved");
using System.Data.OleDb;
using System.Windows.Forms;
class Db
{
public static OleDbConnection GetConnection()
{
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" + Application.StartupPath + @"\b20db.mdb";
cn.Open();
return cn;
}
}
using
using
using
class
{
cn.Open();
return cn;
}
02.08.2010
Working with SQL Server
-
Start the Server and not down the server name or IP Address
Create a database
o E.g. b20
Create your tables
o Product table
Pcode Primary Key - Identity
Pname
Price
Qty
o Setting the auto incremental
Select the field name as Pcode
Select it properties
Identity Specifications
o Is Identity =true
o
Example
03.08.2010
Saving Images to Database
- Select an image to save into a PicureBox
- Read the Image from the PictureBox and place it into memory using a class
MemoryStream class of System.IO namespace
- Convert data into byte array using GetBuffer() method of MemoryStream class
- Save the byte array into database
- To pass binary data as parameter create an object of SqlParameter class
SqlParameter p=new SqlParameter(paramname, data type);
p.Value=binary data;
Test Case
Create table as Student (rollno numeric, name varchar(50), photo Image)
Create front-end form
Example
private void button2_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=.;Integrated
Security=true;Database=b20";
cn.Open();
string sql = "INSERT INTO student
VALUES(@rollno,@name,@photo)";
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@rollno", textBox1.Text);
cmd.Parameters.AddWithValue("@name", textBox2.Text);
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] data = ms.GetBuffer();
ms.Close();
SqlParameter p = new SqlParameter("@photo",
SqlDbType.Image);
p.Value = data;
cmd.Parameters.Add(p);
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Record Saved");
textBox1.Clear();
textBox2.Clear();
pictureBox1.Image = null;
}
To read data from data database and hold it at the client even after closing the
connection is called as disconnected mode
Use container classes to hold the data
o DataTable
DataTable()
DataTable(string tablename)
o DataSet
DataTable class can hold result of single query
DataSet is a collection of DataTable type objects and can hold result of many
queries that can be accessed using Tables collection of DataSet
To read data from database and hold it into some container use xxxDataAdapter
class
It provides Fill() method to fill data into a container
Example
string sql="Select * from employee";
Example
Write a program to read the records from Student table and show them one by one. Create
buttons First, Last, Prev and Next to show those records.
Also show the current record position and total number of records.
Data Binding
-
Relations *
rcode
rname
Members *
mcode
mname
mobile
email
rcode
10.08.2010
DataView control
-
o RowFilter="condition";
To sort the records based on a field use Sort property
o Sort="fieldname";
Doctors *
dcode
dname
mobile
email
Prescriptions *
pcode
mcode
dcode
dop
symptoms
spinstructions
Members *
mcode
mname
mobile
email
Medicines *
medcode
rcode
medname
medtypecode
dosage
pcode
Relations
rcode
rname
MedicineTypes *
medtypecode
medtypename
Crystal Reports
-
A reporting tool from Seagate that provides advance reports that can be printed or
exported
Add a crystal report from
o Project Add New Item Crystal Report
A file get created as .rpt
Select the table name and field names to be added on report then style of report
To use this report
o Add a new blank form
o Add the CrystalReporViewer control
o Select the CrystalReport name and its properties like
DisplayGroupTree=true/false
To show the report create an object of the form and show the report