C#入门2

WinForm开发:
十五、一个简单的WinForm程序:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


public class SimpleForm : System.Windows.Forms.Form
{

    private System.ComponentModel.Container components = null;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.TextBox textBox1;
    public SimpleForm()
    {
        InitializeComponent();
    }

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

    #region Windows Form Designer generated code
    private void InitializeComponent()
    {

        this.components = new System.ComponentModel.Container();
        this.Size = new System.Drawing.Size(300,300);
        this.Text = Form1;

        this.button1 = new System.Windows.Forms.Button();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout(); 
    //
    // button1
    //

    this.button1.Location = new System.Drawing.Point(8, 16);
    this.button1.Name = button1;
    this.button1.Size = new System.Drawing.Size(80, 24);
    this.button1.TabIndex = 0;
    this.button1.Text = button1;

    //
    // textBox1
    //
    this.textBox1.Location = new System.Drawing.Point(112, 16);
    this.textBox1.Name = textBox1;
    this.textBox1.Size = new System.Drawing.Size(160, 20);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = textBox1;
    //
    // Form1
    //

    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
    this.textBox1,
    this.button1});
    this.Name = Form1;
    this.Text = Form1;
    this.ResumeLayout(false); 

    }
    #endregion

    [STAThread]
    static void Main()
    {
        Application.Run(new SimpleForm());
    } 
}
十六、运行时显示自己定义的图标:
//load icon and set to form
System.Drawing.Icon ico = new System.Drawing.Icon(@c:\temp\app.ico);
this.Icon = ico;
十七、添加组件到ListBox中:
private void Form1_Load(object sender, System.EventArgs e)
{
    string str = First item;
    int i = 23;
    float flt = 34.98f; 
    listBox1.Items.Add(str);
    listBox1.Items.Add(i.ToString());
    listBox1.Items.Add(flt.ToString());
    listBox1.Items.Add(Last Item in the List Box);

网络方面的:
十八、取得IP地址:
using System;
using System.Net;

class GetIP
{
     public static void Main()
     {
         IPHostEntry ipEntry = Dns.GetHostByName (localhost);
         IPAddress [] IpAddr = ipEntry.AddressList;
         for (int i = 0; i < IpAddr.Length; i++)
         { 
             Console.WriteLine (IP Address {0}: {1} , i, IpAddr.ToString ());
         }
    }
}
十九、取得机器名称:
using System;
using System.Net;

class GetIP
{
    public static void Main()
    {
          Console.WriteLine (Host name : {0}, Dns.GetHostName());
    }
}
二十、发送邮件:
using System;
using System.Web;
using System.Web.Mail;

public class TestSendMail
{
    public static void Main()
    {
        try
        {
            // Construct a new mail message 
            MailMessage message = new MailMessage();
            message.From = 
from@domain.com;
            message.To   =  
pengyun@cobainsoft.com;
            message.Cc   = ;
            message.Bcc  = ;
            message.Subject = Subject;
            message.Body = Content of message;
            
            //if you want attach file with this mail, add the line below
            message.Attachments.Add(new MailAttachment(c:\\attach.txt, MailEncoding.Base64));
  
            // Send the message
            SmtpMail.Send(message);  
            System.Console.WriteLine(Message has been sent);
        }

        catch(Exception ex)
        {
            System.Console.WriteLine(ex.Message.ToString());
        }

    }
}
二十一、根据IP地址得出机器名称:
using System;
using System.Net;

class ResolveIP
{
     public static void Main()
     {
         IPHostEntry ipEntry = Dns.Resolve(172.29.9.9);
         Console.WriteLine (Host name : {0}, ipEntry.HostName);         
     }
}

GDI+方面的:
二十二、GDI+入门介绍:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

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

    public Form1()
    {
        InitializeComponent();
    }

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

    #region Windows Form Designer generated code
    private void InitializeComponent()
    {
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Name = Form1;
        this.Text = Form1;
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    #endregion

    [STAThread]
    static void Main() 
    {
        Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        Graphics g=e.Graphics;
        g.DrawLine(new Pen(Color.Blue),10,10,210,110);
        g.DrawRectangle(new Pen(Color.Red),10,10,200,100);
        g.DrawEllipse(new Pen(Color.Yellow),10,150,200,100);
    }
}

XML方面的:
二十三、读取XML文件:
using System;
using System.Xml;  

class TestReadXML
{
    public static void Main()
    {
        
        XmlTextReader reader  = new XmlTextReader(C:\\test.xml);
        reader.Read();
        
        while (reader.Read())
        {            
            reader.MoveToElement();
            Console.WriteLine(XmlTextReader Properties Test);
            Console.WriteLine(===================);  

            // Read this properties of element and display them on console
            Console.WriteLine(Name: + reader.Name);
            Console.WriteLine(Base URI: + reader.BaseURI);
            Console.WriteLine(Local Name: + reader.LocalName);
            Console.WriteLine(Attribute Count: + reader.AttributeCount.ToString());
            Console.WriteLine(Depth: + reader.Depth.ToString());
            Console.WriteLine(Line Number: + reader.LineNumber.ToString());
            Console.WriteLine(Node Type: + reader.NodeType.ToString());
            Console.WriteLine(Attribute Count: + reader.Value.ToString());
        }        
    }               

二十四、写XML文件:
using System; 
using System.Xml; 

public class TestWriteXMLFile 

    public static int Main(string[] args) 
    { 
        try 
        {  
            // Creates an XML file is not exist 
            XmlTextWriter writer = new XmlTextWriter(C:\\temp\\xmltest.xml, null); 
            // Starts a new document 
            writer.WriteStartDocument(); 
            //Write comments 
            writer.WriteComment(Commentss: XmlWriter Test Program); 
            writer.WriteProcessingInstruction(Instruction,Person Record); 
            // Add elements to the file 
            writer.WriteStartElement(p, person, urn:person); 
            writer.WriteStartElement(LastName,); 
            writer.WriteString(Chand); 
            writer.WriteEndElement(); 
            writer.WriteStartElement(FirstName,); 
            writer.WriteString(Mahesh); 
            writer.WriteEndElement(); 
            writer.WriteElementInt16(age,, 25); 
            // Ends the document 
            writer.WriteEndDocument(); 
        } 
        catch (Exception e) 
        {  
            Console.WriteLine (Exception: {0}, e.ToString()); 
        } 
        return 0; 
    } 

Web Service方面的:
二十五、一个Web Service的小例子:
<% @WebService Language=C# Class=TestWS %>

using System.Web.Services;

public class TestWS : System.Web.Services.WebService
{
    [WebMethod()]
    public string StringFromWebService()
    {
        return This is a string from web service.;
    }

 

posted on 2011-10-18 09:29  叮铛猫  阅读(309)  评论(0)    收藏  举报

导航