2

Here is something i can get my head around. In windows form i am tring to update combobox by result of function. The Function return correct resutlt as List<string> but it updates only the first time the event passes. Here is what i have tried.

cbDatabase.DataSource = GetServerDatabases(txbServer.Text); 
cbDatabase.Refresh(); 

I know I cant just loop the result list and refill it but i cant stop thinking that there is a better way.

Thanks in advance

Simon Karlsson
  • 4,090
  • 22
  • 39
Hristo Alexsiev
  • 146
  • 2
  • 11
  • 1
    What is not *first time*? Do you set `DataSource` again (e.g. [like here](http://stackoverflow.com/a/17615334/1997232)) or how does *second time* looks like? – Sinatr Feb 02 '16 at 16:06
  • It should refresh __whenever you set the datasource again__. Calling __refresh alone will not__ make it go to get the values again, only to update the screen. – TaW Feb 02 '16 at 16:56
  • The first and the second are the same as i wrote above first time it pass true the code i have list a,b,c ec. and it fills it in the combo box second time i pass true // it's onclick on button GetServerDatabases(txbServer.Text) return list as z , x, c . But the combo box still shows a,b,c that is my question. – Hristo Alexsiev Feb 02 '16 at 18:50

1 Answers1

1

You could just use a foreach loop to go through the list, or if you wanted to make it a bit more sophisticated you could raise an event each time the function passes and load each result individually. Here's a snippet of code I wrote recently to do just this job:

    public void Camera_FoundEvent(string addr, string port)
    {
        if (InvokeRequired)
        {
            Invoke(new UpdateCameraList(Camera_FoundEvent), addr, port);
        }
        else
        {
            if (!cboCameras.Items.Contains(addr))
            {
                //if default port 80 port no. is ommitted, else add port
                string cam = (port != null) ? (addr + port) : addr;
                cboCameras.Items.Add(cam);
            }
        }
    }