Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

winforms - C# Click button Without clicking manually

Just want to click accept button inside source without clicking it manually on form

And hide the form to the user

Form will come with accept=ok and gonna be hide

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;
    }
}

Just want to click accept button inside source without clicking it manually on form

And hide the form to the user0

Form will come with accept=ok and gonna be hide

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are talking about clicking, most probably you have event handler, which simply you want to invoke. I suppose it is OnAcceptClick .

So to achieve your goal simply invoke the method just like any other:

OnAcceptClick(null, null);

As first argument, you can pass reference to your accept button and if you need event arguments, you can create new instance of it as needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...