在单独的表单上写入文本框(C#)

Writing to a textbox on a separate form (C#)(在单独的表单上写入文本框(C#))
本文介绍了在单独的表单上写入文本框(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个表格:Form1 和 Form2.Form2 有一个名为 text1 的文本控件

Suppose I have two forms: Form1 and Form2. Form2 has a text control named text1

在 VB6 中,我可以写入 Form 2 的文本框

In VB6 I could write to Form 2's textbox

使用 Form1 控制:Form2.Text1.text = "Some text here"

control from Form1 by using: Form2.Text1.text = "Some text here"

如何在 C# 中完成这项工作?我什么都试过了!

How can I make this work in C#? I have tried everything!

我尝试过的:

Form2 Frm2 = new Form2();
Frm2.show();
Frm2.Activate(); // Trying everything to make sure it sees the form (it does).

Frm2.Text1 (Doesn't find the control)...

回答:

我最终在 Form2 中创建了一个公共例程,然后只从 form1 调用这个例程.在 Form2 的这个公共例程中,我会调用文本框!

I ended up making a public routine in Form2, and then just calling this routine from form1. In this public routine of Form2 I would then call the textbox!

推荐答案

我的目标是将文本添加到另一个表单的文本框中.我有Form1和Form2.Form2 有一个名为 Text1 的文本框控件.为了完成这项工作,我创建了一个子例程:

My goal was add text to a Text Box on another form. I had Form1 and Form2. Form2 has a text box control named Text1. In order to make this work, I created a Sub Routine:

public Void WriteToText(string sData)
{
// Here is where I wrote to my textbox
Text1.text = sData;
}

<小时>

表格1代码:


Form 1 code:

Form2 Frm2 = new Form2();
Frm2.WriteToText("My Data");

这篇关于在单独的表单上写入文本框(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)