friend.cs
上传用户:yan_wy
上传日期:2007-06-09
资源大小:112k
文件大小:7k
源码类别:

ICQ/即时通讯

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. namespace qq
  8. {
  9. /// <summary>
  10. /// friend 的摘要说明。
  11. /// </summary>
  12. public class friend : System.Windows.Forms.Form
  13. {
  14. Form1 form;
  15. private System.Windows.Forms.Button button1;
  16. private System.Windows.Forms.Button button2;
  17. private System.Windows.Forms.DataGrid dataGrid1;
  18. /// <summary>
  19. /// 必需的设计器变量。
  20. /// </summary>
  21. private System.ComponentModel.Container components = null;
  22. public friend(Form1 f)
  23. {
  24. //
  25. // Windows 窗体设计器支持所必需的
  26. //
  27. form=f;
  28. InitializeComponent();
  29. //
  30. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  31. //
  32. }
  33. /// <summary>
  34. /// 清理所有正在使用的资源。
  35. /// </summary>
  36. protected override void Dispose( bool disposing )
  37. {
  38. if( disposing )
  39. {
  40. if(components != null)
  41. {
  42. components.Dispose();
  43. }
  44. }
  45. base.Dispose( disposing );
  46. }
  47. #region Windows 窗体设计器生成的代码
  48. /// <summary>
  49. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  50. /// 此方法的内容。
  51. /// </summary>
  52. private void InitializeComponent()
  53. {
  54. this.button1 = new System.Windows.Forms.Button();
  55. this.button2 = new System.Windows.Forms.Button();
  56. this.dataGrid1 = new System.Windows.Forms.DataGrid();
  57. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
  58. this.SuspendLayout();
  59. // 
  60. // button1
  61. // 
  62. this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  63. this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  64. this.button1.Location = new System.Drawing.Point(128, 240);
  65. this.button1.Name = "button1";
  66. this.button1.Size = new System.Drawing.Size(72, 32);
  67. this.button1.TabIndex = 2;
  68. this.button1.Text = "加为好友";
  69. this.button1.Click += new System.EventHandler(this.button1_Click);
  70. // 
  71. // button2
  72. // 
  73. this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
  74. this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
  75. this.button2.Location = new System.Drawing.Point(24, 240);
  76. this.button2.Name = "button2";
  77. this.button2.Size = new System.Drawing.Size(72, 32);
  78. this.button2.TabIndex = 3;
  79. this.button2.Text = "取消";
  80. this.button2.Click += new System.EventHandler(this.button2_Click);
  81. // 
  82. // dataGrid1
  83. // 
  84. this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  85. | System.Windows.Forms.AnchorStyles.Left) 
  86. | System.Windows.Forms.AnchorStyles.Right)));
  87. this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  88. this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.ActiveBorder;
  89. this.dataGrid1.DataMember = "";
  90. this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  91. this.dataGrid1.Location = new System.Drawing.Point(16, 32);
  92. this.dataGrid1.Name = "dataGrid1";
  93. this.dataGrid1.SelectionBackColor = System.Drawing.Color.Red;
  94. this.dataGrid1.SelectionForeColor = System.Drawing.Color.Black;
  95. this.dataGrid1.Size = new System.Drawing.Size(208, 184);
  96. this.dataGrid1.TabIndex = 4;
  97. this.dataGrid1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseDown);
  98. // 
  99. // friend
  100. // 
  101. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  102. this.ClientSize = new System.Drawing.Size(240, 301);
  103. this.Controls.Add(this.dataGrid1);
  104. this.Controls.Add(this.button2);
  105. this.Controls.Add(this.button1);
  106. this.Name = "friend";
  107. this.Text = "friend";
  108. ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
  109. this.ResumeLayout(false);
  110. }
  111. #endregion
  112. public void startset(string a)
  113. {
  114. try
  115. {
  116. this.form.sdata.dataset.Tables.Clear();
  117. System.Data.SqlClient.SqlCommand sss=new System.Data.SqlClient.SqlCommand();
  118. if(a=="")
  119.                      sss.CommandText="SELECT id,name,sex,age from [user]";
  120. else
  121. sss.CommandText="SELECT id,name,sex,age from [user] where "+a;
  122. sss.Connection=this.form.sdata.sqlConnection1;
  123. this.form.sdata.sqlDataAdapter1.SelectCommand=sss;
  124. this.form.sdata.sqlDataAdapter1.Fill(this.form.sdata.dataset,"you");
  125. if(this.form.sdata.dataset.Tables["you"].Rows.Count!=0)
  126. {
  127. this.dataGrid1.SetDataBinding(this.form.sdata.dataset,"you");
  128. }
  129. }
  130. catch(Exception ee)
  131. {
  132. MessageBox.Show(ee.ToString());
  133. }
  134. }
  135. private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  136. {
  137. }
  138. private void button1_Click(object sender, System.EventArgs e)
  139. {
  140. int i=0;
  141. foreach(DataRow dt in this.form.sdata.dataset.Tables["you"].Rows)
  142. {
  143. if(i==this.dataGrid1.CurrentRowIndex)
  144. {
  145. try
  146. {
  147. int j=0;
  148. foreach(UserControl1 dt1 in form.userlist)
  149. {
  150. if(dt1.uid==dt[0].ToString())
  151. {
  152. MessageBox.Show("你已经加了这个用户");
  153. j=1;
  154. break;
  155. }
  156. }
  157. if(j==0)
  158. {
  159. System.Data.SqlClient.SqlCommand insert=new System.Data.SqlClient.SqlCommand();
  160. insert.CommandText = "INSERT INTO friends( uid, friendid) VALUES ("+form.userid+","+dt[0].ToString()+")";
  161. insert.Connection=this.form.sdata.sqlConnection1;
  162. this.form.sdata.sqlConnection1.Open();
  163. insert.ExecuteNonQuery();
  164. this.form.sdata.sqlConnection1.Close();
  165. System.Data.SqlClient.SqlCommand select=new System.Data.SqlClient.SqlCommand();
  166. select.CommandText= "SELECT [user].id,[user].name,[user].sex,[user].picture,[user].age,[user].jianjie,[user].state FROM [user]  where id="+dt[0].ToString();
  167. select.Connection=this.form.sdata.sqlConnection1;
  168. this.form.sdata.sqlDataAdapter1.SelectCommand=select;
  169. this.form.sdata.dataset.Tables.Add("aa");
  170. this.form.sdata.sqlDataAdapter1.Fill(this.form.sdata.dataset,"aa");
  171. foreach(DataRow d in this.form.sdata.dataset.Tables["aa"].Rows)
  172. {
  173. UserControl1 userControl13=new UserControl1(form);
  174. userControl13.Dock=DockStyle.Top;
  175. userControl13.uid=d[0].ToString();
  176. userControl13.name=d[1].ToString();
  177. userControl13.sex=d[2].ToString();
  178. userControl13.picture=d[3].ToString();
  179. userControl13.age=d[4].ToString();
  180. userControl13.jianjie=d[5].ToString();
  181. userControl13.state=d[6].ToString();
  182. userControl13.setname(d[1].ToString());
  183. userControl13.setimage((d[3].ToString()).Trim());
  184. form.userlist.Add(userControl13);
  185. form.panel4.Controls.Add(userControl13);
  186. }
  187. this.form.sdata.dataset.Tables.Remove("aa");
  188. break;
  189. }
  190. }
  191. catch(Exception ee)
  192. {
  193. MessageBox.Show(ee.ToString());
  194. }
  195. }
  196. i++;
  197. }
  198. }
  199. private void button2_Click(object sender, System.EventArgs e)
  200. {
  201. this.Close();
  202. }
  203. }
  204. }