首页 新闻 会员 周边 捐助

winform中怎么将按钮事件封装成一个cs文件

0
悬赏园豆:10 [已关闭问题] 关闭于 2013-03-07 09:33

例子(也是我要完成的任务,不需要完全告诉我,提示一下即可):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Xml;
using System.Web;
using System.Net;
using System.IO;
using System.Security.Cryptography;

namespace SmsDemo_2008
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void sendSmsBtn_Click(object sender, EventArgs e)
        {
            if (true == check())
            {
                //添加参数
                NameValueCollection parameters = new NameValueCollection();
                parameters.Add("LoginName", smsLoginName.Text);
                parameters.Add("Password", GetMd5Str(Password.Text));
                parameters.Add("SmsKind", SmsKind.Text);
                parameters.Add("SendSim", SendSim.Text);
                parameters.Add("ExpSmsId", "" + ExpSmsId.Text);
                parameters.Add("MsgContext", MsgContext.Text);

                SendMessage sm = new SendMessage(smsLoginName.Text, GetMd5Str(Password.Text), SmsKind.Text, SendSim.Text, ExpSmsId.Text, MsgContext.Text);


                if (true == wsRadio.Checked)
                {
                    sendSmsByWs(parameters);
                }
                else
                {
                    sendSmsByHttp(parameters);
                }
            }
        }

        //调用Webservice接口
        public void sendSmsByWs(NameValueCollection parameters)
        {
            try
            {
                SmsWebservice.SmsServiceSoapClient ws = new SmsWebservice.SmsServiceSoapClient("SmsServiceSoap");
                XmlNode nodes = ws.SendEx(parameters["LoginName"],
                    parameters["Password"],
                    parameters["SmsKind"],
                    parameters["SendSim"],
                    parameters["ExpSmsId"],
                    parameters["MsgContext"]
                );

                addMsg("调用WebService接口发送短信成功:\n" + nodes.OuterXml);
                MessageBox.Show("发送成功");
            }
            catch (Exception ex)
            {
                string error = "调用WebService接口发送短信失败:\n" + ex.Message + "\n";
                addMsg(error);
                MessageBox.Show("发送失败");
            }
        }

        //调用http接口,接口编码为GB2312
        public void sendSmsByHttp(NameValueCollection parameters)
        {

            System.Text.Encoding GB2312 = System.Text.Encoding.GetEncoding("GB2312");
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://61.190.35.51/SmsService/SmsService.asmx/SendEx");
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                //设置参数,并进行URL编码
                StringBuilder codedString = new StringBuilder();
                foreach (string key in parameters.Keys)
                {
                    // codedString.Append(HttpUtility.UrlEncode(key));
                    codedString.Append(key);
                    codedString.Append("=");
                    codedString.Append(HttpUtility.UrlEncode(parameters[key], GB2312));
                    codedString.Append("&");
                }
                string paraUrlCoded = codedString.Length == 0 ? string.Empty : codedString.ToString().Substring(0, codedString.Length - 1);
                //将URL编码后的字符串转化为字节
                byte[] payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
                request.ContentLength = payload.Length;
                Stream writer = request.GetRequestStream();
                writer.Write(payload, 0, payload.Length);
                writer.Close();
                //获得响应流
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream s = response.GetResponseStream();
                //转化为XML
                StreamReader readStream = new StreamReader(s, Encoding.UTF8);
                string strValue = readStream.ReadToEnd();
                addMsg("调用http接口发送短信成功:\n" + strValue);
                MessageBox.Show("发送成功");
            }
            catch (Exception ex)
            {
                string error = "调用http接口发送短信失败:\n" + ex.Message + "\n";
                addMsg(error);
                MessageBox.Show("发送失败");
            }
        }

        //md5加密 16位
        public static string GetMd5Str(string ConvertString)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
            t2 = t2.Replace("-", "");
            t2 = t2.ToLower();
            return t2;
        }


        //验证表单
        public Boolean check()
        {
            if (smsLoginName.Text.Equals(""))
            {
                MessageBox.Show("请输入登录名");
                return false;
            }
            if (Password.Text.Equals(""))
            {
                MessageBox.Show("请输入密码");
                return false;
            }
            if (SmsKind.Text.Equals(""))
            {
                MessageBox.Show("请输入网关类型");
                return false;
            }
            if (SendSim.Text.Equals(""))
            {
                MessageBox.Show("请输入接收号码");
                return false;
            }
            if (MsgContext.Text.Equals(""))
            {
                MessageBox.Show("请输入短信内容");
                return false;
            }
            return true;
        }

        public void addMsg(string str)
        {
            msg.Text = msg.Text + "\n" + str;
        }

        private void btn_GetMessage_Click(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            //this.Hide();
            form.Show();

           
        }
    }
}

百品的主页 百品 | 菜鸟二级 | 园豆:253
提问于:2013-03-04 17:55
< >
分享
所有回答(2)
0

新建一个CS文件,然后里面写一个方法,把事件里的代码放进去,然后从事件里调用这个方法.

不知道我说的你能看明白不.?

上次好像你就问过.

只会造轮子 | 园豆:2274 (老鸟四级) | 2013-03-04 18:06

嗯,但是按钮的方法,比如: if (true == wsRadio.Checked)
                {
                    sendSmsByWs(parameters);
                }
                else
                {
                    sendSmsByHttp(parameters);
                }

 

这个地方。下面调用另一个方法。条件中还设计到按钮的方法- -这里怎么搞

支持(0) 反对(0) 百品 | 园豆:253 (菜鸟二级) | 2013-03-04 18:21

@一定要记住论坛的名字不要起的太长否则别人会讨厌你的: 给方法添加参数,把需要的值传进去

支持(0) 反对(0) 只会造轮子 | 园豆:2274 (老鸟四级) | 2013-03-04 18:39
0

把你上述代码,在按钮点击事件{}里面的部分,直接用VS自带的提取方法(右击,提取方法)提取成一个方法,然后放到随便一个类里就行了。

林J | 园豆:202 (菜鸟二级) | 2013-03-05 09:58

非常感谢你能帮我

但是这个放到类里之后,在那个类里面需要定义字段和属性的。这些怎么在那些方法中找出来

主要是那个按钮名称的属性怎么再次定义成属性

比如:

 if (true == wsRadio.Checked)
                {
                    sendSmsByWs(parameters);
                }
                else
                {
                    sendSmsByHttp(parameters);
                }

这个地方。

wsRadio.Checked这个怎么弄(winform)

支持(0) 反对(0) 百品 | 园豆:253 (菜鸟二级) | 2013-03-05 10:49

@一定要记住论坛的名字不要起的太长否则别人会讨厌你的: 

 if (true == wsRadio.Checked)
                {
                    sendSmsByWs(parameters);
                }
                else
                {
                    sendSmsByHttp(parameters);
                }

这一部分都提取出来成方法,然后看参数,估计是一个Radioa按钮,接下去你调用的时候把这个按钮传进来就行了。

支持(0) 反对(0) 林J | 园豆:202 (菜鸟二级) | 2013-03-05 10:52

@林J: 这个按钮要作为一个属性传进来怎么穿呢?这个按钮如何set到一个属性中?

支持(0) 反对(0) 百品 | 园豆:253 (菜鸟二级) | 2013-03-05 19:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册