我试图从另一种形式访问公共财产,但我似乎无法以第二种形式访问它

时间:2015-10-06 03:17:57

标签: c# forms properties

在我的主要表单中,我设置了一个属性:

主要表格:

Int32 _pid1;
public Int32 pid1
{
    get { return _pid1; }
    set { _pid1 = value; }
}

单击按钮,我打开一个新表单,将主表单作为构造函数参数传递,如下所示:

private void admin_Click(object sender, EventArgs e)
{
    adminSettings adminW = new adminSettings(this);
    //adminW.ShowDialog();
}

在我的第二种形式中,我试图像这样访问该属性:

public adminSettings(Form mains)
{
    temp = mains.pid1;
    InitializeComponent();
}

在VS中我无法编译,因为它突出显示属性并显示错误:

  

' System.Windows.Forms.Form中'不包含的定义   ' PID1'没有扩展方法' pid1'接受第一个论点   键入System.Windows.Forms.Form'可以找到(你错过了吗?   使用指令或程序集   参考?)D:\ UI_still_in_progress \ user_interface \ Admin_screen.cs 67 25 user_interface

Intellisense也没有接受它。我已经探索了stackoverflow的答案,但到目前为止我似乎已经做了一切正确的事情。我在这里不知所措,任何帮助都会非常感激。

修改1:

这是我在主窗体中声明属性的地方:

public partial class mainForm : Form
{
    //constructor
    public mainForm()
    {
        InitializeComponent();
        //Add game titles here - The text must be referenced later in order to change game directory for exe file
        this.gameSelect.Items.Add("Bumblebee Game");
        this.gameSelect.Items.Add("Flight Simulator");
        this.gameSelect.DropDownStyle = ComboBoxStyle.DropDownList;  //read only    
        this.FormBorderStyle = FormBorderStyle.FixedSingle;
        this.MinimizeBox = false;
        this.MaximizeBox = false;
    }

    //properties
    Int32 _pid1;
    public Int32 pid1
    {
        get { return _pid1; }
        set { _pid1 = value; }
    }

    Int32 _did1;
    public Int32 did1
    {
        get { return _did1; }
        set { _did1 = value; }
    }

    Int32 _sid1;
    public Int32 sid1
    {
        get { return _sid1; }
        set { _sid1 = value; }
    }

    private void admin_Click(object sender, EventArgs e)
    {
        adminSettings adminW = new adminSettings(newLoginRequest, this);
        //adminW.ShowDialog();
    }
}

1 个答案:

答案 0 :(得分:4)

您正在将MainForm转换为基本的Windows窗体,该窗体不具有您的属性。在adminSettings构造函数中将其强制转换为主窗体的类名。

public adminSettings(Form1 mains)
{
    temp = mains.did1;
    InitializeComponent();
}

这假定您的MainForm类是Form1