c# - Getting all the items from the listbox winform -


good day! having troubles on getting items, selected or not, listbox. whenever click send button, items ones selected (this current results of code below: http://imgur.com/ja94bjm). want items textbox not selected ones , not repeating.

private void cmd_send_click_1(object sender, eventargs e) {      (int = 0; < listbox1.items.count; i++)      {          try          {              string pno = textbox4.text.tostring();              string path = textbox5.text.tostring();              string name = textbox6.text.tostring();              string user = textbox7.text.tostring();              output.text += "\n sent data : " + pno + " " + user + " " + name + " " + path;           }           catch (exception ex)           {               wait.abort();               output.text += "error..... " + ex.stacktrace;           }            networkstream ns = tcpclnt.getstream();           string data = "";           data = "--++" + "  " + textbox4.text + " " + textbox5.text + " " + textbox6.text + " " + textbox7.text;            if (ns.canwrite)           {               byte[] bf = new asciiencoding().getbytes(data);               ns.write(bf, 0, bf.length);               ns.flush();           }     } } 

if want access items listbox, have iterate of items , access value of item. here's sample how can achieve this:

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;  namespace consoleapplication1 {     class program     {         class process         {             public int processid { get; set; }             public string filepath { get; set; }             public string filename { get; set; }             public string user { get; set; }         }         static void main(string[] args)         {              process p1 = new process();             p1.processid = 1;             p1.filename = "tool.exe";             p1.filepath = @"c:\tool.exe";             p1.user = "user1";              process p2 = new process();             p2.processid = 2;             p2.filename = "tool2.exe";             p2.filepath = @"c:\tool2.exe";             p2.user = "user2";              process p3 = new process();             p3.processid = 3;             p3.filename = "tool3.exe";             p3.filepath = @"c:\tool3.exe";             p3.user = "user3";               listbox listbox = new listbox();             listbox.items.add(p1);             listbox.items.add(p2);             listbox.items.add(p3);              (int = 0; < listbox.items.count; i++)             {                 process p = (process)listbox.items[i]; //access value of item                 console.writeline("process id: {0}", p.processid);                 console.writeline("process filename: {0}", p.filename);                 console.writeline("process file path: {0}", p.filepath);                 console.writeline("process user: {0}", p.user);             }              console.readline();         }     } } 

we have sample class process different properties. each process added on listbox later accessed inside loop.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -