winforms - c#: adding new item to comboBox -
i want list of clients ip address added combobox in winforms list doesn't appear in combobox.
here code server
// list of sockets each client connected server list<socket> astr = new list<socket>(); public form1() { initializecomponent(); addfg(); combobox1.selectedindexchanged += combobox1_selectedindexchanged; } public void addfg() { foreach (socket s in astr) { string str = string.format("client : " + s.remoteendpoint); combobox1.items.add(new object[] {str}) } } private void combobox1_selectedindexchanged(object sender, eventargs e) { messagebox.show(combobox1.items[0]); }
but getting error "argumentexception unhandled" or in detail "items collection cannot modified when datasource property set".
try this:
foreach (socket s in astr) { string str = string.format("client : " + s.remoteendpoint); combobox1.items.add(str); }
in version, looks trying add array, items.add()
method adds single item. add multiple items, use addrange()
, doesn't case code.
be careful code:
private void combobox1_selectedindexchanged(object sender, eventargs e) { messagebox.show(combobox1.items[0]); }
you should check see if selected:
if (combobox1.items.selectedindex > -1) { messagebox.show(combobox1.items[0]); }
Comments
Post a Comment