謝謝kuolung大及ilybydlm大
昨日有重新研究一下去try按鑑button1後先出現圖二訊息後
在跳出連線狀況
連線後,數據其是呈現靜態,非動態。
嘗試在上網查詢相關資料修正,還是有相同訊息出來。。。。。
此部份還需要請兩位大方便的話,
能否多跟你們請益呢。
寫法如下:
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using NDde.Client;
- namespace TradeDDE
- {
- public partial class TDControl : Form
- {
- //private DdeClient client;
- private Hashtable ht_conn;
- private Hashtable ht_connitem;
- //private Hashtable ht_itemvalue;
- public string data = null;
- public TDControl()
- {
- InitializeComponent();
- ht_conn = new Hashtable();
- ht_connitem = new Hashtable();
- // ht_itemvalue = new Hashtable();
- //client = new DdeClient( "","" );
- }
- private void TDControl_Load(object sender, EventArgs e)
- {
- }
- /**新增 DDE連線的事件處理**/
- private void button1_Click(object sender, EventArgs e)
- {
-
- DdeClient client = new DdeClient("MMSDDE", "FUSA");
- if (ht_conn.ContainsKey(client))
- {
- MessageBox.Show("已連線不能重複登入!");
- return;
- }
- client.Disconnected += client_Disconnected;
- client.Advise += client_Advise;
- try
- {
- client.Connect();
- label1.Text = "已連線";
- string key = "MMSDDE"+"|"+"FUSA";
- ht_conn.Add(key, client);
- }
- catch(Exception thrown)
- {
- MessageBox.Show("無法連結DDE伺服器:" + thrown.Message);
- }
-
- DdeClient dc = (DdeClient)ht_connitem[client];
- string item = "B1YM&.125";
- this.AddItem(dc, item);
- string abc;
- abc = client.Request(item, 10000);
- client.StartAdvise(item, 1, true, 10000);
- client.Advise += client_Advise;
- decimal lab;
- lab = Convert.ToDecimal(abc);
- lab = decimal.Parse(abc);
- label2.Text = abc;
- int abcd = dataGridView1.Rows.Add(abc);
- }
- private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
- if (e.RowIndex < 0 ||(e.ColumnIndex != dataGridView1.Columns["Col_item"].Index && e.ColumnIndex != dataGridView1.Columns["Col_del"].Index))
- return;
- DataGridViewRow del_row = dataGridView1.Rows[e.RowIndex];
-
- DdeClient client = new DdeClient("MMSDDE", "FUSA");
- DdeClient dc = (DdeClient)ht_connitem[client];
-
- string stop_item = del_row.Cells["Col_item"].Value.ToString();
-
- this.Stop_advise(dc,stop_item);
- string service_topic_item = client + "!" + stop_item;
- DataGridViewRow del_item_row = (DataGridViewRow)ht_connitem[service_topic_item];
-
- ht_connitem.Remove(service_topic_item);
- dataGridView1.Rows.Remove(del_item_row);
- dataGridView1 = null;
- return;
- }
- //Stop Advise
- private void Stop_advise(DdeClient dc, string item)
- {
- try
- {
- dc.StopAdvise(item, 10000);
- }
- catch (Exception thrown)
- {
- MessageBox.Show("停止更新: \n" + thrown.Message);
- }
- }
-
- private void Dictionary(string Label)
- {
- Label L = new Label();
-
- }
- private void client_Advise(object sender, DdeAdviseEventArgs args)
- {
- //Console.WriteLine("OnAdvise:" + args.Text);
- DdeClient dc = (DdeClient)sender;
- string key = dc.Service + "|" + dc.Topic + "!" + args.Item;
- //Item it = new Item();
- //it.item = args.Item; it.value = "";
- //Hashtable ht_connitem = new Hashtable();
-
- DataGridViewRow chg_value_row = (DataGridViewRow)ht_connitem[key];
- chg_value_row = null;
- return ;
- int codepage = Encoding.Default.CodePage;
- string data = Encoding.GetEncoding(codepage).GetString(args.Data, 0, args.Data.Length);
-
- chg_value_row.Cells["Col_item"].Value = data;
- label2.Text = data;
- }
- private void AddItem(DdeClient dc, string item)
- {
- try
- {
- string key = dc.Service + "|" + dc.Topic + "!" + item;
- Item it = new Item();
- it.item = item; it.value = "";
- try
- {
- byte[] data = dc.Request(item, 1, 10000);
- int codepage = Encoding.Default.CodePage;
- string value = Encoding.GetEncoding(codepage).GetString(data, 0, data.Length);
- it.value = value;
- }
- catch (Exception)
- {
- ;
- }
- dc.StartAdvise(item, 1, true, 10000);
- int idx = dataGridView1.Rows.Add(it.value);
- ht_conn.Add(key, dataGridView1.Rows[idx]);
- label2.Text = it.value;
- }
- catch (Exception thrown)
- {
- MessageBox.Show("無法新增項目:\n" + thrown.Message);
- }
- }
- private void Remove_Connect_and_Item_Row(int row_idx)
- {
- DataGridViewRow del_row = dataGridView1.Rows[row_idx];
- string key = del_row.Cells["Col_item"].Value.ToString();
- DdeClient client_conn = (DdeClient)ht_connitem[key];
- ArrayList list = new ArrayList();
- foreach (DictionaryEntry del in ht_connitem)
- {
- string item_key = del.Key.ToString();
- string item_key_substr = item_key.Substring(0, item_key.IndexOf("!"));
- if(key.Equals(item_key_substr))
- {
- DataGridViewRow del_item_row = (DataGridViewRow)del.Value;
- string item = del_item_row.Cells["Col_item"].Value.ToString();
- this.Stop_advise(client_conn, item);
- list.Add(item_key);
- }
- }
- foreach(string str_key in list)
- {
- DataGridViewRow del_item_row = (DataGridViewRow)ht_connitem[str_key];
- ht_connitem.Remove(str_key);
- dataGridView1.Rows.Remove(del_item_row);
- }
- ht_connitem.Remove(key);
- client_conn.Dispose();
- dataGridView1.Rows.RemoveAt(row_idx);
- }
- public void Connect(string service, string topic)
- {
- service = "MMSDDE";
- topic = "FUSA";
-
- DdeClient client = new DdeClient(service, topic);
- client.Disconnected += client_Disconnected;
- client.Advise += client_Advise;
- try
- {
-
- client.Connect();
- this.startAdvise(client);
- }
- catch (Exception thrown)
- {
- throw new Exception("無法連結DDE SERVER \n" + thrown.Message);
- }
- }
- private void startAdvise(DdeClient client)
- {
- //Advise Loop
- client.StartAdvise("B1YM&.125", 1, true, 60000);
- client.Advise += client_Advise;
- }
-
-
-
-
- private void client_Disconnected(object sender, DdeDisconnectedEventArgs args)
- {
- string msg =
- "OnDisconnected: " +
- "IsServerInitiated=" + args.IsServerInitiated.ToString() + " " +
- "IsDisposed=" + args.IsDisposed.ToString();
- //可實做將該訊息顯示在 StatusBar 上.
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
-
- }
- }
複製代碼
|