delgate to update listbox in form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bahaa
    New Member
    • Apr 2010
    • 2

    delgate to update listbox in form

    hii

    i have a list box in form and i want to update list from listuser in dll , i can't used any event because i want to update list autumatlically after every user connected (chat).

    the code after user connect
    Code:
               //t = new Thread(AcceptClients);
                //t.Start(); 
            }
    
            //private void AcceptClients()
            //{
            //    while (true)
            //    {
            //        msguser user = new msguser(string.Empty);
            //        //create new socket
            //        TcpClient client = listener.AcceptTcpClient();
            //        n = client.GetStream();
            //        arr.Add(n);
    
            //        Thread t = new Thread(getuser);
            //        t.Start(n);
            //    }
            //}
    
            //private void getuser(object obj)
            //{
            //    BinaryFormatter b = new BinaryFormatter();
            //    msguser username = new msguser("");
            //    username.Usrname = b.Deserialize((NetworkStream)obj).ToString();
            //    lusername.Add(username.Usrname);
    
    
            //    foreach (NetworkStream item in arr)
            //    {
            //        b.Serialize(item, username);
            //    }
            //}
    Last edited by tlhintoq; Apr 2 '10, 08:32 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.


    Do you understand that the code you supplied never runs? It is all commented out.

    i have a list box in form and i want to update list from listuser in dll
    Nobody here is going to know anything about your dll that you wrote.

    Comment

    • bahaa
      New Member
      • Apr 2010
      • 2

      #3
      the DLL code

      Code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Runtime.Serialization.Formatters.Binary;
      using System.Net;
      using System.Net.Sockets;
      using System.Threading;
      
      namespace setgetmessage
      {
          [Serializable]
          public class msguser
          {
              public string data;
              private string usrname;
      
              public string Usrname
              {
                  get { return usrname; }
                  set { usrname = value; }
              }
              public msguser(string msg)
              {
                  data = msg;
              }
      
          }
      }
      this code in form
      Code:
      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.Net.Sockets;
      using System.Net;
      using System.Threading;
      using setgetmessage;
      using System.Collections;
      using System.Runtime.Serialization.Formatters.Binary;
      using System.IO;
      
      namespace server
      {
          delegate void pleasework(TcpListener listener);
          public partial class formserver : Form
          {
              public formserver()
              {
                  InitializeComponent();
              }
      
              Thread t;
              static List<NetworkStream> arr = new List<NetworkStream>();
              static List<string> lusername = new List<string>();
              static TcpListener listener;
              static NetworkStream n;
              private void btnconnect_Click(object sender, EventArgs e)
              {
      
                  btnconnect.Visible = false;
      
                  listener = new TcpListener(IPAddress.Parse(masktbip.Text), (int) numericport.Value);
                  listener.Start();
      
      
                  t = new Thread(AcceptClients);
                  t.Start();
              }
      
              private void AcceptClients()
              {
                  while (true)
                  {
                      msguser user = new msguser(string.Empty);
                      //create new socket
                      TcpClient client = listener.AcceptTcpClient();
                      n = client.GetStream();
                      arr.Add(n);
      
                      Thread t = new Thread(getuser);
                      t.Start(n);
                  }
              }
      
              private void getuser(object obj)
              {
                  BinaryFormatter b = new BinaryFormatter();
                  msguser username = new msguser("");
                  username.Usrname = b.Deserialize((NetworkStream)obj).ToString();
                  lusername.Add(username.Usrname);
                  listBox1.Items.Add(username.Usrname);//ERROR
      
                  foreach (NetworkStream item in arr)
                  {
                      b.Serialize(item, username);
                  }
              }
      
                private void btndiconnect_Click(object sender, EventArgs e)
              {
      
                  Application.Exit();
              }
      
      
          }
      
      }
      thanks
      Last edited by tlhintoq; Apr 2 '10, 10:07 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        You've been told once politely.
        Let me tell you a second time: Put in [code] tags around your code snippets. Making others do it for you is just lazy and will make others not want to help you.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Originally posted by bahaa
          i have a list box in form and i want to update list from listuser in dll , i can't used any event because i want to update list autumatlically after every user connected (chat).
          Take a look at your own DLL. There is no listuser in your DLL.

          Am I correct in thinking that you have two pieces of code that you didn't write (got someplace on the web or handed to you by someone else) and you don't understand either piece, but you want someone to tie them together for you?


          Generic answer #4:
          Originally posted by OriginalPoster
          I have xxx, then do yyy then zzz happens. Here's my code ...
          Ok. You know what you have.
          What you don't have is a question. Nobody here knows why you posted this since you haven't asked anything. What is it you are looking for?
          I recommend you read the FAQ about How to ask a good question so the volunteers will be able to help you.

          Comment

          Working...