什么';这是用c#中的不同按钮执行重复操作的最佳方式

本文关键字:执行 按钮 方式 最佳 操作 什么 | 更新日期: 2023-09-27 18:28:54

这是我当前的代码:

        subject subject1;
    string DocPath = AppDomain.CurrentDomain.BaseDirectory  + "Documents/";
    public Form1()
    {
        InitializeComponent();
        subject1 = new subject();                     
    }
    public class subject
    {
        Form1 frm;
        public void changeTab(int tabPage/* , Form1 frm1 */, Form1 frm1)
        {
            frm = frm1;
            frm.TabControlSubjects.SelectTab(tabPage);
        }
    }
    private void materialRaisedButton1_Click(object sender, EventArgs e)
    {
        subject1.changeTab(0, this);        
    }
    private void materialRaisedButton2_Click(object sender, EventArgs e)
    {
        subject1.changeTab(1, this);
    }
    private void materialRaisedButton4_Click(object sender, EventArgs e)
    {
        subject1.changeTab(2, this);
    }
    private void materialRaisedButton3_Click(object sender, EventArgs e)
    {
        subject1.changeTab(3, this);
    }
    private void materialRaisedButton8_Click(object sender, EventArgs e)
    {
        subject1.changeTab(4, this);
    }
    private void materialRaisedButton7_Click(object sender, EventArgs e)
    {
        subject1.changeTab(5, this);
    }
    private void materialRaisedButton6_Click(object sender, EventArgs e)
    {
        subject1.changeTab(6, this);
    }
    private void materialRaisedButton5_Click(object sender, EventArgs e)
    {
        subject1.changeTab(7, this);
    }
    private void materialRaisedButton12_Click(object sender, EventArgs e)
    {
        subject1.changeTab(8, this);
    }
    private void materialRaisedButton11_Click(object sender, EventArgs e)
    {
        subject1.changeTab(9, this);
    }
    private void materialRaisedButton10_Click(object sender, EventArgs e)
    {
        subject1.changeTab(10, this);
    }
    private void materialRaisedButton9_Click(object sender, EventArgs e)
    {
        subject1.changeTab(11, this);
    }

有没有更有效的方法可以做到这一点?因为如果我现在来看,似乎有更好的方法可以做到这一点。我是C#的新手,我还在边讲边学习。欢迎任何建议/提示。谢谢你的阅读。

什么';这是用c#中的不同按钮执行重复操作的最佳方式

试试这个。我从按钮的文本名称中提取了按钮的编号。

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;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        subject subject1;
        string DocPath = AppDomain.CurrentDomain.BaseDirectory + "Documents/";
        public Form1()
        {
            InitializeComponent();
            subject1 = new subject();
            materialRaisedButton1.Click += new EventHandler(materialRaisedButton_Click);
            materialRaisedButton2.Click += new EventHandler(materialRaisedButton_Click);
            materialRaisedButton3.Click += new EventHandler(materialRaisedButton_Click);
            materialRaisedButton4.Click += new EventHandler(materialRaisedButton_Click);
            materialRaisedButton5.Click += new EventHandler(materialRaisedButton_Click);
            materialRaisedButton6.Click += new EventHandler(materialRaisedButton_Click);
            materialRaisedButton7.Click += new EventHandler(materialRaisedButton_Click);
            materialRaisedButton8.Click += new EventHandler(materialRaisedButton_Click);
            materialRaisedButton9.Click += new EventHandler(materialRaisedButton_Click);
            materialRaisedButton10.Click += new EventHandler(materialRaisedButton_Click);
            materialRaisedButton11.Click += new EventHandler(materialRaisedButton_Click);
            materialRaisedButton12.Click += new EventHandler(materialRaisedButton_Click);
        }
        public class subject
        {
            Form1 frm;
            public void changeTab(int tabPage/* , Form1 frm1 */, Form1 frm1)
            {
                frm = frm1;
                frm.TabControlSubjects.SelectTab(tabPage);
            }
        }
        const string buttonPrefix = "materialRaisedButton";
        private void materialRaisedButton_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            string name = button.Text;
            int number = int.Parse(name.Substring(buttonPrefix.Length));
            subject1.changeTab(number, this);
        }
    }
}​