c# Accessing WinForm control properties from another class

后端 未结 4 612
难免孤独
难免孤独 2021-01-22 02:58

How does one access WinForm controls such as ProgressBar properties from another class?

Please see my code below. I know this might not be the best option

4条回答
  •  无人及你
    2021-01-22 03:09

    I do not recommend to use this method, for simple reason as mentioned here by one of the comments. But if you really want to access that form control, here is how:

    1) Select that control and set its access modifier to internal. 2) Assume your form id is "Form1" and control id is "control1"

    Inside your method:

    Form1 form = (Form1)Application.OpenForms["Form1"];
    // form.control1  should now be available.
    

提交回复
热议问题