This is not the document you are looking for? Use the search form below to find more!

Report home > Computer / Internet

Professional examples in C#.NET

1.00 (1 votes)
Document Description
Professional Sample Codes in C#.NET
File Details
Submitter
  • Name: Nazrul
Embed Code:

Add New Comment




Related Documents

Summer Training in Dot Net

by: duccsystems, 14 pages

DUCC Systems invites all to the summer training program in dot net. Batches start in June and July.

6 Months Project Based Training in Dot Net

by: duccsystems, 14 pages

Welcome to the 6 months training program in Dot Net in Delhi. It is a project based training program having placement opportunities.

Summer Training in C++

by: duccsystems, 7 pages

DUCC Systems invites all to C++ training in June and july.

UPTU MCA 1SEM CONCEPT PROGRAMMIN IN C 2010-2011

by: ASHISH, 4 pages

OLD PAPER 2010-2011

Dynamic Binding in C# 4.0

by: alina, 44 pages

Dynamic Binding in C# 4.0

An Easy Timer In C Language

by: alfredina, 2 pages

An Easy Timer In C Language

Celebrities Aren't Immune to Professional Negligence in Health Care!

by: pcrblog, 2 pages

Professional negligence in health care is a serious matter as many doctors, nurses, physicians, psychiatrists and other medical practitioners are alleged in a huge numbers annually due to their ...

File Handling in C++

by: aleksander, 27 pages

FILE HANDLING IN C++ Files (Streams) Files are used to store data in a relatively permanent form, on floppy disk, hard disk, tape or other form of secondary storage. ...

Using Examples In Oral Presentations

by: fadheela, 7 pages

Using Examples in Oral Presentations By Adan Avalos Noriega Overview • Planning your Speech • Purpose of using Examples • Finding & Connecting ...

Static and Dynamic polymorphism in C++

by: chan, 29 pages

Polymorphism in C++ 24th Aug 2007 Overview Polymorphism means “Many forms” OO Purists – “Only virtual methods” Liberal C++ ...

Content Preview
Example 1 - How to pass data from One form To Other
Step 1: Add a property in form1 to retrieve value from textbox.
public string _textBox1
{
get { return textBox1.Text; }
}
Step 2: Add a property in form2 to set the labels' text
public string _textBox
{
set { textBox1.Text = value; }
}
Step 3: In form1's button click event handler add the fol owing code.
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f._textBox = _textBox1;

f.Show();
this.Hide();

}
Further if you want than the data passed from one form is not changed or deleted on other form
then simply set the Read Only property of textBox1 on form 2 to true
Example 2 - Check whether form is open or not if it is open then bring it from task bar to screen
and avoid opening of multiple instance of same form
First add a private variable in Initialize Component section like this
public partial class Form1 : Form
{

private Form2 bm = null;
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (bm == null)
{
bm = new Form2();
bm.Closed += new EventHandler(Form2_Closed);
}
bm.WindowState = FormWindowState.Normal;
bm.Show();
}
private void Form2_Closed(object sender, EventArgs e)
{
bm = null;
}





Example 3 - Create Shortcuts after instal ation on Desktop
1. From Solution Explorer click your setup project.
2. Click File System Editor (on top of Solution Explorer).
3. When File System Editor has opened, on left tab you can see "File System on Target
Machine". Click Application Folder.
4. Then on the left tab the "Primary output from MyApp (Active)" appears. Right click it and select
"Create shortcut to Primary
output from MyApp (Active)". You should now see the shortcut appear below the primary
output, which is your application executable.
5. Drag & Drop that shortcut from right tab to left tabs File System on Target Machine - User's
Desktop folder.
6. Now the shortcut to your exe wil appear on the desktop after instal .
7. You can click the shortcut in File System Editor and edit the Name property from Properties
grid to change the text that appears on the icon.
Example 4 - Change the date time format on your choice
Add 3 Date Time Picker control on your form
private void Form1_Load(object sender, EventArgs e)
{

dateTimePicker1.Format = DateTimePickerFormat.Custom;
// Display the date as "Mon 26 Feb 2001".
dateTimePicker1.CustomFormat = "dd | MM | yyyy";
//Display the date as " 26 02 2001".
dateTimePicker2.CustomFormat = " dd MM yyyy";
dateTimePicker3.CustomFormat = "'Today is:' hh:mm:ss dddd
MMMM dd, yyyy";
}

The fol owing code displays the date as it is entered in database irrespective of format
dateTimePicker1.CustomFormat = dr["dob"].ToString();
Example 5 - Change the error provider messeges
private void textBox2_Validating(object sender, CancelEventArgs e)
{
try

{
int x = Int32.Parse(textBox2.Text);
errorProvider1.SetError(textBox2, "");
}
catch (Exception ex)
{
errorProvider1.SetError(textBox2, "Not an integer
value.");
}
}
Example 6 - Add a Clock to your Windows Form
1. First add a label to your Form1
2. Add A timer control and set its Enabled property to true from properties window
3. Double click the timer control added below the form
4. Add the fol owing code to it
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToLongTimeString();
}
Another way
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.Second.ToString() ;
}
private void button1_Click(object sender, EventArgs e)
{
if ( button1.Text == "Stop" )
{
button1.Text = "Start";
timer1.Enabled = false;
}
else
{
button1.Text = "Stop";
timer1.Enabled = true;
}
}
private void InitializeTimer()
{
// Set to 1 second.
timer1.Interval = 1000;
timer1.Enabled = true;
button1.Text = "Stop";
}

