简体   繁体   English

我正在尝试从其他形式访问公共财产,但是我似乎无法以第二种形式访问它

[英]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.

相关问题 父窗体无法访问子窗体公共属性-Winforms C# - Parent Form can't access Child Form Public Property - Winforms c# 为什么我不能在静态类中访问公共属性字段 - Why can't I access public property field in static class 为什么我不能访问表单的公共System.Windows.Forms.BindingSource? - Why can't I access the public System.Windows.Forms.BindingSource of a form? 无法访问第二个表单标签 - Can't Access the Second Form Label 当我处于其他形式时,如何从一种形式访问“标签中的文本”? - How can I access the Text in a Label from one form while I am in a different form? 我正在尝试从第二个表单中将带有标题,描述的用户输入添加到我的数据数组,但是无法 - I am trying to add user input from the second form with title, description to my Data array but can't 我如何继承表格和从母表格访问控件? - How can I inherit form and access controls from mother form? 我正在尝试使用公共Form1()中公共void Form1_KeyDown()创建的字符 - I'm trying to use a char created by public void Form1_KeyDown() in public Form1() 我如何从C#中同一项目下的不同Windows窗体访问不同的组件? - How can i access the different component from different windows form which are under same project in c#? 如何访问主窗体公共属性WPF - How to access Main Form Public Property WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM