如何从另一个类访问winform组件?

时间:2022-11-28 15:59:56

i have a form with one button and two labels

我有一个有一个按钮和两个标签的表格

and i have a separate class called myCounter

我有一个单独的类,叫做myCounter

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

我希望myCounter类能够通过一个名为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
        }
    }
}

2 个解决方案

#1


4  

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.

请记住,这是我在wiki标记编辑器中编写的代码,没有经过测试。这应该对你有用。

#2


3  

Create a public method on your form for this.

为此在表单上创建一个公共方法。

public partial class Form1 : Form{
    public void SetLabelColor(Color color){
        mylabel.BackColor = color;
    }
    //... Other code
}

#1


4  

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.

请记住,这是我在wiki标记编辑器中编写的代码,没有经过测试。这应该对你有用。

#2


3  

Create a public method on your form for this.

为此在表单上创建一个公共方法。

public partial class Form1 : Form{
    public void SetLabelColor(Color color){
        mylabel.BackColor = color;
    }
    //... Other code
}