【问题标题】:C# Click button Without clicking manuallyC#单击按钮无需手动单击
【发布时间】:2020-09-29 10:35:37
【问题描述】:

只想在源代码中单击接受按钮,而不是在表单上手动单击它

并向用户隐藏表单

表单将带有 accept=ok 并会被隐藏

using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Honeygain.Presenters;
using Honeygain.Views.Controls;
using Honeygain.Views.Events;

namespace Honeygain.Views.Forms
{
    // Token: 0x02000011 RID: 17
    internal class TermsForm : BaseForm
    {
        // Token: 0x1400001F RID: 31
        // (add) Token: 0x060000EB RID: 235 RVA: 0x00007DE0 File Offset: 0x00005FE0
        // (remove) Token: 0x060000EC RID: 236 RVA: 0x00007E18 File Offset: 0x00006018
        public event EventHandler<FormFailedEventArgs> TermsFailed;

        // Token: 0x14000020 RID: 32
        // (add) Token: 0x060000ED RID: 237 RVA: 0x00007E50 File Offset: 0x00006050
        // (remove) Token: 0x060000EE RID: 238 RVA: 0x00007E88 File Offset: 0x00006088
        public event EventHandler TermsRejected;

        // Token: 0x060000EF RID: 239 RVA: 0x00002A43 File Offset: 0x00000C43
        public TermsForm(IMainPresenter presenter)
        {
            this.InitializeComponent();
            this._presenter = presenter;
        }

        // Token: 0x060000F0 RID: 240 RVA: 0x00002A58 File Offset: 0x00000C58
        public void CloseTerms()
        {
            CancellationTokenSource actionCTS = this._actionCTS;
            if (actionCTS != null)
            {
                actionCTS.Cancel();
            }
            this._actionCTS = null;
            this._vid = null;
            base.Hide();
        }

        // Token: 0x060000F1 RID: 241 RVA: 0x00007EC0 File Offset: 0x000060C0
        public void ShowTerms(string text, string version, string vid)
        {
            if (!base.Visible)
            {
                CancellationTokenSource actionCTS = this._actionCTS;
                if (actionCTS != null)
                {
                    actionCTS.Cancel();
                }
                this._actionCTS = new CancellationTokenSource();
                this.body.DocumentText = text;
                this._version = version;
                this._vid = vid;
                base.ShowCenter();
            }
        }

        // Token: 0x060000F2 RID: 242 RVA: 0x00002A7F File Offset: 0x00000C7F
        private void OnFormVisibleChanged(object sender, EventArgs e)
        {
            if (!base.Visible)
            {
                this.Reject();
            }
        }

        // Token: 0x060000F3 RID: 243 RVA: 0x00007F14 File Offset: 0x00006114
        private void OnAcceptClick(object sender, EventArgs e)
        {
            TermsForm.<OnAcceptClick>d__14 <OnAcceptClick>d__;
            <OnAcceptClick>d__.<>4__this = this;
            <OnAcceptClick>d__.<>t__builder = AsyncVoidMethodBuilder.Create();
            <OnAcceptClick>d__.<>1__state = -1;
            <OnAcceptClick>d__.<>t__builder.Start<TermsForm.<OnAcceptClick>d__14>(ref <OnAcceptClick>d__);
        }

        // Token: 0x060000F4 RID: 244 RVA: 0x00002A8F File Offset: 0x00000C8F
        private void Reject()
        {
            if (!string.IsNullOrEmpty(this._vid))
            {
                EventHandler termsRejected = this.TermsRejected;
                if (termsRejected != null)
                {
                    termsRejected(this, EventArgs.Empty);
                }
            }
            this.CloseTerms();
        }

        // Token: 0x060000F5 RID: 245 RVA: 0x00007F4C File Offset: 0x0000614C
        private Task Accept()
        {
            TermsForm.<Accept>d__16 <Accept>d__;
            <Accept>d__.<>4__this = this;
            <Accept>d__.<>t__builder = AsyncTaskMethodBuilder.Create();
            <Accept>d__.<>1__state = -1;
            <Accept>d__.<>t__builder.Start<TermsForm.<Accept>d__16>(ref <Accept>d__);
            return <Accept>d__.<>t__builder.Task;
        }

