0

I have a form MainForm and a form form1, declared like this:

namespace SdkDemo
{
    public partial class MainForm : Form
    {        
        public CoreWrapper _icCore;        
        private Dictionary<string, int> audioDevices;

        #region MainForm Ctor

        public MainForm()
        {
            InitializeComponent();
            _icCore.Start();
        }

        public Form1 form1 = new Form1();
    }
}

So form1 is just created with Visual Studio tools.

The whole code of form1:

namespace SdkDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dial = Registry.CurrentUser.OpenSubKey("SOFTWARE").OpenSubKey("CS").GetValue("DIAL").ToString();

            // Error "An object reference is expected":
            MainForm.txtSendKeys.Text = ("esdcze");
        }        
    }
}

The problem is that an object reference error appears when i try to call something in "Mainform". Of course, the txtSendKeys field is public.

BTW, I can easily do the reverse action, which is to do something like

MainForm.textField = x

from my form1.

I seriously lack of knowledge about C#, but this one is for my work and I start to desperate by now..

KingCronus
  • 4,509
  • 1
  • 24
  • 49
Grigory Kornilov
  • 366
  • 2
  • 4
  • 17

2 Answers2

0

MainForm is the definition of a form, not an Instance of it. Also you don't appear to be showing Form1 anywhere, so I have added that to MainForm.Load for now, but obviously you could move that.

You need to try something more like this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();            
    }

    public MainForm MyParentForm { get; set; }       

    private void button1_Click(object sender, EventArgs e)
    {
        string dial = Registry.CurrentUser.OpenSubKey("SOFTWARE").OpenSubKey("CS").GetValue("DIAL").ToString();

        // Use the reference to MyParentForm
        MyParentForm.txtSendKeys.Text = "esdcze";
    }        
}

Then in your Main:

public partial class MainForm : Form
{        
    public CoreWrapper _icCore;        
    private Dictionary<string, int> audioDevices;

    #region MainForm Ctor

    public MainForm()
    {
        InitializeComponent();
        _icCore.Start();
    }

    private void MainForm_Load(object sender, System.EventArgs e)
    {
        form1.MyParentForm = this;
        form1.Show();
    }

    public Form1 form1 = new Form1();
}

It is very important to make sure that you set the "MyParentForm" property before you use it in Form1. Form1 will throw an exception if not. A more clever way might involve checking the property isn't Null first.

KingCronus
  • 4,509
  • 1
  • 24
  • 49
0

Modify your MainForm class:

public partial class MainForm : Form
{
    public CoreWrapper _icCore;
    private Dictionary<string, int> audioDevices;

    public MainForm()
    {
        InitializeComponent();
        _icCore.Start();
        // sets the form1's owner form to the MainForm instance
        form1.Owner = this;
        // show the Form1 form
        form1.Show();
    }

    public Form1 form1 = new Form1();

    internal void SetSendKeys(string value)
    {
        txtSendKeys.Text = value;
    }
}

Modify your Form1.button1_Click method's body:

private void button1_Click(object sender, EventArgs e)
{
    string dial = Registry.CurrentUser.OpenSubKey("SOFTWARE").OpenSubKey("CS").GetValue("DIAL").ToString();
    var mainForm = (MainForm)this.Owner;
    mainForm.SetSendKeys("esdcze");
}
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78