0

This is my current code:

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

Is there a more efficient way to do this? Because if i look at it now it seems like there could be a better way to do this. I'm very new at C# and I'm still learning as I speak. Any advice/tips is welcome. thank you for reading.

s. bou
  • 3
  • 1

1 Answers1

0

Try this. I extracting the number of the button from the text name of the button.

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

    }

}​
jdweng
  • 33,250
  • 2
  • 15
  • 20