Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how to adjust textbox property in a form from another class?

i still cant get how to access the controls in the form from another class.im new to c# so my "try and error" method is not actually working.

can anyone give me a simple guide?

this is my code snippet

Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            print pr = new print();
            pr.p();

        }

    }
}

This is class print:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication2
{
    class print 
    {
        public print()
        {

        }

        public void p()
        {
            Form1 f = new Form1();
            f.textBox1.Text = "change text";

        }     

    }

}

as you can see, im trying to change the textBox1 property in the print class.but when i compile it,im thrown with a System.StackOverflowException!

im getting frustrated now because i cant access it from another class.

can anyone give me a step by step on how to call it from the print class?i tried many steps from the internet but i just cant get it.

btw, i did make the modifier for the textbox to public.

like image 962
Psychocryo Avatar asked Jan 20 '23 00:01

Psychocryo


2 Answers

The problem is that print.p method creates a new form, and the form's constructor calls the print.p method again. Which then creates another new form, whose constructor calls the print.p method again, loop over and over. That's where the StackOverflowException is coming from.

The solution is to get a book that teaches object-oriented programming. I know that probably sounds snarky or unhelpful, but none of the answers you'll get on a Q&A website can adequately explain to you why what you're trying to do is a bad idea. External helper classes like print should not be able to mess with private members of your form class. Instead, you should call public methods on your form class that make these modifications.

The quick-and-dirty fix requires that you figure out a way to get a reference to the instance of your form class in the print.p method. That's the only way you're going to be able to call methods on that object, rather than creating a new object of that class. One way is to pass a reference in to the method as a parameter:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        print pr = new print();
        pr.p(this);
    }
}

class print 
{
    public print()
    {

    }

    public void p(Form frm)
    {
        frm.textBox1.Text = "change text";
    }     
}

Also, as a somewhat irrelevant stylistic note, you should be aware that almost all coding guidelines/conventions for C# (and the .NET platform) require that the names of classes and methods be PascalCased, rather than camelCased. So, your print class should actually be named Print.

like image 58
Cody Gray Avatar answered Jan 30 '23 06:01

Cody Gray


You are getting a StackOverFlowException because you create a new instance of print in Form1 using this code:

print pr = new print();

Then in the constructor in print, you create a new instance of Form1 like this:

Form1 f = new Form1();

which creates a new instance of print, which creates a new instance of Form1, which creates a new instance of print, which creates a new instance of Form1, which...

You have created an infinite loop, which kills the Stack.

like image 30
Mikael Östberg Avatar answered Jan 30 '23 08:01

Mikael Östberg