2013-05-31 84 views
0

我有一个表格MainForm,然后一个窗体Form1的函数,声明如下:呼叫从另一种形式

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(); 
    } 
} 

所以Form1中只是使用Visual Studio工具创建的。

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"); 
     }   
    } 
} 

的问题是,当我尝试打电话东西“的MainForm”出现的对象引用错误。当然,txtSendKeys字段是公开的。

BTW,我可以轻松地做反向动作,就是像做

MainForm.textField = x 

从我的Form1上。

我严重缺乏有关C#的知识,但是这个人是我的工作,我现在开始绝望..

+0

什么是改革? – Zigma

+0

您在Form1类中对MainForm的引用在哪里? – KingCronus

+0

请参阅:http://stackoverflow.com/questions/6803970/access-form-component-from-another-class?rq=1 – KingCronus

回答

0

MainForm中是一种形式的定义,而不是它的一个实例。此外,您似乎并未在任何地方展示Form1,因此我现在已将其添加到MainForm.Load中,但显然您可以移动它。

你需要更多的尝试是这样的:

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"; 
    }   
} 

然后在您的主营:

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(); 
} 

这是确保您之前设置的“MyParentForm”属性非常重要你在Form1中使用它。如果不是,Form1将会抛出异常。更聪明的方式可能涉及检查财产不是第一次。

+0

谢谢你的帮助!然而,它引发了一个异常“对象引用没有定义到对象实例”,当涉及到“MyParentForm.txtSendKeys.Text”行 –

+0

嗨,在设计器中,确保MainForm.cs的Load事件设置为正确的方法。 – KingCronus

+0

花生价格!有用!在设计师中,我将“form1_load”放到“Load”事件中,并且它可以工作。多次感谢你! –

0

修改您MainForm类:

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

修改您Form1.button1_Click方法体:

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"); 
}