Showing posts with label C#. Show all posts
Showing posts with label C#. 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());
}
}

Followers

Get our toolbar!

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