Professional Documents
Culture Documents
Visual C
Visual C
2010
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
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
Test Application 2
Write an application to get a number and Print some of its digits
Assignment
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;
}
Test Case
Create two groups
Gender Male, Female
Qualification MBA, MCA
DateTimePicker control
- To select Date, Time or Both
o Format
o CustomFormat
d Date
M Month
Y year
h hours
m minutes
s seconds
o ShowUpDown
o Value
Example
this.Text = dateTimePicker1.Value.ToString("dd-MMM-yyyy");
.NET Provides different kind of dialogs. Use MessageBox class with Show() method to
show a dialog. Dialogs can be of two types
2. Response Dialogs
DialogResult MessageBox.Show(string message, string title, MessageBoxButtons b,
MessageBoxIcon ic)
Example
Write a program to ask as user for being adult. If yes, allow to join else send back
Solution
private void button2_Click(object sender, EventArgs e)
{
DialogResult ans = MessageBox.Show("Are you adult", "Query",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (ans == DialogResult.Yes)
MessageBox.Show("You can join us", "Welcome",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else
MessageBox.Show("Sorry! Not Allowed", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
- FontDialog
- ColorDialog
- OpenFileDialog
- SaveFileDialog
- FolderBrowserDialog
Color Dialog
- To select a color
o Color
o FullOpen=true/false
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
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
26.07.2010
Note: To work with current date and time use DateTime class with Now property
Creating a toolbar
- Use ToolStip control
- Add the controls like Button, Combo box etc.
- Add Image and ToolTip on the buttons
- Now write the code on button
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;
Example
private void ChangeColor(object sender, EventArgs e)
{
Label x = (Label)sender;
f.BackColor = x.BackColor;
}
27.07.2010
- sender gives reference the current control on which activity has done
- e helps in getting information and taking certain instructions from us
Case 1
Show the current mouse position on a form when we move the mouse pointer on it.
Case 2
Write an application to ask a dialog when we close a form by clicking on X
Case 3
Create a text box to input a mobile number. Allow to input only digits and backspace.
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
Case 5
Using Tabcontrol
- used to hold more controls in less space
- Provided under Containers section
- It contains TabPages collection
File Add New Project Other Project Types Setup and Deployement
Setup Project
Create the shortcut of application file and drop into Desktop and Program Menu folders
Select the Setup Project from Solution Explorer and then select its properties
Select Pre-requisites
- 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
Add both the tables and Drag and drop the field for setting the relationship as one-to-
many relationship
1. Relations Master
2. Member Master
Step 1
Create a connection object using the information we have and open the connection
or
cn.Open();
Step 2
Step 3
30.07.2010
Import System.Data.OleDb
Create the SQL Statement. Use String.Format() method to create the SQL statement
Example
OleDbConnection cn = new OleDbConnection();
cn.ConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=F:\Batches2010\20\b20db.mdb";
cn.Open();
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 System.Data.OleDb;
using System.Windows.Forms;
using System.Configuration;
class Db
{
public static OleDbConnection GetConnection()
{
string provider =
ConfigurationSettings.AppSettings["provider"];
string dbname = ConfigurationSettings.AppSettings["dbname"];
cn.Open();
return cn;
}
}
02.08.2010
- 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
Integrated Security=true
To connect without login and password on local machine
Example
Data Source=.;Initial Catalog=b20;Integrated Security=True
Data Source=.;Initial Catalog=b20;User ID=sa; Password=pass
Provider : System.Data.SqlClient
Example
private void cmdSave_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=.;Initial
Catalog=b20;Integrated Security=True";
cn.Open();
string sql = "INSERT INTO product(pname,price,qty)
VALUES(@pname,@price,@qty)";
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@pname", txtPName.Text);
cmd.Parameters.AddWithValue("@price", txtPrice.Text);
cmd.Parameters.AddWithValue("@qty", txtQty.Text);
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Record Saved");
}
03.08.2010
Test Case
Create table as Student (rollno numeric, name varchar(50), photo Image)
Example
private void button2_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=.;Integrated
Security=true;Database=b20";
cn.Open();
09.08.2010
- 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";
SqlDataAdapter da=new SqlDataAdapter(sql,cn);
DataTable dt=new DataTable();
da.Fill(dt);
cn.Close();
- Each DataTable provides two collections
o Rows collection
o Columns collection
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
symptoms
spinstructions
Members *
mcode
mname
mobile
Medicines *
email
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
-