        // Token: 0x060000F6 RID: 246 RVA: 0x00002ABB File Offset: 0x00000CBB
        private void Prepare()
        {
            base.Enabled = false;
        }

        // Token: 0x060000F7 RID: 247 RVA: 0x00002AC4 File Offset: 0x00000CC4
        private void Finish()
        {
            base.Enabled = true;
        }

        // Token: 0x060000F8 RID: 248 RVA: 0x00002ACD File Offset: 0x00000CCD
        private void Fail(Exception exception)
        {
            EventHandler<FormFailedEventArgs> termsFailed = this.TermsFailed;
            if (termsFailed != null)
            {
                termsFailed(this, new FormFailedEventArgs
                {
                    Error = exception
                });
            }
            this.CloseTerms();
        }

        // Token: 0x060000F9 RID: 249 RVA: 0x00002AF3 File Offset: 0x00000CF3
        protected override void Dispose(bool disposing)
        {
            if (disposing && this.components != null)
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        // Token: 0x060000FA RID: 250 RVA: 0x00007F90 File Offset: 0x00006190
        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(TermsForm));
            this.panel = new TableLayoutPanel();
            this.body = new TermsBrowser(this.components);
            this.accept = new Button();
            this.title = new Label();
            this.panel.SuspendLayout();
            base.SuspendLayout();
            componentResourceManager.ApplyResources(this.panel, "panel");
            this.panel.Controls.Add(this.body, 0, 1);
            this.panel.Controls.Add(this.accept, 0, 2);
            this.panel.Controls.Add(this.title, 0, 0);
            this.panel.Name = "panel";
            componentResourceManager.ApplyResources(this.body, "body");
            this.body.IsWebBrowserContextMenuEnabled = false;
            this.body.Name = "body";
            this.body.ScriptErrorsSuppressed = true;
            this.body.WebBrowserShortcutsEnabled = false;
            componentResourceManager.ApplyResources(this.accept, "accept");
            this.accept.BackColor = Color.FromArgb(11, 152, 222);
            this.accept.ForeColor = Color.White;
            this.accept.Name = "accept";
            this.accept.UseVisualStyleBackColor = false;
            this.accept.Click += this.OnAcceptClick;
            componentResourceManager.ApplyResources(this.title, "title");
            this.title.ForeColor = Color.FromArgb(14, 65, 88);
            this.title.Name = "title";
            componentResourceManager.ApplyResources(this, "$this");
            base.Controls.Add(this.panel);
            base.Name = "TermsForm";
            base.VisibleChanged += this.OnFormVisibleChanged;
            this.panel.ResumeLayout(false);
            this.panel.PerformLayout();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        // Token: 0x0400006D RID: 109
        private readonly IMainPresenter _presenter;

        // Token: 0x0400006E RID: 110
        private string _version;

        // Token: 0x0400006F RID: 111
        private string _vid;

        // Token: 0x04000070 RID: 112
        private CancellationTokenSource _actionCTS;

        // Token: 0x04000073 RID: 115
        private IContainer components;

        // Token: 0x04000074 RID: 116
        private TableLayoutPanel panel;

        // Token: 0x04000075 RID: 117
        private TermsBrowser body;

        // Token: 0x04000076 RID: 118
        private Button accept;

        // Token: 0x04000077 RID: 119
        private Label title;
    }
}

只想在源代码中单击接受按钮,而不是在表单上手动单击它

并将表单隐藏到user0

表单将带有 accept=ok 并会被隐藏

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    如果您正在谈论单击,很可能您有事件处理程序,您只想调用它。我想是OnAcceptClick

    所以要实现您的目标,只需像其他任何方法一样调用该方法:

    OnAcceptClick(null, null);
    

    作为第一个参数,您可以传递对接受按钮的引用,如果您需要事件参数,您可以根据需要创建它的新实例。

    【讨论】:

    • 感谢您的回答,我是编码新手,我需要更改和添加它
    • @Codforc 我不能告诉你。编写代码的地点也决定了执行它的时刻和环境。只有你自己知道。
    【解决方案2】:

    可以使用PerformClick方法:

    button1.PerformClick();
    

    【讨论】:

    • 非常感谢您在我需要添加执行点击的地方以及我的新功能如何
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 2017-08-12
    • 2012-10-20
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多