Showing posts with label Tutorial. Show all posts
Showing posts with label Tutorial. Show all posts

Wednesday, September 29, 2010

Button click Action to move ball tutorial example

using System;
using System.Drawing;
using System.Windows.Forms;

public class ButtonToMove : Form {
private int x = 50, y = 50;
private Button move = new Button();

public ButtonToMove() {
move.Text = "Move";
move.Location = new Point(5,5);
Controls.Add(move);
move.Click += new EventHandler(Move_Click);
}
protected void Move_Click(object sender, EventArgs e) {
x += 9;
y += 9;
Invalidate();
}
protected override void OnPaint( PaintEventArgs e ) {
Graphics g = e.Graphics;
Brush red = new SolidBrush(Color.Red);
g.FillEllipse(red ,x ,y, 20 ,20);
base.OnPaint(e);
}
public static void Main( ) {
Application.Run(new ButtonToMove());
}
}

Sunday, January 11, 2009

Dock Top and Bottom tutorial example

using System;
using System.Drawing;
using System.Windows.Forms;

public class ControlDock : Form
{
public ControlDock()
{
Size = new Size(350,400);

int yButtonSize = Font.Height * 2;

Button btn = new Button();
btn.Parent = this;
btn.Text = "First Button";
btn.Height = yButtonSize;
btn.Dock = DockStyle.Top;

btn = new Button();
btn.Parent = this;
btn.Text = "Second Button";
btn.Height = yButtonSize;
btn.Dock = DockStyle.Bottom;
}

static void Main()
{
Application.Run(new ControlDock());
}
}

Use Inherited form in a separate Main class tutorial example

using System;
using System.Drawing;
using System.Windows.Forms;

class SeparateMain
{
public static void Main()
{
Application.Run(new AnotherHelloWorld());
}
}
class AnotherHelloWorld: Form
{
public AnotherHelloWorld()
{
Text = "Another Hello World";
BackColor = Color.White;
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics graphics = pea.Graphics;

graphics.DrawString("Hello, Windows Forms!", Font,
Brushes.Black, 0, 0);
}
}

IP address of your computer or any website tutorial example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



using System.Net;



namespace IPAddressApplication

{

class Program

{

static void Main(string[] args)

{

//Get IP info about this computer

//

Console.WriteLine("Local host:");

Console.WriteLine();

string Name = Dns.GetHostName();

Console.WriteLine("\tHost Name: " + Name);

Line();



DisplayHostInfo(Name);

Line();



//Get IP info of an internet address

//

DisplayHostInfo("www.amazon.com");

Line();



Console.ReadKey();

}



static void Line()

{

Console.WriteLine("\t============================================");

}



static void DisplayHostInfo(string Host)

{

IPHostEntry hostStuff;



hostStuff = Dns.GetHostEntry(Host);



// Start displaying info

// DNS name of the host

Console.WriteLine("\tPrimary host name: " + hostStuff.HostName);



// Alias names associated with the host

Console.Write("\tAliases: ");

foreach (string alias in hostStuff.Aliases)

{

Console.WriteLine("\t\t" + alias);

}

Console.WriteLine();



// List of IP addresses associated with the host

// May be IPv4 or IPv6 forms

Console.WriteLine("\tIP Addresses: ");

foreach (IPAddress ip in hostStuff.AddressList)

{

Console.WriteLine("\t\t" + ip.ToString());

}

Console.WriteLine();



}

}

}

Set Form Visible properties tutorial example

using System.Windows.Forms;

class RunFormBadly
{
public static void Main()
{
Form form = new Form();

form.Text = "Not a Good Idea...";
form.Visible = true;

Application.Run();
}
}

Mouse Event information tutorial example

using System;
using System.Drawing;
using System.Windows.Forms;

public class MainClass{
static void Main()
{
Console.WriteLine("DoubleClickSize"+ SystemInformation.DoubleClickSize.ToString());
Console.WriteLine("DoubleClickTime"+SystemInformation.DoubleClickTime.ToString());
Console.WriteLine("MouseButtons"+SystemInformation.MouseButtons.ToString());
Console.WriteLine("MouseButtonsSwapped"+SystemInformation.MouseButtonsSwapped.ToString());
Console.WriteLine("MousePresent"+SystemInformation.MousePresent.ToString());
Console.WriteLine("MouseWheelPresent"+SystemInformation.MouseWheelPresent.ToString());
Console.WriteLine("MouseWheelScrollLines"+SystemInformation.MouseWheelScrollLines.ToString());
Console.WriteLine("NativeMouseWheelSupport"+SystemInformation.NativeMouseWheelSupport.ToString());
}

}

