Hi everybody,
i am newbie working on a small application using c# .net

problem:

i have two forms, form1 and fom2.
i have textbox in both forms.
when i click the submit button i want to get the value from a form1 textbox into the form2 textbox automatically.

code:

// in form2


private void textBox12_TextChanged(object sender, EventArgs e)
        {
            textBox12 = Admin_Calculator.textBox13.Text;
        }

Error:

An object reference is required for the non-static field, method, or property 'WindowsFormsApplication3.Form1.textBox13'

OR

is there any other alternative way to acheive this..
please help me with issue

Dani AI

Generated

The error you saw happens because the code tries to reach a control through the form class instead of through the actual form instance. was right: you need a reference to the Admin_Calculator instance that actually holds the textbox value. has two independent forms, so pick one of these safe patterns rather than making controls static or directly public.

A simple, recommended approach is to expose the result as a public property on the source form and pass that value when you create the display form. Example pattern:

public string CalculationResult { get { return txtResult.Text; } }

var display = new DisplayCalculator(this.CalculationResult);
display.Show();

And inside the display form constructor:

public DisplayCalculator(string initialText)
{
    InitializeComponent();
    txtDisplay.Text = initialText;
}

If the two forms are created independently (both shown from different places), get the running instance of the calculator with Application.OpenForms, read a public property, and set the target textbox:

var admin = Application.OpenForms.OfType<AdminCalculator>().FirstOrDefault();
if (admin != null)
    txtDisplay.Text = admin.CalculationResult;

Notes and cautions: prefer properties or methods over exposing control fields; avoid static controls except for quick throwaway tests. Use TryParse for numeric input to avoid exceptions. If you need the display to update automatically when a new calculation finishes, implement an event on the calculator form (raise it after computing) and have the display subscribe to that event so updates happen on the live instance rather than via static access.

Recommended Answers

All 4 Replies

How is your application set up?

Is Admin_Calculator the name of the Form or is it an instance of the form you have created? Do you create an instance of the form to show with the .Show() method?

Admin_Calculator is the name of the form from where i want to get the value into form2 textbox

To do what you're trying to do, you're probably going to need to create an instance of the Admin_Calculator class.

How do you open the forms right now? Do you click something on one of the forms that causes the other to open?

here's the code....

//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;

using System.Data.SqlClient;



namespace WindowsFormsApplication3
{
    public partial class Admin_Calculator : Form
    {
        public Admin_Calculator()
        {
            InitializeComponent();
        }

        private decimal MultiplyNumbers(decimal number1, decimal number2)
        {
            return number1 * number2;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            decimal value1 = decimal.Parse (SPOT.Text);
            decimal value2 = decimal.Parse (textBox2.Text);
            decimal returnValue1 = MultiplyNumbers(value1, value2);
            textBox13.Text = returnValue1.ToString();
       

    }
}

}

------------------------------------

//FORM2

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 WindowsFormsApplication3
{
    public partial class Display_Calculator : Form
    {
        public Display_Calculator()
        {
            InitializeComponent();
        }

        private decimal MultiplyNumbers(decimal number1, decimal number2)
        {
            return number1 * number2;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            decimal value1 = decimal.Parse(textBox12.Text);
            decimal value2 = decimal.Parse(textBox18.Text);
            decimal returnValue1 = MultiplyNumbers(value1, value2);
            textBox24.Text = returnValue1.ToString();
        }

        private void textBox12_TextChanged(object sender, EventArgs e)
        {
            textBox12 = Admin_Calculator.textBox13.Text;
        }
    }
}

these two forms are independent, there is no button on form1 which opens the form2...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.