2015年10月15日 星期四

接線圖



C# winForm --- Aform 開起其他程式[ Bform.exe ]並傳送參數;

Aform 開起其他程式[ Bform.exe ]並傳送參數;

1. 被開啟的程式[ Bform.exe ]
    Program.cs  內容須修改為可接受參數;
    static class Program
    {
        /// <summary>
        /// 應用程式的主要進入點。
        /// </summary>
        static System.Threading.Mutex _mutex;
        [STAThread]
        static void Main(string[] args)
        {
            //是否可以打開新進程
            bool createNew;
            Attribute guid_attr = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute));
            string guid = ((GuidAttribute)guid_attr).Value;
            _mutex = new System.Threading.Mutex(true, guid, out createNew);
            if (false == createNew)
            {
                //發現重複進程
            }
            _mutex.ReleaseMutex();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (args.Length == 0)
            {
                Application.Run(new Bform());
            }
            else
            {
                Application.Run(new Bform(args[0].ToString()));
            }
        }
    }
/////////////////////////////////
 
   Bform程式需新增

        public Bform(string P) ///定義字串參數P;
        {
            InitializeComponent();
            if (P != "")     ///判斷有沒有參數;
            {
                label1.Text = P;
            }
            else
            {
                label1.Text = "";
            }
        }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2. 執行端程式 [ Aform.exe ] 在按鍵中加入;
       private void button_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Bform.exe", "ABC");
        }

         ////System.Diagnostics.Process.Start("程式.exe", "參數");


範例檔案







C# winForm 儲存文字檔[ .txt ]的格式定義為[ Unicode ]

FileStream fs = new FileStream(@"C:\test.txt", System.IO.FileMode.OpenOrCreate);

StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Unicode);


sw.Write("メールマガジン「.NETプログラミング研究」では");
sw.Flush();
sw.Close();
fs.Close();

C# winForm 子母視窗互傳參數的方式!!

Form1 開啟 Form2 並傳參數

1. 先在Form2 新增參數名稱及接收的方式
   ex:
   string _type;
   public void _Type 
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
            }
        }

2. 在Form1 寫開啟 Form2 及傳參數

private void btn_Click(object sender, EventArgs e)  
        {
            Form2 fm = new Form2();
            fm._Type = "0";
            //fm._Table = masterDGV.CurrentRow.Cells[0].Value.ToString();
            //fm._Shop = _shopid;
            //fm._Date = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");

            fm.ShowDialog();
        }

///////////////////////////////////////////////////////////////////////////////////////////

Form2 子視窗關閉,將參數回傳 主視窗 Form1
1. 先在Form1 開啟子視窗時定義 Owner = this;
    以及接收的參數及方式!
   string _strValue;
   public void _StrValue 
        {
            get
            {
                return _strValue;
            }
            set
            {
                _strValue = value;
            }
        }


private void btnShow_Click(object sender, EventArgs e)  
     {
            Form2 fm = new Form2();
            fm.Owner = this;
            //fm._Table = masterDGV.CurrentRow.Cells[0].Value.ToString();
            //fm._Shop = _shopid;
            //fm._Date = DateTime.Now.ToString("yyyyMMdd HH:mm:ss");

            fm.ShowDialog();
            TextBox1.text=_strValue;  //關閉表單後返回參數填入TextBox1;
     }


2. 在Form2 寫關閉視窗時回傳的方法

private void btnClose_Click(object sender, EventArgs e)  
    {
      Form1 F1 = (Form1)this.Owner;
      F1.StrValue = "Form2返回";;
      this.Close();

    }