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

前端 未结 8 1033
轮回少年
轮回少年 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 07:59

    You are trying to access the class as opposed to the object. That statement can be confusing to beginners, but you are effectively trying to open your house door by picking up the door on your house plans.

    If you actually wanted to access the form components directly from a class (which you don't) you would use the variable that instantiates your form.

    Depending on which way you want to go you'd be better of either sending the text of a control or whatever to a method in your classes eg

    public void DoSomethingWithText(string formText)
    {
       // do something text in here
    }
    

    or exposing properties on your form class and setting the form text in there - eg

    string SomeProperty
    {
       get 
       {
          return textBox1.Text;
       }
       set
       {
          textBox1.Text = value;
       }
    }
    
    0 讨论(0)
  • 2020-11-30 08:00

    If the form starts up first, in the form Load handler we can instantiate a copy of our class. We can have properties that reference whichever controls we want to reference. Pass the reference to the form 'this' to the constructor for the class.

    public partial class Form1 : Form
    {
        public ListView Lv
        {
            get { return lvProcesses; }
        }
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            Utilities ut = new Utilities(this);
        }
    }
    

    In your class, the reference from the form is passed into the constructor and stored as a private member. This form reference can be used to access the form's properties.

    class Utilities
    {
        private Form1 _mainForm;
        public Utilities(Form1 mainForm)
        {
            _mainForm = mainForm;
            _mainForm.Lv.Items.Clear();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 08:09

    You need to make the members in the for the form class either public or, if the service class is in the same assembly, internal. Windows controls' visibility can be controlled through their Modifiers properties.

    Note that it's generally considered a bad practice to explicitly tie a service class to a UI class. Rather you should create good interfaces between the service class and the form class. That said, for learning or just generally messing around, the earth won't spin off its axis if you expose form members for service classes.

    rp

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 08:11

    Another solution would be to pass the textbox (or control you want to modify) into the method that will manipulate it as a parameter.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            TestClass test = new TestClass();
            test.ModifyText(textBox1);
        }
    }
    
    public class TestClass
    {
        public void ModifyText(TextBox textBox)
        {
            textBox.Text = "New text";
        }
    }
    
    0 讨论(0)
  • 2020-11-30 08:18

    JUST YOU CAN SEND FORM TO CLASS LIKE THIS

    Class1 excell = new Class1 (); //you must declare this in form as you want to control
    
    excel.get_data_from_excel(this); // And create instance for class and sen this form to another class
    

    INSIDE CLASS AS YOU CREATE CLASS1

    class Class1
    {
        public void get_data_from_excel (Form1 form) //you getting the form here and you can control as you want
        {
            form.ComboBox1.text = "try it"; //you can chance Form1 UI elements inside the class now
        }
    }
    

    IMPORTANT : But you must not forgat you have declare modifier form properties as PUBLIC and you can access other wise you can not see the control in form from class

    0 讨论(0)
提交回复
热议问题