如何在变量值更改时调用方法

时间:2017-02-14 09:10:43

标签: c# web-services

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Web;
 using System.Net;


 namespace CS_Ex1
 {
  public partial class Form1 : Form
  {
   private string tb1, tb2;
  public Form1()
  {
     InitializeComponent();
  }

  private void Form1_Load(object sender, EventArgs e)
  {

  }

  private void Connect_Click(object sender, EventArgs e)
  {
      WSMBT.Result Result;
      wsmbtControl1.Mode = WSMBT.Mode.TCP_IP;
      wsmbtControl1.ResponseTimeout = 1000;
      Result = wsmbtControl1.Connect("127.0.0.1", 502);
      if (Result != WSMBT.Result.SUCCESS)
          MessageBox.Show(wsmbtControl1.GetLastErrorString());
  }

  private void Disconnect_Click_1(object sender, EventArgs e)
  {
      wsmbtControl1.Close();
  }

  private void button1_Click(object sender, EventArgs e)
  {
      Int16[] Registers = new Int16[20];
      WSMBT.Result Result;
      Result = wsmbtControl1.ReadHoldingRegisters(1, 0, 20, Registers);
      if (Result == WSMBT.Result.SUCCESS)
      {
          string DataString = "";
          string str = "";

          for (int i = 0; i < 20; i++)
          {
              str = string.Format("{0:D}", Registers[0]);
              str = string.Format("{0:D}", Registers[1]);

              DataString = DataString + str + "\r\n";
          }
          TextBox1.Text = Registers[0].ToString();
          TextBox2.Text = Registers[1].ToString();


           tb1 = TextBox1.Text.ToString();
          tb2 = TextBox2.Text.ToString();
          }
      else
      {
          MessageBox.Show(wsmbtControl1.GetLastErrorString());
      }

  }

  private void Write_Click(object sender, EventArgs e)
  {
      Int16[] Registers = new Int16[20];
      WSMBT.Result Result;
      for (Int16 i = 0; i < 20; i++)
          Registers[i] = i;
      Result = wsmbtControl1.WriteMultipleRegisters(1, 0, 20, Registers);
      if (Result != WSMBT.Result.SUCCESS)
      {
          MessageBox.Show(wsmbtControl1.GetLastErrorString());
      }
  }

  public static void message_Click()
  {
      string senderusername = "XXX";
      string senderpassword = "XXXX";
      string senderid = "22687";
      string sURL;
      StreamReader objReader;
      sURL = "URL";
      WebRequest wrGETURL;
      wrGETURL = WebRequest.Create(sURL);
      try
      {
          Stream objStream;
          objStream = wrGETURL.GetResponse().GetResponseStream();
          objReader = new StreamReader(objStream);
          objReader.Close();
      }
      catch (Exception ex)
      {
          ex.ToString();
      }
  }
  }


}

在这个代码VARIABLE中tb1一直在变化我想调用message_click()方法,当tb1值变化或假设它的值为10然后tb1值变为68然后它应该发送到用户手机调用message_click()并发送短信

1 个答案:

答案 0 :(得分:2)

您可以使用属性。在setter中,您可以在更改变量后调用所需的方法:

public partial class Form1 : Form
{
    private tb2;
    private string _tb1;

    public string tb1
    {
        get { return _tb1; }
        private set
        {
            _tb1 = value;
            message_click();
        }
    }

    public Form1()
    {
       InitializeComponent();
    } 

请注意,尽管属性为public,但您可以将setter设为私有,以便不能在此类之外更改变量。您也可以使用protected

protected string tb1
{ ...