[C#]如何在一个函数里调用一个控件的事件过程

时间:2022-12-04 20:20:57
        private static string GetWebContent(string Url)
        {
            string strResult = "";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);//声明一个HttpWebRequest请求
                request.Timeout = 30000;//设置连接超时时间
                request.Headers.Set("Pragma", "no-cache");
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamReceive = response.GetResponseStream();
                Encoding encoding = Encoding.GetEncoding("utf-8");
                StreamReader streamReader = new StreamReader(streamReceive, encoding);
                strResult = streamReader.ReadToEnd();
            }
            catch
            {
                Form3 frm3 = new Form3();
                frm3.Text = "提示";
                frm3.STR = "连接目标地址出错!是否重试?";
                frm3.StartPosition = FormStartPosition.CenterParent;
                frm3.ShowDialog();
                if (frm3.Retry == true)
                {
                    Application.ExitThread();
                    System.Environment.Exit(System.Environment.ExitCode);
                    button3_Click (...);//我想在这里做个button的事件调用,怎么实现呢?
                }
                else
                {
                    MessageBox.Show("连接目标地址出错!是否重试?", "提示", MessageBoxButtons.RetryCancel);
                }
                 
            }
            return strResult;
        }

15 个解决方案

#1


button3_Click(button3,EventArgs.Empty)

#2


button3_Click   +=new System.EventHandler(this.Button_Click);  

#3


button3_Click +=new System.EventHandler(this.Button_Click);  //添加事件
button3_Click -=new System.EventHandler(this.Button_Click);  //移除事件

#4


引用 1 楼 gxttr 的回复:
button3_Click(button3,EventArgs.Empty)


这个我加进去怎么没有用呢?

#5


引用 3 楼 wtj2832 的回复:
button3_Click +=new System.EventHandler(this.Button_Click); //添加事件
button3_Click -=new System.EventHandler(this.Button_Click); //移除事件


这两个我该怎么应用呢?

#6


引用 5 楼 icoer 的回复:
引用 3 楼 wtj2832 的回复:
button3_Click +=new System.EventHandler(this.Button_Click); //添加事件
button3_Click -=new System.EventHandler(this.Button_Click); //移除事件


这两个我该怎么应用呢?
好像跟我提出的问题好像不搭边吧???

#7


试试 button3_click(this, new EventArg());

#8


引用 7 楼 tootto 的回复:
试试 button3_click(this, new EventArg());


试试???
晕!!
试过了,不行哦!

#9


是不是要用委托呢?
我是新手,要怎么实现呢?

#10


this.button3.PerformClick();

#11


引用 10 楼 ojlovecd 的回复:
this.button3.PerformClick();


编译时出现以下提示:
错误 CS0026: 关键字“this”在静态属性、静态方法或静态字段初始值设定项中无效

#12


把button中要实现的操作提取方法,然后再次此处调用。

#13


private static string GetWebContent(string Url)

我把static去掉后,编译通过 谢谢!ojlovecd

#14


版主,这个可以帮我结贴了,谢谢!
怎么给分呢?

#15


引用 14 楼 icoer 的回复:
版主,这个可以帮我结贴了,谢谢!
怎么给分呢?

点击“结贴”按钮

#1


button3_Click(button3,EventArgs.Empty)

#2


button3_Click   +=new System.EventHandler(this.Button_Click);  

#3


button3_Click +=new System.EventHandler(this.Button_Click);  //添加事件
button3_Click -=new System.EventHandler(this.Button_Click);  //移除事件

#4


引用 1 楼 gxttr 的回复:
button3_Click(button3,EventArgs.Empty)


这个我加进去怎么没有用呢?

#5


引用 3 楼 wtj2832 的回复:
button3_Click +=new System.EventHandler(this.Button_Click); //添加事件
button3_Click -=new System.EventHandler(this.Button_Click); //移除事件


这两个我该怎么应用呢?

#6


引用 5 楼 icoer 的回复:
引用 3 楼 wtj2832 的回复:
button3_Click +=new System.EventHandler(this.Button_Click); //添加事件
button3_Click -=new System.EventHandler(this.Button_Click); //移除事件


这两个我该怎么应用呢?
好像跟我提出的问题好像不搭边吧???

#7


试试 button3_click(this, new EventArg());

#8


引用 7 楼 tootto 的回复:
试试 button3_click(this, new EventArg());


试试???
晕!!
试过了,不行哦!

#9


是不是要用委托呢?
我是新手,要怎么实现呢?

#10


this.button3.PerformClick();

#11


引用 10 楼 ojlovecd 的回复:
this.button3.PerformClick();


编译时出现以下提示:
错误 CS0026: 关键字“this”在静态属性、静态方法或静态字段初始值设定项中无效

#12


把button中要实现的操作提取方法,然后再次此处调用。

#13


private static string GetWebContent(string Url)

我把static去掉后,编译通过 谢谢!ojlovecd

#14


版主,这个可以帮我结贴了,谢谢!
怎么给分呢?

#15


引用 14 楼 icoer 的回复:
版主,这个可以帮我结贴了,谢谢!
怎么给分呢?

点击“结贴”按钮