+ 3

C# Help with button

Hi everyone, I have this code where it gets a response from an API and I want it to send to my textbox by pressing a button, but when I run the code and press the button nothing happens.. pls help? Thank you. using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; using System.Windows; using System.Windows.Forms; namespace JokeGEN { public partial class Form1 : Form { public Form1() { InitializeComponent(); } static readonly HttpClient client = new HttpClient(); public class Value { public int id { get; set; } public string joke { get; set; } public List<string> categories { get; set; } } public class Root { public string type { get; set; } public Value value { get; set; } } private async System.Threading.Tasks.Task btnjoke_ClickAsync(object sender, EventArgs e) { string responseBody = await client.GetStringAsync("http://api.icndb.com/jokes/random"); Root jokes = JsonConvert.DeserializeObject<Root>(responseBody); string joke = jokes.value.joke; txtjoke.Text = joke; } } }

4th Apr 2021, 5:13 PM
Wanna be
Wanna be - avatar
10 Antworten
+ 2
The button is not waiting for the result of GetStringAsync. GetStringAsync is returning a Task. Use this code inside you button click. Do not make your button click Async. private void button2_Click(object sender, EventArgs e) { Task<string> responseBody = client.GetStringAsync("http://api.icndb.com/jokes/random"); //Root jokes = JsonConvert.DeserializeObject<Root>(responseBody); //string joke = jokes.value.joke; txtjoke.Text = responseBody.Result; } https://stackoverflow.com/questions/28517923/reading-the-response-from-httpclient-getstringasync (read second answer, not the one that is approved best)
4th Apr 2021, 9:18 PM
sneeze
sneeze - avatar
+ 2
@sneeze can you send the code completed? I tried your way and i readed the stackoverflow post and i cant manage to put it working
5th Apr 2021, 3:13 PM
Wanna be
Wanna be - avatar
+ 2
@sneeze I tried here and didnt work.. I only have a textbox called txtjoke and a button called btnjoke on the design part and as you can see up there thats all my code.. using winforms etc.. Im trying to make this programm like since 2 weeks ago and i can't find solutions... Can you try to do it and see if you can get it working and help me pls?
5th Apr 2021, 3:55 PM
Wanna be
Wanna be - avatar
+ 1
Have a look at this link. I place the code in the playground to be able to add more text. It does not run in the playground. https://code.sololearn.com/ca18a18a7a10/?ref=app
5th Apr 2021, 3:41 PM
sneeze
sneeze - avatar
+ 1
Sorry You have to put in more effort your self. Programming is not copy and paste someone else its code. Add another button to you form. Call it button2. or make a standard button click with the btnjoke button. And copy the code lines from my button click event, to this event. Add a listbox to you from or comment out this line Take small steps. Make sure every single step works. Do not be afraid, errors do not ruine your computer
5th Apr 2021, 4:16 PM
sneeze
sneeze - avatar
+ 1
Wanna be you can't test out Windows Forms code in SoloLearn, because SoloLearn can't and doesn't implement Windows Forms content
5th Apr 2021, 6:24 PM
️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️
️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️ - avatar
+ 1
@C4Oc yeah im using Visual Studio 2019 , but anyways i can't find way to put my code working and everyone i asked so far cant do it aswell..
6th Apr 2021, 5:46 PM
Wanna be
Wanna be - avatar
0
I think you have to add this in Form1(): yourButtonName.Click += btnjoke_ClickAsync; after InitalizeComponent() if you added the button in the Forms Designer. If you have already added this, then I can't help you further
5th Apr 2021, 6:51 AM
️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️
️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️️ - avatar
0
yeah I did that and its the same.. @sneeze
6th Apr 2021, 5:47 PM
Wanna be
Wanna be - avatar
0
post your code here, again. And lets have a look.
6th Apr 2021, 6:17 PM
sneeze
sneeze - avatar