如何从C#中的另一个表单中清除文本框? [英] How to clear textbox from another form in C#?

查看:35
本文介绍了如何从C#中的另一个表单中清除文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击另一个表单上的按钮时,我试图从 Visual C# 中的另一个表单中清除我的文本框,但没有任何效果.我已经在 VB.Net 中轻松完成了这项工作,但在 Visual C# 中我做不到.告诉我一个在 (WinForm) 中做到这一点的简单方法.我使用的是 .Net Framework 4.8.

I'm trying to clear my textBox from another form in Visual C# when I click a button on another form, but nothing is working. I have done this is VB.Net with ease but in Visual C# I can't do it. Tell me an easy way to do that in (WinForm). I'm using .Net Framework 4.8.

这是我在 VB.Net 中使用的代码.告诉我如何在 C# 中做到这一点.

This is my code which I used in VB.Net. Tell me how to do this in C#.

注意:form1 是我的 textBox1 所在的表单,form2 是我的按钮所在的表单,我希望当我单击该按钮时 textBox1 文本变为空.

Note: form1 is a form where my textBox1 is present and form2 is a form where my button is present and I want that when I click that button textBox1 text become empty.

form1.textBox1.Text = ""

推荐答案

通常,我在嵌入式系统上使用 C.我是 OOP 和 C# 的新手.我找到了 2 个解决方案,我知道它们是否正确,但我希望它可以解决您的问题.

Normally, I am working with C on embedded system. I am newbie at OOP and C#. I found 2 solutions Idk they are proper way or not but I hope it solve your problem.

解决方案 1:

转到您的 WinForm 并添加以下代码以生成稍后调用您的表单的引用

Go to your WinForm and add the code below to produce a reference to call your Form later

public partial class Form1:Form 
{
    public static Form1 form;
    public Form1()
    {
      form = this;
      InitializeComponent();
    }
}

现在,转到您的其他表单并尝试此操作

Now, go to your other Form and try this

Form1.form.textBox1.Clear();

解决方案 2:

我尝试使用 ref 调用,它奏效了.如果此方法不合适,请说明原因.

I tried to use call by ref and it worked. If this method not proper, please inform me with reason.

我向 Form1 添加了带有单击操作的新按钮.

I added new button with click action to Form1.

private void ButtonClear_Click(object sender, EventArgs e)
{
     class.TextClear(ref textBox1);
} 

然后我创建了一个名为class"的新类并在下面添加了方法.

And I created new class named "class" and added method below.

public static void TextClear(ref TextBox textBox1)
{
    textBox1.Clear();
}

这篇关于如何从C#中的另一个表单中清除文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