myTimer.Elapsed +=
new System.Timers.ElapsedEventHandler(myTimer_Elapsed);

private void myTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
System.Windows.Forms.MessageBox.Show("Elapsed!",
"Timer Event Raised!");
}
private void CreateTimer()
{
System.Timers.Timer Timer1 = new System.Timers.Timer();
Timer1.Enabled = true;
Timer1.Interval = 5000;
Timer1.Elapsed +=
new System.Timers.ElapsedEventHandler(Timer1_Elapsed);
}
private void Timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
System.Windows.Forms.MessageBox.Show("Elapsed!",
"Timer Event Raised!");
}
Example 7 - Ask before clearing a Text Box and Picture Box
Add a picture box and textbox to Form then add fol owing code to click event of a button1
private void button1_Click(object sender, EventArgs e)
{
if (MessageBox.Show("DO U WANT TO CLEAR", "",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ textBox1.Clear();
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
}
}

Example 8 - Add a Progress Bar to your Windows Form
Add a progress bar to your form and add fol owing code on double click of button
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 1000; i++)
{
progressBar1.PerformStep();

}

}

Example 9 - Hiding Menu Items and Labels at Runtime
private void Form1_Load(object sender, EventArgs e)
{
label2.Hide(); label3.Hide();
toolStripMenuItem5.Enabled = false;
toolStripMenuItem3.Enabled = false;

}
Example 10 - Opening File Open dialog and setting the File Filters Title of Dialog
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = true; dlg.Title = "Select File";
dlg.Filter = "JPEG (*.jpg;*.jpeg;*.jpe;*.jfif)|
*.jpg;*.jpeg;*.jpe;*.jfif|All Files|*.*";
try
{
if (dlg.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(dlg.FileName);
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message.ToString()); }
}
Example 11- Cal ing Third Party or Microsofts .exe in Windows Forms
private void mSOfficeToolStripMenuItem_Click(object sender, EventArgs e)
{
try { System.Diagnostics.Process.Start("winword.exe"); }
catch (Exception ex) {
MessageBox.Show(ex.Message.ToString()); }
}
private void backupRestoreToolStripMenuItem_Click(object sender,
EventArgs e)
{
try { System.Diagnostics.Process.Start("C:\\Program
Files\\MySQL\\MySQL Tools for 5.0\\MySQLAdministrator.exe"); }
catch (Exception ex) {
MessageBox.Show(ex.Message.ToString()); }
}

Example12-Displaying SUM(), COUNT(), AVG(), DATABASE(),VERSION(), doirectly from
database in Text Box along with the Product name Product version
private void FormMenu_Load(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "ddd ddMMMMyyyy";
MySqlConnection conn = new MySqlConnection();
MySqlCommand cmd = new MySqlCommand();
string constr =
"server=127.0.0.1;uid=root;pwd=root;database=LIBRARY;";
conn = new MySqlConnection(constr);
try
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "select version() ";
object a = cmd.ExecuteScalar();
conn.Close();
string c = " MySQL ";
textBox1.Text = ProductName.ToString() +
ProductVersion.ToString() + '\n' + c + a.ToString();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show("Error " + ex.Number + " has
occurred: " + ex.Message,
"Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
Example 13 - Displaying Sliding Effects on Forms
For making sliding form effect we need to work with Win32 API functions so to call
API's Functions.
So first of all import
using System.Runtime.InteropServices;
here we are calling WIN32 API function so we need to declare some constant values
that will be used by function when calling it
so declare all the constants in global area of the class.
//Constants
const int AW_SLIDE = 0X40000;
const int AW_HOR_POSITIVE = 0X1;
const int AW_HOR_NEGATIVE = 0X2;
const int AW_BLEND = 0X80000;


[DllImport("user32")]
static extern bool AnimateWindow(IntPtr hwnd, int time,
int flags);
Write code in overrides OnLoad Event of the form which you want to Slide

protected override void OnLoad(EventArgs e)
{
//Load the Form At Position of Main Form
int
WidthOfMain=Application.OpenForms["MainForm"].Width;
int HeightofMain=
Application.OpenForms["MainForm"].Height;
int
LocationMainX=Application.OpenForms["MainForm"].Location.X ;
int
locationMainy=Application.OpenForms["MainForm"].Location.Y;

//Set the Location
this.Location = new
Point(LocationMainX+WidthOfMain,locationMainy+10);

//Animate form
AnimateWindow(this.Handle, 500, AW_SLIDE |
AW_HOR_POSITIVE);
}
here in code we need to set the Sliding Window Location according to the Main
window so we need to Code Some extra lines so that
sliding form can slide from proper place.
and Finally I am showing you how whole code will look alike I have Two Forms
MainForm from which i press Slide Button and Another Form which will slide ie Form1
MainForm Code
-------------
namespace SlidingEffect
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
bool IsOpen = false;

FormCollection fc = Application.OpenForms;
foreach (Form f in fc)
{
if (f.Name == "Form1")
{
IsOpen = true;
f.Focus();
break;
}
}

if (IsOpen == false)
{
Form1 form = new Form1();
form.Show();
}
}

}
}
Form1 Code (form Which will Slide From MainForm)
----------------------------------------------------

public partial class Form1 : Form
{
//Constants
const int AW_SLIDE = 0X40000;
const int AW_HOR_POSITIVE = 0X1;
const int AW_HOR_NEGATIVE = 0X2;
const int AW_BLEND = 0X80000;

[DllImport("user32")]
static extern bool AnimateWindow(IntPtr hwnd, int time,
int flags);

public Form1()
{
InitializeComponent();
}

protected override void OnLoad(EventArgs e)
{
//Load the Form At Position of Main Form
int
WidthOfMain=Application.OpenForms["MainForm"].Width;
int HeightofMain=
Application.OpenForms["MainForm"].Height;
int
LocationMainX=Application.OpenForms["MainForm"].Location.X ;
int
locationMainy=Application.OpenForms["MainForm"].Location.Y;


//Set the Location
this.Location = new
Point(LocationMainX+WidthOfMain,locationMainy+10);

//Animate form
AnimateWindow(this.Handle, 500, AW_SLIDE |
AW_HOR_POSITIVE);
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
string WorkingDirectory = Application.StartupPath + "\\";
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
WorkingDirectory + "icard.mdb;Persist Security Info=True";
crystalReportViewer1.ReportSource = Application.StartupPath + "\\" +
"CrystalLibraryReport2.rpt";
crystalReportViewer1.ReportSource = (@".\CrystalLibraryReport2.rpt");
Example -14 Play sounds when Form Loads
using System.Media;
private void playSimpleSound()
{
SoundPlayer simpleSound = new SoundPlayer(@".\Speech
On.wav");
simpleSound.Play();//@"c:\Windows\Media\Speech On.wav"
}
public void playExclamation()
{
SystemSounds.Exclamation.Play();
}
private void Form1_Load(object sender, EventArgs e)
{
playSimpleSound();playExclamation();
}

Get Operating Systems Name in Java
class GetPropertyDemo

{
public static void main(String args[])
{
// Display value for machine type and OS
System.out.println ( System.getProperty("os.arch") +
" running " +
System.getProperty("os.name") );
}
}
Example 15 - Inserting jpeg images in MS Acces Database from Picture Box
string WorkingDirectory = Application.StartupPath + "\\";
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
WorkingDirectory + "icard.mdb;Persist Security Info=True";
OleDbConnection con = new OleDbConnection();
con.ConnectionString = constr;
con.Open();
OleDbCommand cmd = new OleDbCommand();
string inst = "insert into
stud(rollbatchno,name,fname,dob,course,duration,address,issuedate,valid
upto,photo,contact) values('" + textBox5.Text + "','" + textBox1.Text +
"','" + textBox2.Text + "','" + dateTimePicker1.Text + "','" +
comboBox1.Text + "','" + textBox4.Text + "','" + textBox3.Text + "','"
+ dateTimePicker2.Text + "','" + dateTimePicker3.Text +
"',@BLOBData,'"+textBox6.Text+"')";
cmd.CommandText = inst;
cmd.Connection = con;
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
//Read from MemoryStream into Byte array.
Byte[] bytBLOBData = new Byte[ms.Length];
ms.Position = 0;
ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length));
//Create parameter for insert statement that contains image.
OleDbParameter prm = new OleDbParameter("@BLOBData",
OleDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input,
false, 0, 0, null, DataRowVersion.Current, bytBLOBData);
cmd.Parameters.Add(prm);
cmd.ExecuteNonQuery();
try
{
playSound();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message.ToString()); }
MessageBox.Show("Data Entry OK", "I-CARD", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
con.Close();
pictureBox1.Image.Dispose(); pictureBox1.Image = null;
label17.ResetText();

Download
Professional examples in C#.NET

 

 

Your download will begin in a moment.
If it doesn't, click here to try again.

Share Professional examples in C#.NET to:

Insert your wordpress URL:

example:

http://myblog.wordpress.com/
or
http://myblog.com/

Share Professional examples in C#.NET as:

From:

To:

Share Professional examples in C#.NET.

Enter two words as shown below. If you cannot read the words, click the refresh icon.

loading

Share Professional examples in C#.NET as:

Copy html code above and paste to your web page.

loading