【发布时间】:2020-04-11 05:28:26
【问题描述】:
我想将 Form1 上的文本框的 Text 属性公开给 Form2,以便 Form2 可以在 Form1 上的文本框中设置文本。我已经阅读了如何做到这一点,但它不起作用,所以我一定是做错了什么。
这是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();
}
}
}
问题是Form2仍然看不到该属性,或者我不知道如何访问它,我做错了什么?
【问题讨论】:
-
很难说你做错了什么而不看你是如何尝试访问它的。
-
看不到 Form1 和 Form2 是如何交互的。您是否遵循这些帖子中的任何一个的设计模式? stackoverflow.com/questions/39026518/…stackoverflow.com/questions/1559770/…
-
我尝试过使用 FormName.TextInputText,我认为这就是您访问它的方式。
-
你需要给 Form2 一个对 Form1 的引用。 Form2 还能如何访问有关 Form1 的任何信息?
-
请分享一个尝试访问它的代码示例。这段代码在我看来没问题,所以问题可能就在那里。
标签: c# .net properties public