Animate Image with Image Animator in C# tutorial example

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

public class Form1 : Form
{
private Bitmap bmp;

public Form1()
{

}

private void Form1_Load(object sender, EventArgs e)
{
bmp = new Bitmap("arrow.gif");
ImageAnimator.Animate(bmp, new EventHandler(this.OnFrameChanged));
}

private void OnFrameChanged(object o, EventArgs e)
{
this.Invalidate();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
ImageAnimator.UpdateFrames();
e.Graphics.DrawImage(this.bmp, new Point(0, 0));
}
}

Change Form size in menu action tutorial example


using System;
using System.Windows.Forms;

class FormChangeSize : Form {
MainMenu MyMenu;

public FormChangeSize() {
Text = "Adding a Main Menu";
MyMenu = new MainMenu();

MenuItem m1 = new MenuItem("File");
MyMenu.MenuItems.Add(m1);

MenuItem m2 = new MenuItem("Tools");
MyMenu.MenuItems.Add(m2);

MenuItem subm1 = new MenuItem("Open");
m1.MenuItems.Add(subm1);

MenuItem subm2 = new MenuItem("Close");
m1.MenuItems.Add(subm2);

MenuItem subm3 = new MenuItem("Exit");
m1.MenuItems.Add(subm3);

MenuItem subm4 = new MenuItem("Coordinates");
m2.MenuItems.Add(subm4);

MenuItem subm5 = new MenuItem("Change Size");
m2.MenuItems.Add(subm5);

MenuItem subm6 = new MenuItem("Restore");
m2.MenuItems.Add(subm6);


subm4.Click += MMCoordClick;
subm5.Click += MMChangeClick;
subm6.Click += MMRestoreClick;

Menu = MyMenu;
}

[STAThread]
public static void Main() {
FormChangeSize skel = new FormChangeSize();

Application.Run(skel);
}

protected void MMCoordClick(object who, EventArgs e) {
Console.WriteLine("Top:"+Top);
Console.WriteLine("Left:"+Left);
Console.WriteLine("Bottom:"+Bottom);
Console.WriteLine("Right:"+Right);

}

protected void MMChangeClick(object who, EventArgs e) {
Width = Height = 200;
}

protected void MMRestoreClick(object who, EventArgs e) {
Width = Height = 300;
}

}

Change Form Cursor tutorial example

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class FormCursorSetting : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public FormCursorSetting()
{
InitializeComponent();
Cursor = Cursors.WaitCursor;
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Form1";
}
[STAThread]
static void Main()
{
Application.Run(new FormCursorSetting());
}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("String...", new Font("Times New Roman", 20), new SolidBrush(Color.Black), 40, 10);
}
}

Button click Action to move ball tutorial example

using System;
using System.Drawing;
using System.Windows.Forms;

public class ButtonToMove : Form {
private int x = 50, y = 50;
private Button move = new Button();

public ButtonToMove() {
move.Text = "Move";
move.Location = new Point(5,5);
Controls.Add(move);
move.Click += new EventHandler(Move_Click);
}
protected void Move_Click(object sender, EventArgs e) {
x += 9;
y += 9;
Invalidate();
}
protected override void OnPaint( PaintEventArgs e ) {
Graphics g = e.Graphics;
Brush red = new SolidBrush(Color.Red);
g.FillEllipse(red ,x ,y, 20 ,20);
base.OnPaint(e);
}
public static void Main( ) {
Application.Run(new ButtonToMove());
}
}

c# control tutorial example

using System;
using System.Drawing;
using System.Windows.Forms;

public class MainForm : Form
{
public static void Main()
{
MainForm MyForm = new MainForm();

Application.Run(MyForm);
}

public MainForm()
{
Button MyButton = new Button();

Text = "Button Test";
MyButton.Location = new Point(25, 25);
MyButton.Text = "Click Me";
MyButton.Click += new EventHandler(MyButtonClicked);
Controls.Add(MyButton);
}

public void MyButtonClicked(object sender, EventArgs Arguments)
{
MessageBox.Show("The button has been clicked.");
}
}

using Inserting Data Using SQLStatements tutorial example

using System;
using System.Data;
using System.Data.SqlClient;

class MainClass
{
static void Main(string[] args)
{
SqlConnection MyConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

MyConnection.Open();

String MyString = @"INSERT INTO Employee(ID, FirstName, LastName) VALUES(2, 'G', 'M')";
SqlCommand MyCmd = new SqlCommand(MyString, MyConnection);

MyCmd.ExecuteScalar();
MyConnection.Close();
}
}

