事件触发问题
我写了一小程序,想在程序一启动就触发按钮事件,要怎么做到?我尝试在mian()声明窗体frmEmulateLoginLookup 的变量,想通过窗体变量带出事件,但事实上是不行的。
下面是窗体frmEmulateLoginLookup 的具体代码
[code=csharp]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace sample_1
{
public partial class frmEmulateLoginLookup : Form
{
CookieCollection curCookies = null;
public frmEmulateLoginLookup()
{
InitializeComponent();
}
/// <summary>
/// post data
/// </summary>
/// <param name="paras"></param>
/// <returns></returns>
public string quoteParas(Dictionary<string, string> paras)
{
string quotedParas = "";
bool isFirst = true;
string val = "";
foreach (string para in paras.Keys)
{
if (paras.TryGetValue(para, out val))
{
if (isFirst)
{
isFirst = false;
quotedParas += para + "=" + HttpUtility.UrlPathEncode(val);
}
else
{
quotedParas += "&" + para + "=" + HttpUtility.UrlPathEncode(val);
}
}
else
{
break;
}
}
return quotedParas;
}
public void btnSearch_Click(object sender, EventArgs e)
{
if (txtIP.Text.Trim().Length <= 0)
{
MessageBox.Show("please input a IP for search...");
txtIP.Focus();
return;
}
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
//init post dict info
Dictionary<string, string> postDict = new Dictionary<string, string>();
postDict.Add("search_string",txtIP.Text.Trim());
postDict.Add("tos_accepted","on");
string LookupMainsearchUrl ="http://www.senderbase.org/lookup/";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(LookupMainsearchUrl);
//add cookie
req.CookieContainer = new CookieContainer();
//req.CookieContainer.Add(curCookies);
//set to POST
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
//req.Referer="http://www.senderbase.org/lookup/";
//prepare post data
string postDataStr = quoteParas(postDict);
byte[] postBytes = Encoding.UTF8.GetBytes(postDataStr);
req.ContentLength = postBytes.Length;
//send post data
Stream postDataStream = req.GetRequestStream();
postDataStream.Write(postBytes, 0, postBytes.Length);
postDataStream.Close();
//got response
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
//got returned html
StreamReader sr = new StreamReader(resp.GetResponseStream());
string loginBaiduRespHtml = sr.ReadToEnd();
txt_GetResult.Text = loginBaiduRespHtml;
//check whether got all expected cookies
this.Cursor = System.Windows.Forms.Cursors.Default;
}
public void btn_get_Click(object sender, EventArgs e)
{
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
string h1userP = @"<div\s+class=""leftside"">(?<h1user>.+?)</div>";
Match foundH1user = (new Regex(h1userP)).Match(txt_GetResult.Text);
if (foundH1user.Success)
{
//extracted the expected h1user's value
txt_GetEvaluate.Text = foundH1user.Groups["h1user"].Value;
}
else
{
txt_GetEvaluate.Text = "Not found Evaluate !";
}
this.Cursor = System.Windows.Forms.Cursors.Default;
}
public void btnclear_Click(object sender, EventArgs e)
{
txtIP.Text = "";
txt_GetEvaluate.Text = "";
txt_GetResult.Text = "";
}
}
}
[code]
[解决办法]
Load()事件里调用按钮事件
[解决办法]
Load事件中 button1_Click(this,null)
[解决办法]
+1