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