1

Im trying to add items to combobox from a task that fetches values from an api, but I'm not sure how to do that properly. I have models set up and i can parse api to a console but don't know how to display them in a combobox. This is my library for fetching api:

 public static HttpClient httpClient = new HttpClient();
 public static async Task<List<Result>> GetResults(string url) {
        List <Result> results = null;
        try
        {
            HttpResponseMessage response = await httpClient.GetAsync(url);
            var jsonString =  response.Content.ReadAsStringAsync();
            results = JsonConvert.DeserializeObject<List<Result>> 
(jsonString.Result);

        }
        catch (HttpRequestException e)
        {
            Console.WriteLine(e.Message);
        }

        return results;
    }

And this is my form and a combobox:

public async Task ResultBoxLoader() {
        List<Result> rezultati = new List<Result>();
        foreach (var result in rezultati)
        {
            resultBox.Items.Add(Repository.GetResults("http://worldcup.sfg.io/teams/results/"));
        }
    }

    private async void resultBox_SelectedIndexChanged(object sender, 
    EventArgs e)
    {
       await ResultBoxLoader();
        Task task = ResultBoxLoader();
        task.Wait();

    }

Combobox should display a property from a list that is "country". My model looks like this:

private int Id { get; set; }
public string Country { get; set; }

resultBox is the combobox

  • 1
    https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread?rq=1 – Robin Bennett Aug 05 '19 at 13:51
  • @RobinBennett Their code isn't trying to update the GUI from another thread, they just appear to be a bit confused to how async/await works – Sasha Aug 05 '19 at 13:52

1 Answers1

2

Your code is close, but this should work:

public async Task ResultBoxLoader() {
    List<Result> rezultati = await Repository.GetResults("http://worldcup.sfg.io/teams/results/");
    foreach (var result in rezultati)
    {
        resultBox.Items.Add(result);
    }
}

I've moved your API call to the List<Result> and awaited it so you return the list rather than the task. From there you just loop through and add them how you was already attempting to do so :)

Sasha
  • 1,674
  • 1
  • 16
  • 23
  • he should also remove second "ResultBoxLoader" call – Adassko Aug 05 '19 at 13:55
  • I think it's better to return a list, but i'm a bit confused what to do next i added result.country but still i don't populate the comobox. Should i add something to a property of a combobox and is my task call ok? It should happen when the form loads – Nadan Marenković Aug 05 '19 at 13:58
  • still nothing happens, how do i run that task in a combobox? i wrote it like: Task task = ResultBoxLoader(); task.Start(); inside of a combobox – Nadan Marenković Aug 05 '19 at 14:35
  • in Form Load, put the async before void and call `await ResultBoxLoader()` If you want country to appear, override the `ToString` function on your model and have it return country. ComboBoxes support adding objects as items, all it requires is it to be parsable to a string. – Sasha Aug 05 '19 at 15:18