ButtonFrom.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace BurronModel
  15. {
  16. public partial class ButtonFrom : Form
  17. {
  18. public ButtonFrom()
  19. {
  20. InitializeComponent();
  21. }
  22. private void button1_Click(object sender, EventArgs e)
  23. {
  24. #region 动态添加模块
  25. //var strColor = dType.GetColor();
  26. List<string> strColor = new List<string>();
  27. strColor.Add("#e67817");
  28. strColor.Add("#449284");
  29. strColor.Add("#974478");
  30. strColor.Add("#996666");
  31. strColor.Add("#ffcccc");
  32. strColor.Add("#999999");
  33. strColor.Add("#669966");
  34. strColor.Add("#cccc99");
  35. strColor.Add("#669999");
  36. strColor.Add("#cc3366");
  37. var list = GetMess();
  38. if (list.Count > 0)
  39. {
  40. Button[] btn = new Button[list.Count];
  41. btn[0] = new Button();
  42. for (int i = 0; i < list.Count; i++)
  43. {
  44. btn[i] = new Button();
  45. btn[i].Size = new Size(240, 180); //按钮大小
  46. btn[i].Text = "医保ID:" + list[i].InterfaceID + "\r\n" +
  47. "医保名称:" + list[i].InterfaceName + "\r\n" +
  48. "医院ID:" + list[i].HospitalNO + "\r\n" +
  49. "医院名称:" + list[i].HospitalLevel; //设置按钮的text
  50. btn[i].ForeColor = Color.White;
  51. btn[i].Font = new Font("微软雅黑", 12, FontStyle.Bold);
  52. btn[i].BackColor = ColorTranslator.FromHtml(strColor[i]);
  53. btn[i].Name = list[i].HospitalLevel;
  54. if (i >= 1)
  55. {
  56. if (i % 2 == 0)
  57. {
  58. btn[i].Top = btn[i - 1].Top + btn[i - 1].Height + 10;
  59. btn[i].Left = btn[0].Left;
  60. }
  61. else
  62. {
  63. btn[i].Top = btn[i - 1].Top;
  64. btn[i].Left = btn[i - 1].Left + btn[i - 1].Width + 20;
  65. }
  66. }
  67. else
  68. {
  69. btn[i].Top = 14;
  70. btn[i].Left = 20;
  71. }
  72. btn[i].Visible = true;
  73. btn[i].Click += new EventHandler(ClickHandler);//点击按钮触发事件
  74. //this.Controls.Add(btn[i]);
  75. this.panel1.Controls.Add(btn[i]);
  76. }
  77. }
  78. #endregion
  79. }
  80. #region 动态的点击事件
  81. //ToolTip tt = null;
  82. public void ClickHandler(Object sender, System.EventArgs e)
  83. {
  84. string text = ((Button)sender).Text;
  85. var dd = ((Button)sender);
  86. MessageBox.Show("已单击按钮: " + ((Button)sender).Text, "提示窗体");
  87. }
  88. #endregion
  89. #region post
  90. public string Post(string Url, string jsonParas)
  91. {
  92. HttpWebResponse response;
  93. try
  94. {
  95. string strURL = Url;
  96. //创建一个HTTP请求
  97. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
  98. //Post请求方式
  99. request.Method = "POST";
  100. //内容类型
  101. request.ContentType = "application/json";
  102. //设置参数,并进行URL编码
  103. string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas);
  104. byte[] payload;
  105. //将Json字符串转化为字节
  106. payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
  107. //设置请求的ContentLength
  108. request.ContentLength = payload.Length;
  109. //发送请求,获得请求流
  110. Stream writer;
  111. try
  112. {
  113. writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
  114. }
  115. catch (Exception ex)
  116. {
  117. writer = null;
  118. return ("连接服务器失败!");
  119. }
  120. //将请求参数写入流
  121. writer.Write(payload, 0, payload.Length);
  122. writer.Close();//关闭请求流
  123. // String strValue = "";//strValue为http响应所返回的字符流
  124. //获得响应流
  125. response = (HttpWebResponse)request.GetResponse();
  126. }
  127. catch (WebException ex)
  128. {
  129. response = ex.Response as HttpWebResponse;
  130. }
  131. Stream s = response.GetResponseStream();
  132. // Stream postData = Request.InputStream;
  133. StreamReader sRead = new StreamReader(s);
  134. string postContent = sRead.ReadToEnd();
  135. sRead.Close();
  136. return postContent;//返回Json数据
  137. }
  138. public List<Hospital> GetMess()
  139. {
  140. List<Hospital> hospList = new List<Hospital>();
  141. string rtn = Post("http://10.1.7.29:8090/cloudinsu/sysMedInsu", tableToJson());
  142. if (rtn == "连接服务器失败!")
  143. {
  144. return hospList;
  145. }
  146. JObject jo = (JObject)JsonConvert.DeserializeObject(rtn);
  147. string errorCode = jo["errorCode"].ToString();
  148. string errorMessage = jo["errorMessage"].ToString();
  149. JArray data = (JArray)JsonConvert.DeserializeObject(jo["result"]["data"].ToString());
  150. if (errorCode == "0" && string.IsNullOrWhiteSpace(errorMessage))
  151. {
  152. StringBuilder str = new StringBuilder();
  153. foreach (var item in data)
  154. {
  155. JObject job = (JObject)item;
  156. Hospital hosp = new Hospital();
  157. hosp.InterfaceID = job["InterfaceID"].ToString().Trim();
  158. hosp.InterfaceName = job["InterfaceName"].ToString().Trim();
  159. hosp.HospitalNO = job["HospitalNO"].ToString().Trim();
  160. hosp.HospitalLevel = job["HospitalLevel"].ToString().Trim();
  161. hospList.Add(hosp);
  162. }
  163. }
  164. return hospList;
  165. }
  166. public string tableToJson()
  167. {
  168. String json = " { \"code\" :\"09010003\",\"params\" : [ ";
  169. json += "{\"HospitalDr\" : \"50\", ";
  170. json += " \"InterfaceID\" : \"\"}";
  171. json = json.Substring(0, json.Length - 1);
  172. json += "] } ";
  173. return json;
  174. }
  175. #endregion
  176. }
  177. }