C# 什么';在c语言中,使用不同的按钮执行重复操作的最佳方法是什么#
这是我当前的代码:C# 什么';在c语言中,使用不同的按钮执行重复操作的最佳方法是什么#,c#,button,recurring,C#,Button,Recurring,这是我当前的代码: subject subject1; string DocPath = AppDomain.CurrentDomain.BaseDirectory + "Documents/"; public Form1() { InitializeComponent(); subject1 = new subject(); } public class subj
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#的新手,说话的时候还在学习。欢迎提供任何建议/提示。
谢谢阅读。试试这个。我从按钮的文本名称中提取按钮的编号
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);
}
}
}
是的,将所有按钮映射到同一个单击事件,并执行一些逻辑以获得所需的整数。谢谢!但最后一件事,你能给我解释一下
as
在Button-Button=sender-as-Button中的作用吗代码>?只需将发送者强制转换为按钮对象。