[英]I'm trying to access a public property from a different form however I can't seem to access it in the second form
In my main form I have a property being set: 在我的主要形式中,我设置了一个属性:
Main Form: 主要形式:
Int32 _pid1;
public Int32 pid1
{
get { return _pid1; }
set { _pid1 = value; }
}
On a button click, I open a new form, pass the main form as a constructor parameter like this: 单击按钮后,我打开一个新表单,将主表单作为构造函数参数传递,如下所示:
private void admin_Click(object sender, EventArgs e)
{
adminSettings adminW = new adminSettings(this);
//adminW.ShowDialog();
}
In my second form, I try to access the property like this: 在第二种形式中,我尝试像这样访问属性:
public adminSettings(Form mains)
{
temp = mains.pid1;
InitializeComponent();
}
In VS I can't compile because it highlights the property and shows error: 在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
'System.Windows.Forms.Form'不包含'pid1'的定义,并且找不到扩展方法'pid1'接受类型为'System.Windows.Forms.Form'的第一个参数(您是否缺少using指令?还是程序集引用?)D:\\ UI_still_in_progress \\ user_interface \\ Admin_screen.cs 67 25 user_interface
Intellisense also doesn't pick up on it. Intellisense也不接受。 I've explored stackoverflow for an answer but so far it seems I've done everything correct.
我已经探索了stackoverflow来寻找答案,但是到目前为止,看来我所做的一切都正确。 I'm at a loss here, any help would be much appreciated.
我在这里很茫然,任何帮助将不胜感激。
Edit 1: 编辑1:
This is where I have declared the properties in the main form: 这是我以主要形式声明属性的地方:
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();
}
}
You are casting your MainForm to a basic Windows Form which will not have your property. 您正在将MainForm强制转换为基本Windows窗体,该窗体将不具有您的属性。 Cast it to the class name of your main form in your adminSettings constructor.
将其转换为adminSettings构造函数中主窗体的类名称。
public adminSettings(Form1 mains)
{
temp = mains.did1;
InitializeComponent();
}
This assumes that your MainForm's class is Form1 假设您的MainForm的类为Form1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.