Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

c# - how to access winform components from another class?

i have a form with one button and two labels

and i have a separate class called myCounter

i want the myCounter class to be able to access the labels in the form through a method called changeColor..

how can make the labels available in this class

the form

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public Color colTurn
        {
            get { return lblp1Turn.BackColor; }
            set { lblp1Turn.BackColor = value; }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }

the class

class myCounter
{
    private readonly Form1 Board;
    public myCounter(Form1 Board)
    {
        this.Board = Board;
    }

    public int turn = 0;


    public void changeColor()
    {

        if (turn == 0)
        {
            turn = 1;
            lbl

           //change color code here
        }
    }
}
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

So it looks like you're passing the whole form into your second class anyway, So I'd do what LightStriker suggested. Make a public accessor for all of your items and then set it in your other class.

public partial class Form1 : Form
    {
        private myCounter _counterClass;
        public Form1()
        {
            InitializeComponent();
        }

        public Label MyLabel1
        {
            get {return mylabel1;}
        }

        public Label MyLabel2
        {
            get {return mylabel2;}
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _counterClass = new myCounter(this);
        }

        protected void ButtonClick(object sender, EventArgs e)
        {
            _counterClass.changeColor();
        }
    }

Then in your second class you have access to your Label.

class myCounter
{
    private readonly Form1 Board;
    public myCounter(Form1 Board)
    {
        this.Board = Board;
    }

    public int turn = 0;


    public void changeColor()
    {

        if (turn == 0)
        {
            turn = 1;
            Board.MyLabel1.BackColor = Color.Red;
            Board.MyLabel2.BackColor = Color.White;
        }
        else
        {
            turn = 0;
            Board.MyLabel2.BackColor = Color.Yellow;
            Board.MyLabel1.BackColor = Color.White;
        }
    }
}

Keep in mind this is code I have written in a wiki markup editor and is untested. This SHOULD work for you though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...