- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我仍在学习 C#,如果这是一个愚蠢的问题,请原谅我,也请原谅我的长篇文章,因为我不确定如何说这个,而不是“长”的方式。
我的场景:
我正在使用 DockPanel Suite对于我的应用程序,我有一个 ParentForm
、一个 NotesForm
和 BrowserForm
(我实际上还有很多 BrowserForm
类型,但如果我能让它对一个工作,我应该能够让它对其他人工作)
因此 ParentForm
在应用程序启动时加载,当它加载时加载 NotesForm
和 BrowserForm
通过按钮单击事件从ParentForm
,然后该实例打开并保持打开状态,不会关闭。
因此,当我在 NotesForm
中输入信息时,我已经拥有了所有 3 种形式的已建立实例。 (我认为这就是问题所在)
我的目标是将在 NotesForm
上的 textBox1
中键入的信息获取到一个字符串变量,让我们在 上将其称为
而无需单击 notesText
>BrowserFormNotesForm
上的任何内容,我认为这将通过 TextChanged
事件进行。
我目前能够使用属性(获取、设置)将信息传递到表单,但我必须单击 NotesForm
和 BrowserForm
上的按钮让它发生。
我包括了一种简化的代码来展示我目前是如何做的。我的完整代码非常大,所以只是想避免混淆并展示我正在进行的工作的概念。
Parent Form
public partial class parentForm : Form
{
private notesForm notesForm = new notesForm();
public parentForm()
{
InitializeComponent();
}
private void parentForm_Load(object sender, EventArgs e)
{
notesForm.Show(mainDock, DockState.DockLeft);
}
private void tb1_Click(object sender, EventArgs e)
{
BrowserForm.notesText= notesForm.passInfo;
}
NotesForm
public partial class notesForm : DockContent
{
private string _passInfo;
public notesForm()
{
InitializeComponent();
}
public string passInfo
{
get { return textBox1.Text; }
set
{
_passInfo = value;
textBox1.Text = _passInfo;
}
}
}
BrowserForm
public partial class BrowserForm : DockContent
{
private string passInfo;
public BrowserForm()
{
InitializeComponent();
}
public string passInfo
{
get
{
return _passInfo;
}
set
{
_passInfo = value;
notesText = _passInfo;
}
}
string notesText;
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(notesText);
}
}
}
所以这目前有效,但要使其正常工作,我必须在父表单上单击一个按钮,然后在浏览器表单上单击一个按钮。理想情况下,我需要它在您在注释表单的文本框中完成输入后将该信息传递给浏览器表单。如果有帮助,输入到文本框中的信息是静态的,长度始终为 9 个字符,由数字(电话号码)组成。
还有我的应用程序的一些背景。我在工作中使用它来访问我们内联网中的各种工具。它会自动让您登录,将它们整合到一个应用程序中,而不是打开 40 - 80 个不同的浏览器窗口,并且最终(希望)会自动从这些工具中抓取一些信息,这样我们就不必搜索页面了。该工具将自动拉动它。这不是恶意的,我不是垃圾邮件发送者,也不是我为 10 家不同的公司提供 ISP 技术支持的任何事情,每家公司都有 8-10 种服务工具。
任何与此有关的帮助将不胜感激,或者如果有更简单的方法来处理我正在做的事情,我也很乐意听到。另外,如果您想查看我的完整代码(如果有帮助),请告诉我,我不介意发布它,因为它非常麻烦。
最佳答案
我不确定你的问题是否正确。我假设您想将信息从一个子表单传递到另一个子表单。
这样做的一种方法是这样的:
<强>1。创建一个这样的界面:
public interface IDataContainer
{
void SetData(String data);
}
<强>2。让您的 parentForm
实现您的接口(interface):
public partial class parentForm : Form, IDataContainer
并在表单类中为该方法提供一个实现。这个简单的接口(interface)将使实现者能够出于任何目的存储(字符串)数据。
在您的情况下,SetData
方法的实现将充当桥梁,将 notesForm
连接到您的 BrowserForm
,在表格,以干净的方式:
public partial class parentForm : Form, IDataContainer
[...]
public void SetData(String dataComingFromOtherSource)
{
this.browserFormInstance.passInfo = dataComingFromOtherSource;
}
[...]
<强>3。将类型为 IDataContainer
的参数添加到每个 child-form 的构造函数中,以便您可以将引用传递给 IDataContainer
实现(那将是你的 parentForm
)。这样:
public partial class parentForm : Form
{
private notesForm notesForm = null;
public parentForm()
{
InitializeComponent();
// Note that at this point you are instantiating
// the child-form and you are passing a reference
// to the `parentForm` so that you can pass information
// to it, and from it to whatever other form you may
// need, in an elegant way.
this.notesForm = new notesForm(this);
}
<强>4。最后,在 notesForm
中,将设置数据的逻辑放入您的 container
:
public partial class notesForm : DockContent
{
// On runtime this will in fact the `parentForm` object instance.
IDataContainer myDataContainerInstance = null;
public notesForm(IDataContainer myDataContainerInstance) : base()
{
InitializeComponent();
// Assign the reference so that you can use it later
this.myDataContainerInstance = myDataContainerInstance;
textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.myDataContainerInstance.SetData(textBox1.Text);
}
}
注意:拥有静态类/单例也是一种解决方案,顺便说一句,更简单......
关于c# - 在 TextChanged 事件中将信息从打开的表单传递到打开的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18502214/
最近有人问我是否可以在 Excel VBA 中制作一个宏,允许用户输入两个数字并让它自动放到下一行。这样做的目的是让他们可以一次输入两个数字的测试成绩,而无需按回车键,因为他们不擅长打字。 当我第一次
我在 Windows Phone 上的应用程序中有三个 TextBox。我想在用户在其中任何一个中插入输入后立即更改这三个 TextBox 的数值。 我的 xaml 看起来像这样: 我在 C#
我有 CalendarExtender 并将其绑定(bind)到文本框,但是当我在 textchanged 事件中编写代码时发现它没有触发;这里是我的代码片段: 代码隐藏: protected vo
我在做一个项目。我有文本框,通过 TextChanged 事件我将文本更改为大写 txt.ToUpper(); 然后 selectionStart 从头开始。我将其更改为 textbox.text
我仍然是 Android 开发的新手,我想做的是创建一个监听器,它将包含两个 TextView 对象,它们分别保存面积和周长。宽度和高度是 EditText 对象。输入宽度和高度后,应根据 calcA
我有一个文本框,它有一个相当庞大的 _TextChanged 事件处理程序。在正常打字条件下,性能还可以,但当用户执行长时间的连续操作时,性能会明显滞后,例如按住退格键一次删除大量文本。 例如,事件用
在尝试做一些更复杂的事情时,我遇到了一种我不太了解的行为。 假设下面的代码处理 textChanged 事件。 private void textChanged(object sender, Tex
我正在关注 M-V-VM我的 WPF UI 的模式。我想将文本框的 TextChanged 事件的命令连接到我的 ViewModel 类中的命令。我能想到的完成此任务的唯一方法是从 TextBox 控
我有一个简单的 aspx 文件 这是 test4.aspx.vb 代码文件 Partial Class test4 Inhe
我正在使用绑定(bind)到对象的基本文本框。基本上,我想要做的是每次编辑框中的文本并且用户取消选择框或编辑底层绑定(bind)数据时调用一个方法。我现在使用的是 TextChanged 事件,但这有
我的 TabControl 页面中有一个 RichTextBox。请注意,RichTextBox 是使用以下代码以编程方式创建的: TabPage addedTabPage = new TabPage
这是我的第一个问题。我接受过非常基本的 Java 培训,现在正在 Android Studio 中开发一个简单的应用程序。该应用程序是一个温度转换器,它读取一个文本字段,并在应用一种方法后,它实时在另
我有一个 TextChanged我的事件 ComboBox喜欢; private void comboBox1_TextChanged(object sender, EventArgs e) {
我仍在学习 C#,如果这是一个愚蠢的问题,请原谅我,也请原谅我的长篇文章,因为我不确定如何说这个,而不是“长”的方式。 我的场景: 我正在使用 DockPanel Suite对于我的应用程序,我有一个
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我刚开始使用 C# 和 Visual Studio,但遇到了问题。 我正在尝试使用 TextChanged 事件计算三个不同乘法的总值,因此每次我在用于乘法的文本框中输入数字时,总文本框都会更新。 t
所以我想做的是能够连续输入所有 10 个文本框。我从第一个开始,然后在输入 3 个字符后将焦点移至下一个,并在输入 3 个字符后将焦点移至下一个。 我已经完成了那部分,但是如果第二个文本框中已经有文本
我想知道是否有可能制作KeyDown TextChanged 之前的事件触发? 我为一个简单的 TextBox 添加了这两个事件现在我想检查是否 Delete键被按下。如果是, , 如果没有 .我创
我目前正在处理一个项目,但出于某种原因,我的文本框的 textchanged 事件没有触发。我试图在我的代码中放置一个断点,但他没有到达那里,所以他的事件没有触发 imo。
我在 xml 中制作了一个不可见的按钮,我想在我的 EditText 中创建某个字符串值时再次使按钮可见。当使用 if 语句满足值时,我使用了 TextWatcher 检查。然而,当显示按钮的代码被执
我是一名优秀的程序员,十分优秀!