我正在尝试从不同的形式访问 public 属性 但是我似乎无法以第二种形式访问它
I'm trying to access a public property from a different form however I can't seem to access it in the second form
在我的主窗体中,我设置了 属性:
主要形式:
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' does not contain a definition for
'pid1' and no extension method 'pid1' accepting a first argument of
type 'System.Windows.Forms.Form' could be found (are you missing a
using directive or an assembly
reference?) D:\UI_still_in_progress\user_interface\Admin_screen.cs 67 25 user_interface
Intellisense 也没有接受它。我已经探索了 Whosebug 的答案,但到目前为止,我似乎做的一切都是正确的。我在这里不知所措,将不胜感激。
编辑 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();
}
}
您正在将 MainForm 转换为基本的 Windows 表单,该表单不会包含您的 属性。将其转换为 adminSettings 构造函数中主窗体的 class 名称。
public adminSettings(Form1 mains)
{
temp = mains.did1;
InitializeComponent();
}
这假设您的 MainForm 的 class 是 Form1
在我的主窗体中,我设置了 属性:
主要形式:
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' does not contain a definition for 'pid1' and no extension method 'pid1' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?) D:\UI_still_in_progress\user_interface\Admin_screen.cs 67 25 user_interface
Intellisense 也没有接受它。我已经探索了 Whosebug 的答案,但到目前为止,我似乎做的一切都是正确的。我在这里不知所措,将不胜感激。
编辑 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();
}
}
您正在将 MainForm 转换为基本的 Windows 表单,该表单不会包含您的 属性。将其转换为 adminSettings 构造函数中主窗体的 class 名称。
public adminSettings(Form1 mains)
{
temp = mains.did1;
InitializeComponent();
}
这假设您的 MainForm 的 class 是 Form1