Part 5: design and code of the chat interface
I. Interface design
1 .:
2. Interface Design:
(1) a friend's profile picture and "XX is chatting with XX" are displayed in the upper left corner ".
(2) a ListView is used in the chat window in the middle, and the View uses Details to set a column to display the main item, only the main item is used (the Avatar and the information and message content to be displayed are loaded), and the subitem information is not set.
(3) TextBox is used to write messages.
(4) A sent Button.
(5) Use the Dock layout on the interface.
(6) put the user's ico icon in imageList1, imageList2 put the user's avatar, and the clock is timer1 to query and display the chat message content.
3. Names of the controls used:
Chat content display window chatwindow, write message text box senderword, send button btnsender
II. Code design:
Private string _ Friendname; public string Friendname {get {return _ Friendname;} set {_ Friendname = value ;}} private string _ Mename; public string Mename {get {return _ Mename;} set {_ Mename = value ;}} public Chat () {InitializeComponent ();} // The constructor loads public Chat (string friendname, string mename): this () {_ Friendname = friendname; _ Mename = mename;} // loads private void Chat_Load (object sender, eventArgs e) {// load the ico and display text UsersTableAdapter adp = new UsersTableAdapter (); qqdata in the upper left corner. usersDataTable table = adp. getDataByUsername (_ Friendname); Bitmap a = (Bitmap) imageList1.Images [table [0]. photoCode-1]; this. icon = System. drawing. icon. fromHandle (. getHicon (); this. text = getmemoname (_ Mename) + "chatting with" + getmemoname (_ Friendname) + "";} // method for obtaining nickname based on user name private string getmemoname (string name) {UsersTableAdapter adp1 = new UsersTableAdapter (); qqdata. usersDataTable tab = new qqdata. usersDataTable (); tab = adp1.GetDataByUsername (name); return tab [0]. memo;} // press the send key private void btnsender_Click (object sender, EventArgs e) {string nr = senderword. text; DateTime sj = DateTime. now; // upload the chat content sent by yourself to the database new ChatTableAdapter (). insertChat (_ Mename, _ Friendname, nr, sj, 0); senderword. text = ""; // display the content you sent directly, and add the display (unbound) qqdata. usersDataTable tab = new UsersTableAdapter (). getDataByUsername (_ Mename); // Create the main item ListViewItem item = new ListViewItem (); // set the main item information item. text = getmemoname (_ Mename) + sj. toString () + "said:" + nr; item. imageIndex = tab [0]. photoCode-1; chatwindow. items. add (item) ;}// the time control cyclically queries private void timereffectick (object sender, EventArgs e) {// searches for unread messages sent by this friend in the database and adds the displayed information, display the configured read qqdata. chatDataTable tab2 = new ChatTableAdapter (). getDataBySenderAndReceiver (_ Friendname, _ Mename, 0); foreach (qqdata. chatRow row in tab2.Rows) {qqdata. usersDataTable tab = new UsersTableAdapter (). getDataByUsername (row. sender); // Create the main item ListViewItem item = new ListViewItem (); // set the main item information item. text = getmemoname (row. sender) + row. time. toString () + "said:" + row. content; item. imageIndex = tab [0]. photoCode-1; // mark as read if (row. sender ==_ Friendname) {new ChatTableAdapter (). updateState (1, row. ids);} chatwindow. items. add (item) ;}}// when the form is closed, assign a value of 0 to N, and control the custom control avatar. When the chat is closed, a message notification is sent to private void Chat_FormClosed (object sender, FormClosedEventArgs e) {Friendsmode. N = 0 ;}
Simple QQ communication function (5)