using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace BurronModel { public partial class ButtonFrom : Form { public ButtonFrom() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { #region 动态添加模块 //var strColor = dType.GetColor(); List strColor = new List(); strColor.Add("#e67817"); strColor.Add("#449284"); strColor.Add("#974478"); strColor.Add("#996666"); strColor.Add("#ffcccc"); strColor.Add("#999999"); strColor.Add("#669966"); strColor.Add("#cccc99"); strColor.Add("#669999"); strColor.Add("#cc3366"); var list = GetMess(); if (list.Count > 0) { Button[] btn = new Button[list.Count]; btn[0] = new Button(); for (int i = 0; i < list.Count; i++) { btn[i] = new Button(); btn[i].Size = new Size(240, 180); //按钮大小 btn[i].Text = "医保ID:" + list[i].InterfaceID + "\r\n" + "医保名称:" + list[i].InterfaceName + "\r\n" + "医院ID:" + list[i].HospitalNO + "\r\n" + "医院名称:" + list[i].HospitalLevel; //设置按钮的text btn[i].ForeColor = Color.White; btn[i].Font = new Font("微软雅黑", 12, FontStyle.Bold); btn[i].BackColor = ColorTranslator.FromHtml(strColor[i]); btn[i].Name = list[i].HospitalLevel; if (i >= 1) { if (i % 2 == 0) { btn[i].Top = btn[i - 1].Top + btn[i - 1].Height + 10; btn[i].Left = btn[0].Left; } else { btn[i].Top = btn[i - 1].Top; btn[i].Left = btn[i - 1].Left + btn[i - 1].Width + 20; } } else { btn[i].Top = 14; btn[i].Left = 20; } btn[i].Visible = true; btn[i].Click += new EventHandler(ClickHandler);//点击按钮触发事件 //this.Controls.Add(btn[i]); this.panel1.Controls.Add(btn[i]); } } #endregion } #region 动态的点击事件 //ToolTip tt = null; public void ClickHandler(Object sender, System.EventArgs e) { string text = ((Button)sender).Text; var dd = ((Button)sender); MessageBox.Show("已单击按钮: " + ((Button)sender).Text, "提示窗体"); } #endregion #region post public string Post(string Url, string jsonParas) { HttpWebResponse response; try { string strURL = Url; //创建一个HTTP请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL); //Post请求方式 request.Method = "POST"; //内容类型 request.ContentType = "application/json"; //设置参数,并进行URL编码 string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas); byte[] payload; //将Json字符串转化为字节 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); //设置请求的ContentLength request.ContentLength = payload.Length; //发送请求,获得请求流 Stream writer; try { writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象 } catch (Exception ex) { writer = null; return ("连接服务器失败!"); } //将请求参数写入流 writer.Write(payload, 0, payload.Length); writer.Close();//关闭请求流 // String strValue = "";//strValue为http响应所返回的字符流 //获得响应流 response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = ex.Response as HttpWebResponse; } Stream s = response.GetResponseStream(); // Stream postData = Request.InputStream; StreamReader sRead = new StreamReader(s); string postContent = sRead.ReadToEnd(); sRead.Close(); return postContent;//返回Json数据 } public List GetMess() { List hospList = new List(); string rtn = Post("http://10.1.7.29:8090/cloudinsu/sysMedInsu", tableToJson()); if (rtn == "连接服务器失败!") { return hospList; } JObject jo = (JObject)JsonConvert.DeserializeObject(rtn); string errorCode = jo["errorCode"].ToString(); string errorMessage = jo["errorMessage"].ToString(); JArray data = (JArray)JsonConvert.DeserializeObject(jo["result"]["data"].ToString()); if (errorCode == "0" && string.IsNullOrWhiteSpace(errorMessage)) { StringBuilder str = new StringBuilder(); foreach (var item in data) { JObject job = (JObject)item; Hospital hosp = new Hospital(); hosp.InterfaceID = job["InterfaceID"].ToString().Trim(); hosp.InterfaceName = job["InterfaceName"].ToString().Trim(); hosp.HospitalNO = job["HospitalNO"].ToString().Trim(); hosp.HospitalLevel = job["HospitalLevel"].ToString().Trim(); hospList.Add(hosp); } } return hospList; } public string tableToJson() { String json = " { \"code\" :\"09010003\",\"params\" : [ "; json += "{\"HospitalDr\" : \"50\", "; json += " \"InterfaceID\" : \"\"}"; json = json.Substring(0, json.Length - 1); json += "] } "; return json; } #endregion } }