user3204017
user3204017

Reputation: 51

Can't set TextBox value from other class despite having created an object of Form1

I want to set the TextBox value from another class, though can't access the textBox from different classes. So I created an object of Form1 with a method to set the TextBox value, yet it still doesn't set it. I get no error messages.

Form1 Class:

public partial class Form1 : Form
{
    string text;

    public Form1()
    {
        InitializeComponent();
    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        text = richTextBox1.Text.ToLower();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        CalculateVowels cal = new CalculateVowels(text);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    public void SetText (string text)
    {
        textBox1.Text = text;          //This doesn't work
    }
}

CalculateVowels Class:

class CalculateVowels
{
    public CalculateVowels(string text)
    {
        int counter = 0;
        int l = text.Length;
        for (int i = 0; i < l; i++)
        {
            if (text[i] == 'a' || text[i] == 'e' || text[i] == 'i' || text[i] == 'o' || text[i] == 'u')
            {
                counter++;
            }
        }
        outings(counter);
    }

    public void outings(int vowels)
    {
        string a = vowels.ToString();
        Form1 f = new Form1();
        f.SetText(a);
    }
}

Upvotes: 0

Views: 353

Answers (2)

Habib
Habib

Reputation: 223412

You need to access the already open form instance. You can either pass the form instance like the answer from John Saunders or you can access the open forms through Application.OpenForms property. Like:

public void outings(int vowels)
{
    string a = vowels.ToString();
    var form1 = Application.OpenForms.OfType<Form1>().FirstOrDefault();
    if (form1 != null) // form already open
    {
        form1.SetText(a);
    }  
}

Upvotes: 0

John Saunders
John Saunders

Reputation: 161831

You are creating and setting different instance of the form from the one you clicked the button on. You would have to set the text of the same instance of the form.

You would have to do something like this:

private void button1_Click(object sender, EventArgs e)
{
    CalculateVowels cal = new CalculateVowels(this, text);
}

class CalculateVowels
{
    public CalculateVowels(Form1 f, string text)
    {
        int counter = 0;
        int l = text.Length;
        for (int i = 0; i < l; i++)
        {
            if (text[i] == 'a' || text[i] == 'e' || text[i] == 'i' || text[i] == 'o' || text[i] == 'u')
            {
                counter++;
            }
        }
        outings(f, counter);
    }

    public void outings(Form1 f, int vowels)
    {
        string a = vowels.ToString();
        f.SetText(a);
    }
}

That said, I believe I would not do this work in the constructor of CalculateVowels. Instead, I would create a static method of CalculateVowels, and do the work there:

public static void DoCalculation(Form1 f, string text) {...}

Upvotes: 1

Related Questions