How to access form methods and controls from a class in C#?

前端 未结 8 1087
轮回少年
轮回少年 2020-11-30 07:52

I\'m working on a C# program, and right now I have one Form and a couple of classes. I would like to be able to access some of the Form controls (s

8条回答
  •  情歌与酒
    2020-11-30 08:10

    I'm relatively new to c# and brand new to stackoverflow. Anyway, regarding the question on how to access controls on a form from a class: I just used the ControlCollection (Controls) class of the form.

            //Add a new form called frmEditData to project.
            //Draw a textbox on it named txtTest; set the text to
            //something in design as a test.
            Form frmED =  new frmEditData();
            MessageBox.Show(frmED.Controls["txtTest"].Text);
    

    Worked for me, maybe it will be of assistance in both questions.

提交回复
热议问题