通过单击按钮更改另一种形式的文本 [英] Changing text in another form by clicking a button

查看:105
本文介绍了通过单击按钮更改另一种形式的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图通过单击Form2中的按钮(form2中的button1为"STARTA")来更改Form1中文本框的文本,现在可能花了2个小时了(我是一名编程新手!) . 我一直在寻找类似的问题,但发现了很多问题,但是即使尝试了很多,我仍然无法解决问题.

I've been trying to change the text of a textbox in Form1 by clicking a button (button1 in form2 is "STARTA") in Form2 and probably spent a good 2 hours now (I'm a programming-newbie!). I have been searching around for similiar questions and found a bunch, but even after trying a lot of them I can't get it to work.

Form1 [设计]

Form1[DESIGN]

Form2 [设计]

Form2[DESIGN]

我现在尝试的方法是这里

在Form1中,我这样写:

In Form1 I wrote this:

public string STARTTID
{
    get
    {
        return this.textBox3.Text;
    }
    set
    {
        this.textBox3.Text = value;
    }
}

我知道获取并设置一个空的textBox并没有多大意义,但是我尝试了很多我认为应该可行的解决方案,但是当我单击按钮时,textBox的文本不会改变! 在form2中,当单击button1时,我这样写:

I know it doesn't quite make sense to get and set an empty textBox, but I've tried so many different solutions which I think should work, but the textBox's text just wont change when I click the button! In form2, when button1 is clicked, I wrote this:

string TIDEN = DateTime.Now.ToString("HH:mm:ss tt");
Form1 first = new Form1();
first.STARTTID = TIDEN;

我想做的是,我希望在按form2中的button1时,将form1中的textBox3中的文本更改为当前时间.

What I'm trying to do, is that I want the text in textBox3 in form1 to change to the current time when button1 in form2 is pressed.

很抱歉,如果这篇文章有点混乱,那是我的第一篇文章,英语不是我的最强语言.

Sorry if this post is a bit messy, it's my first and english isn't my strongest language.

推荐答案

问题是您要创建一个新的Form1并在该标签上更新标签,而不是以您的初始形式

problem is you creating new Form1 and update label on that one, not in the your initial form

    Form1 first = new Form1();
    first.STARTTID = TIDEN;

您不需要创建新表单,因为您已经创建了它.您可以做的是在使用接受Form作为参数的构造函数创建Form2时将Form1解析为Form2.或在Form2中为Form1创建属性,并在创建Form2时进行设置.

You don't need to create new form because you already created it. what you can do is parse the Form1 to Form2 when you create Form2 by using constructor which accept Form as parameter. or create property in Form2 for Form1 and set that when you creating Form2.

Form1

Form2 f2 = new Form2(this);
f2.Show();

Form2

public partial class Form2 : Form
{
    private Form1 form1;

    public Form2(Form1 form1)
    {
        InitializeComponent();
        this.form1 = form1;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        form1.STARTTID = "set by form2";
    }
}

这篇关于通过单击按钮更改另一种形式的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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