简体   繁体   English

如何公开公开控件的属性?

[英]How do I expose a control's property publicly?

I want to expose the Text property of a textbox on Form1 to Form2 so Form2 can set the text in the textbox on Form1.我想将 Form1 上文本框的 Text 属性公开给 Form2,以便 Form2 可以在 Form1 上的文本框中设置文本。 I've read how to do it but it doesn't work so I must be doing something wrong.我已经阅读了如何做,但它不起作用,所以我一定是做错了什么。

Here's the code for Form1 including the declaration of the public property (TextInputText is the property, txtInput is the textbox):这是 Form1 的代码,包括公共属性的声明(TextInputText 是属性,txtInput 是文本框):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public string TextInputText
        {
            get => txtInput.Text;
            set => txtInput.Text = value;
        }

        public Form1()
        {
            InitializeComponent();                       
        }

        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            // If enter is pressed clear the textbox, but update() the history first

            if (e.KeyCode == Keys.Enter)
            {
                TextHistory.Update(txtInput.Text);
                txtInput.Text = "";
            }
        }

        private void HistoryButton_Click(object sender, EventArgs e)
        {
            Form2 HistoryForm = new Form2();
            HistoryForm.Show();
        }
    }
}

The problem is Form2 still can't see the property, or I don't know how to access it, what am I doing wrong?问题是 Form2 仍然看不到属性,或者我不知道如何访问它,我做错了什么?

Either inject Form2 with a reference to Form1 when you create it:在创建Form2时注入对Form1的引用:

private void HistoryButton_Click(object sender, EventArgs e)
{
    Form2 HistoryForm = new Form2(this);
    HistoryForm.Show();
}

This requires you to define a custom constructor in Form2 that accepts a Form1 reference.这要求您在Form2中定义一个接受Form1引用的自定义构造函数。 You can then use this reference to access the property:然后,您可以使用此引用访问该属性:

private readonly Form1 _form1;
public Form2(Form1 form1)
{
    InitializeComponent();
    _form1 = form1;

    string text = _form1.TextInputText;
}

Another approach is to use the Application.OpenForms property to get a reference to Form1 in Form2 :另一种方法是使用Application.OpenForms属性在Form2获取对Form1的引用:

var form1 = Application.OpenForms.OfType<Form1>().FirstOrDefault();
string text = form1.TextInputText;

You do not give Form2 a reference to the Form1 instance:你没有给 Form2 一个对 Form1 实例的引用:

Form2 HistoryForm = new Form2();

How could you access a Instance Function, Property or Value, without a Instance?如果没有实例,您如何访问实例函数、属性或值? A static property would not make sense.静态属性没有意义。 So the most likely option is to give Form2 a constructor that takes a Form1 reference as Argument.因此,最可能的选择是为 Form2 提供一个将 Form1 引用作为 Argument 的构造函数。 Store that reference somewhere in Form2.将该引用存储在 Form2 中的某处。 Then call the constructor like this:然后像这样调用构造函数:

Form2 HistoryForm = new Form2(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM