首页 > 解决方案 > 如何通过源代码单击启用按钮?

问题描述

这是我拥有的exe;

我只想在代码中单击启用按钮,而不是在表单上手动;

就像触发按钮,执行点击,启用点击(我不知道的);

如何通过代码点击启用按钮来提供 exe;

如果你能解决,我会很高兴;

非常感谢您对未来的帮助;

using Honeygain.Presenters;
using Honeygain.Properties;
using Honeygain.Utilities;
using Honeygain.Views.Events;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Honeygain.Views.Forms
{
  internal class StreamingForm : BaseForm
  {
    private readonly IMainPresenter _presenter;
    private CancellationTokenSource _actionCTS;
    private IContainer components;
    private TableLayoutPanel panel;
    private PictureBox banner;
    private Label title;
    private LinkLabel terms;
    private Label description;
    private TableLayoutPanel buttons;
    private Button disable;
    private Button enable;
    private LinkLabel questions;

    public event EventHandler<FormFailedEventArgs> StreamingToggleFailed;

    public event EventHandler StreamingToggleFinished;

    public event EventHandler StreamingToggleStarted;

    public StreamingForm(IMainPresenter presenter)
    {
      this.InitializeComponent();
      this._presenter = presenter;
    }

    private void OnFormVisibleChanged(object sender, EventArgs e)
    {
      this._actionCTS?.Cancel();
      this._actionCTS = this.Visible ? new CancellationTokenSource() : (CancellationTokenSource) null;
    }

    private void OnTermsLinkClicked(object sender, LinkLabelLinkClickedEventArgs e) => Websites.OpenTermsOfUse();

    private void OnQuestionsLinkClicked(object sender, LinkLabelLinkClickedEventArgs e) => Websites.OpenContentDelivery();

    private async void OnDisableClick(object sender, EventArgs e) => await this.Toggle(false);

    private async void OnEnableClick(object sender, EventArgs e) => await this.Toggle(true);

    private async Task Toggle(bool enable)
    {
      this.Prepare();
      try
      {
        await this.Update(enable);
      }
      catch (TaskCanceledException ex)
      {
        Logger.Instance.Debug((Exception) ex);
      }
      catch (Exception ex)
      {
        this.Fail(ex);
      }
      finally
      {
        this.Finish();
      }
    }

    private void Prepare()
    {
      EventHandler streamingToggleStarted = this.StreamingToggleStarted;
      if (streamingToggleStarted != null)
        streamingToggleStarted((object) this, EventArgs.Empty);
      this.Enabled = false;
    }

    private async Task Update(bool enable)
    {
      StreamingForm streamingForm = this;
      await streamingForm._presenter.SetStreamingStatusAsync(enable, streamingForm._actionCTS.Token);
      streamingForm.Hide();
    }

    private void Fail(Exception exception)
    {
      this.Hide();
      EventHandler<FormFailedEventArgs> streamingToggleFailed = this.StreamingToggleFailed;
      if (streamingToggleFailed == null)
        return;
      streamingToggleFailed((object) this, new FormFailedEventArgs()
      {
        Error = exception
      });
    }

    private void Finish()
    {
      this.Enabled = true;
      EventHandler streamingToggleFinished = this.StreamingToggleFinished;
      if (streamingToggleFinished == null)
        return;
      streamingToggleFinished((object) this, EventArgs.Empty);
    }

    protected override void Dispose(bool disposing)
    {
      if (disposing && this.components != null)
        this.components.Dispose();
      base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
      ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (StreamingForm));
      this.panel = new TableLayoutPanel();
      this.banner = new PictureBox();
      this.terms = new LinkLabel();
      this.buttons = new TableLayoutPanel();
      this.disable = new Button();
      this.enable = new Button();
      this.questions = new LinkLabel();
      this.description = new Label();
      this.title = new Label();
      this.panel.SuspendLayout();
      ((ISupportInitialize) this.banner).BeginInit();
      this.buttons.SuspendLayout();
      this.SuspendLayout();
      componentResourceManager.ApplyResources((object) this.panel, "panel");
      this.panel.Controls.Add((Control) this.banner, 0, 0);
      this.panel.Controls.Add((Control) this.terms, 0, 4);
      this.panel.Controls.Add((Control) this.buttons, 0, 7);
      this.panel.Controls.Add((Control) this.questions, 0, 5);
      this.panel.Controls.Add((Control) this.description, 0, 2);
      this.panel.Controls.Add((Control) this.title, 0, 1);
      this.panel.Name = "panel";
      componentResourceManager.ApplyResources((object) this.banner, "banner");
      this.banner.Image = (Image) Resources.streaming;
      this.banner.Name = "banner";
      this.banner.TabStop = false;
      this.terms.ActiveLinkColor = Color.FromArgb(7, 100, 146);
      componentResourceManager.ApplyResources((object) this.terms, "terms");
      this.terms.ForeColor = Color.FromArgb(118, 133, 141);
      this.terms.LinkColor = Color.FromArgb(0, 147, (int) byte.MaxValue);
      this.terms.Name = "terms";
      this.terms.TabStop = true;
      this.terms.UseCompatibleTextRendering = true;
      this.terms.LinkClicked += new LinkLabelLinkClickedEventHandler(this.OnTermsLinkClicked);
      componentResourceManager.ApplyResources((object) this.buttons, "buttons");
      this.buttons.Controls.Add((Control) this.disable, 0, 0);
      this.buttons.Controls.Add((Control) this.enable, 2, 0);
      this.buttons.Name = "buttons";
      componentResourceManager.ApplyResources((object) this.disable, "disable");
      this.disable.BackColor = Color.White;
      this.disable.ForeColor = Color.FromArgb(11, 152, 222);
      this.disable.Name = "disable";
      this.disable.UseVisualStyleBackColor = false;
      this.disable.Click += new EventHandler(this.OnDisableClick);
      componentResourceManager.ApplyResources((object) this.enable, "enable");
      this.enable.BackColor = Color.FromArgb(11, 152, 222);
      this.enable.ForeColor = Color.White;
      this.enable.Name = "enable";
      this.enable.UseVisualStyleBackColor = false;
      this.enable.Click += new EventHandler(this.OnEnableClick);
      this.questions.ActiveLinkColor = Color.FromArgb(7, 100, 146);
      componentResourceManager.ApplyResources((object) this.questions, "questions");
      this.questions.ForeColor = Color.FromArgb(118, 133, 141);
      this.questions.LinkColor = Color.FromArgb(0, 147, (int) byte.MaxValue);
      this.questions.Name = "questions";
      this.questions.TabStop = true;
      this.questions.UseCompatibleTextRendering = true;
      this.questions.LinkClicked += new LinkLabelLinkClickedEventHandler(this.OnQuestionsLinkClicked);
      componentResourceManager.ApplyResources((object) this.description, "description");
      this.description.ForeColor = Color.FromArgb(118, 133, 141);
      this.description.Name = "description";
      componentResourceManager.ApplyResources((object) this.title, "title");
      this.title.ForeColor = Color.FromArgb(14, 65, 88);
      this.title.Name = "title";
      componentResourceManager.ApplyResources((object) this, "$this");
      this.Controls.Add((Control) this.panel);
      this.Name = nameof (StreamingForm);
      this.VisibleChanged += new EventHandler(this.OnFormVisibleChanged);
      this.panel.ResumeLayout(false);
      this.panel.PerformLayout();
      ((ISupportInitialize) this.banner).EndInit();
      this.buttons.ResumeLayout(false);
      this.ResumeLayout(false);
      this.PerformLayout();
    }
  }
}```

标签: c#button

解决方案


您可以使用.PerformClick();来在程序中单击它。


推荐阅读