CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2011
    Posts
    3

    [RESOLVED] How to access Form1 controls from another class?

    Hi all c#corner forums,
    I'm trying to create my class, but I couldn't succeed to work this code. When I run program, newSerial.sendData() in the Form1.Load() (this is my function which is in my own class) works perfectly, but when I clicked the button (you can see the code, I'm calling the same function) program throws "NullReferenceException" error. What is the solution?

    Code:
    namespace robitSeri
    {
        
        public partial class Form1 : Form
        {
            public mySerial newSerial;
    
            private void Form1_Load(object sender, EventArgs e) 
            { 
                 mySerial newSerial = new mySerial(this);
                 newSerial.sendData();
     
                // THIS WORKS EXCELLENTLY..
             } 
            
             private void button1_Click(object sender, EventArgs e)
             {
                    newSerial.sendData();
                  // THIS CODE DOES NOT WORK!!!
              }
     
           
        }
    
        public class mySerial // this class created by me
        {
              private Form1 frm;
    
             public mySerial(Form1 newFrm)
            {
               this.frm=newFrm;
            }
    
             public void sendData()
            {
               frm.serialPort1.Write(sth);
            }
    
         
        }
    }

  2. #2
    Join Date
    Feb 2010
    Posts
    13

    Re: How to access Form1 controls from another class?

    I am unsure as to why this cannot work, but can you use the following code instead?

    Code:
    namespace robitSeri
    {
    
        public partial class Form1 : Form
        {
            public mySerial newSerial;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                mySerial newSerial = new mySerial(this.serialPort1);
                newSerial.sendData();
    
                // THIS WORKS EXCELLENTLY..
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                newSerial.sendData();
                // THIS CODE DOES NOT WORK!!!
            }
        }
    
        public class mySerial // this class created by me
        {
            private Form1 frm;
            private System.IO.Ports.SerialPort Port;
            public mySerial(System.IO.Ports.SerialPort serialPort)
            {
                Port = serialPort;
            }
    
            public void sendData()
            {
                Port.Write(sth);
            }
        }
    }

  3. #3
    Join Date
    Aug 2011
    Posts
    3

    Re: How to access Form1 controls from another class?

    Ok, I've found my mistake. In the Form_Load method I've wrote a decleration

    Code:
      private void Form1_Load(object sender, EventArgs e) 
            { 
                 mySerial newSerial = new mySerial(this);
               
             }
    but there is no need to be new decleration using mySerial (or object name). I've fixed code as below and now it's running perfectly.

    private void Form1_Load(object sender, EventArgs e)
    {
    newSerial = new mySerial(this);

    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured