I have this code:
Button ssr = new Button();
ssr.Text = "SSR";
ssr.Location = new Point(500, 5);
ssr.Size = new Size(50, 30);
ssr.Click += new EventHandler(ssr_Click);
And I had a method ssr_Click which wrote a “1234” on the button. That was just for testing, now the method looks like this:
private void ssr_Click(string callsign, object sender, EventArgs e)
{
using (var wb = new WebClient())
{
string url = "xxxxxxxx";
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
string postData = "callsign=" + callsign;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string squawk = reader.ReadToEnd();
System.Windows.Forms.MessageBox.Show(squawk); // !!!
reader.Close();
dataStream.Close();
response.Close();
}
}
It is okay, it just doesn’t know the “callsign”, which makes sense, because it is defined somwhere else, so I added “string callsign” to the method parameters that when I call it, I also will give it the callsign. Since I did this, I get an error in first part of code, at line
ssr.Click += new EventHandler(ssr_Click);
it says this: No overload for ‘ssr_Click’ matches delegate ‘System.EventHandler’. It was fine before editing this method.
It is supposed to do this: someone clicks the button, it sends the callsign to the method, gets a four digit code from a .php at website, and it writes the code on the button, so I know that I will also have to edit the method so it returns a string, but first I need to solve this..
Any ideas?
EventHandleris a delegate type defined as:so your handler method must have the same signature i.e.
In your handler,
sendershould be the object which raised the event, so you might be able to get thecallsigndata from that, alternatively you could store it in a member variable.