2014年2月19日 星期三

[C#/winform] 主Form / 副 Form 如何製作+連結2個Form

在VS2012裡面按 專案-> 加入windows form

你設定的檔名ex : form2

回到主Form,在要顯示的地方加入

Form2 frm2(這名稱可以改) =  new Form2();

frm2.showdialog(this); //show 副form

this.show(); //回到主form

即可。

[C#/Error] 不一致的存取範圍: 欄位型別 ...... 比欄位 ..... 存取範圍低

通常初學者寫Class不會特別宣告型別

但如果你宣告

public static Class名稱;
public Form
{
    ....
    ....
}
Class 名稱
{
    ......
}

這樣就有問題了!! 因為基本設定 Class沒加任何型別 = internal(內部可用)


Public > internal  所以 造成 "不一致的存取範圍: 欄位型別 ...... 比欄位 ..... 存取範圍低"的Error!

public static Class名稱;
public Form
{
    ....
    ....
}
public Class 名稱
{
    ......
}

這樣才對

End


[C#] List的基本用法

在寫程式常常會遇到要動態陣列,C#也很貼心的做了一個功能----泛型List<T>

但我想主要不是用在這邊....沒關係,還是講解一下List的用法

一開始一定就是

List<任意型態> 名稱 = new List<跟前面一樣的任意型態>();   //new給它一個空間

然後就可以在迴圈裡面丟給它值(它可以任意變動長度)

丟值的方法為: 名稱.Add(值);

如果要從中取出值就跟陣列一樣

名稱[0] (陣列位置可以改,這邊只是範例)

如果要長度 :名稱.Count 就會跟平常的.Length一樣囉!

範例:

List<byte> modbus = new List<byte>();

for (int i = 0; i < 30; i++)
{
       if (Modbus_save[i] == true) //這邊只是設一個條件而已
      {
            modbus.Add(Convert.ToByte(i)); //把i轉byte型態,存入List<T>
       }
}

End

2014年2月18日 星期二

[C#/winform] Form與Form之間 傳值

有許多方法可以傳值,包括Properties/Class/ini檔..等

這邊講兩個

1.Properties

使用時機: 需要儲存使用者設定時。

使用方法: 先到專案-> xxx屬性->設定,設定幾個需要使用的變數(型態要調好)

然後Form2內將變數代入Properties.Settings.Default.(設的變數名稱),

接著再去Form1內將Properties.Settings.Default.(設的變數名稱) 帶回form1(設一個新的變數去接)

記得要儲存使用者設定要加入Properties.Settings.Default.Save();


2.Class

使用時機: 滿廣泛的,但關掉無法儲存

使用方法: Form2內設一個public 變數

ex:

Public int a {get; set;}

public Form2{

....
}

回到Form1以後就可以用了,記得要寫在ShowDialog();的下方

Form2 frm2 = new Form2();

frm2.ShowDialog(this);

(寫在這邊)

當然要接值的話,型別也要對就是了。

End

2014年2月14日 星期五

[C#/winform] smtp傳送電子郵件(email)

using System.Net.Mail;
using System.Net;

   class Email
    {
        public string From {get;set; }
        public string To { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
        public string Account { get; set; }
        public string Password { get; set; }
        public string Server { get; set; }
        public string Port { get; set; }
       
        public void Send_email()
        {
           //這邊先設定From,To,Subject,Body,Account,Password,Server,Port

            SmtpClient theSmtpClient = new SmtpClient(Server);
            NetworkCredential networkcredential = new NetworkCredential(Account, Password);//帳密
            theSmtpClient.UseDefaultCredentials = false;
            theSmtpClient.Credentials = networkcredential;
            int Int_port = Convert.ToInt32(Port);
            if (Int_port == 587)
            {
                theSmtpClient.EnableSsl = true;
            }
            else//不是587 就是 25
            {
                theSmtpClient.EnableSsl = false;
            }
            theSmtpClient.Port = Int_port;

            try
            {
                theSmtpClient.Send(From, To, Subject, Body);
            }
            catch (Exception)
            {

            }
        }
    }

十分簡單,不過要注意的地方就是Port在有SSL加密認證的環境下要用587(ex:gmail),沒有的話則用25即可,然後Using要加入net / net.mail

End