file Save text from database to text file tutorial example

/*
Quote from


Beginning C# 2005 Databases From Novice to Professional

# Paperback: 528 pages
# Publisher: Apress (December 18, 2006)
# Language: English
# ISBN-10: 159059777X
# ISBN-13: 978-1590597774
*/
using System;
using System.Data;
using System.Data.SqlClient;

class RetrieveText
{
static string textFile = null;
static char[] textChars = null;
static SqlConnection conn = null;
static SqlCommand cmd = null;
static SqlDataReader dr = null;

public RetrieveText()
{
conn = new SqlConnection(@"data source = .\sqlexpress;integrated security = true;initial catalog = tempdb;");

// Create command
cmd = new SqlCommand(@"select textfile,textdata from texttable", conn);

// Open connection
conn.Open();

// Create data reader
dr = cmd.ExecuteReader();
}

public static bool GetRow()
{
long textSize;
int bufferSize = 100;
long charsRead;
textChars = new Char[bufferSize];

if (dr.Read())
{
// Get file name
textFile = dr.GetString(0);
Console.WriteLine("------ start of file:");
Console.WriteLine(textFile);
textSize = dr.GetChars(1, 0, null, 0, 0);
Console.WriteLine("--- size of text: {0} characters -----",
textSize);
Console.WriteLine("--- first 100 characters in text -----");
charsRead = dr.GetChars(1, 0, textChars, 0, 100);
Console.WriteLine(new String(textChars));
Console.WriteLine("--- last 100 characters in text -----");
charsRead = dr.GetChars(1, textSize - 100, textChars, 0, 100);
Console.WriteLine(new String(textChars));

return true;
}
else
{
return false;
}
}

public static void endRetrieval()
{
// Close the reader and the connection.
dr.Close();
conn.Close();
}

static void Main()
{
try
{

while (GetRow() == true)
{
Console.WriteLine("----- end of file:");
Console.WriteLine(textFile);
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
endRetrieval();
}
}
}

updating Sql tutorial example

using System;
using System.Data;
using System.Data.SqlClient;

class MainClass
{
static void Main(string[] args)
{
SqlConnection MyConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

MyConnection.Open();

String MyString = "UPDATE Employee SET FirstName = 'Lee'";
SqlCommand MyCmd = new SqlCommand(MyString, MyConnection);

MyCmd.ExecuteScalar();
MyConnection.Close();
}
}

file open ,Load Text file to Database tutorial example

/*
Quote from


Beginning C# 2005 Databases From Novice to Professional

# Paperback: 528 pages
# Publisher: Apress (December 18, 2006)
# Language: English
# ISBN-10: 159059777X
# ISBN-13: 978-1590597774
*/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;

class LoadText
{
static string fileName =@"loadtext.cs";

static SqlConnection conn = null;
static SqlCommand cmd = null;

static void Main()
{
try
{
GetTextFile(fileName);

conn = new SqlConnection(@"data source = .\sqlexpress;integrated security = true;initial catalog = tempdb;");
conn.Open();
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = @"create table texttable(textfile varchar(255),textdata varchar(max))";
cmd.ExecuteNonQuery();

PrepareInsertTextFile();
ExecuteInsertTextFile(fileName);
Console.WriteLine("Loaded {0} into texttable.", fileName);
}
catch (SqlException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close();
}
}

static void ExecuteCommand(string commandText)
{
cmd.CommandText = commandText;
cmd.ExecuteNonQuery();

}
static void PrepareInsertTextFile()
{
cmd.CommandText = @"insert into texttable values (@textfile, @textdata)";
cmd.Parameters.Add("@textfile", SqlDbType.NVarChar, 30);
cmd.Parameters.Add("@textdata", SqlDbType.Text, 1000000);
}

static void ExecuteInsertTextFile(string textFile)
{
string textData = GetTextFile(textFile);
cmd.Parameters["@textfile"].Value = textFile;
cmd.Parameters["@textdata"].Value = textData;
ExecuteCommand(cmd.CommandText);
}

static string GetTextFile(string textFile)
{
string textBytes = null;
Console.WriteLine("Loading File: " + textFile);

FileStream fs = new FileStream(textFile, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
textBytes = sr.ReadToEnd();

Console.WriteLine("TextBytes has length {0} bytes.",textBytes.Length);

return textBytes;
}
}

Execute nonquery to delete a record ROW tutorial example

using System;
using System.Data;
using System.Data.SqlClient;

class MainClass
{
static void Main()
{
SqlConnection conn = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

string sqlqry = @"select count(*) from employee ";

string sqlins = @"insert into employee (firstname,lastname)values('Z', 'Z')";

string sqldel = @"delete from employee where firstname = 'Z' and lastname = 'Z'";

SqlCommand cmdqry = new SqlCommand(sqlqry, conn);
SqlCommand cmdnon = new SqlCommand(sqlins, conn);

try
{
conn.Open();

Console.WriteLine("Before INSERT: Number of employees {0}\n", cmdqry.ExecuteScalar());

Console.WriteLine("Executing statement {0}", cmdnon.CommandText);
cmdnon.ExecuteNonQuery();
Console.WriteLine("After INSERT: Number of employees {0}\n", cmdqry.ExecuteScalar());

cmdnon.CommandText = sqldel;
Console.WriteLine("Executing statement {0}", cmdnon.CommandText);
cmdnon.ExecuteNonQuery();
Console.WriteLine("After DELETE: Number of employees {0}\n", cmdqry.ExecuteScalar());
}
catch (SqlException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
conn.Close();
Console.WriteLine("Connection Closed.");
}
}
}

Delete data using sql statements tutorial example

using System;
using System.Data;
using System.Data.SqlClient;

class MainClass
{
static void Main(string[] args)
{
SqlConnection MyConnection = new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

MyConnection.Open();

String MyString = "DELETE Employee";
SqlCommand MyCmd = new SqlCommand(MyString, MyConnection);

MyCmd.ExecuteScalar();
MyConnection.Close();
}
}

Call StoredProcedure with input and output parameters tutorial example

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

class MainClass {
static void Main() {
string cstr = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
using ( SqlConnection conn = new SqlConnection( cstr ) )
{
conn.Open();

SqlCommand cmd = new SqlCommand( "QueryVendor", conn );
cmd.CommandType = CommandType.StoredProcedure;

// input parm
SqlParameter name = cmd.Parameters.Add( "@name", SqlDbType.NVarChar, 15 );
name.Value = "Tom";

// output parm
SqlParameter vendor = cmd.Parameters.Add( "@vendor", SqlDbType.NVarChar, 15 );
vendor.Direction = ParameterDirection.Output;

// return value
SqlParameter rowCount = cmd.Parameters.Add( "@rowCount", SqlDbType.Int );
rowCount.Direction = ParameterDirection.ReturnValue;

cmd.ExecuteNonQuery();

if ( (int)rowCount.Value > 0 )
{
Console.WriteLine(" is available from " + vendor.Value );
}
else
{
Console.WriteLine(" not available from " + vendor.Value );
}
}

}
}

Call storedprocedure and pass in the parameter tutorial example

using System;
using System.Data;
using System.Data.SqlClient;

class MainClass
{
public static void Main()
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = @"Data Source = .\sqlexpress;Database = Northwind; Integrated Security=SSPI";
con.Open();

string category = "Seafood";
string year = "1999";

// Create and configure a new command.
using (SqlCommand com = con.CreateCommand())
{
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "SalesByCategory";

// Create a SqlParameter object for the category parameter.
com.Parameters.Add("@CategoryName", SqlDbType.NVarChar).Value =
category;

// Create a SqlParameter object for the year parameter.
com.Parameters.Add("@OrdYear", SqlDbType.NVarChar).Value = year;

// Execute the command and process the results.
using (IDataReader reader = com.ExecuteReader())
{
Console.WriteLine("Sales By Category ({0}).", year);

while (reader.Read())
{
// Display the product details.
Console.WriteLine(" {0} = {1}",
reader["ProductName"],
reader["TotalPurchase"]);
}
}
}

}
}
}

Call a stored procedure tutorial example

using System;
using System.Data;
using System.Data.SqlClient;

class MainClass
{
static void Main(string[] args)
{
SqlConnection cn = new SqlConnection("Data Source=(local); Initial Catalog = MyDatabase; User ID=sa;Password=");
SqlCommand cmd = new SqlCommand("MyStoredProcedure", cn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("@ReturnValue", SqlDbType.Int);
cmd.Parameters.Add(param);
cmd.Parameters.Add("MyFirstParameter", SqlDbType.Int);
cmd.Parameters.Add("MySecondParameter", SqlDbType.Int).Direction =
ParameterDirection.Output;
SqlDataAdapter da = new SqlDataAdapter(cmd);

}
}

Followers

Get our toolbar!

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | JCPenney Coupons