C#-从第二个表单更改一个表单的文本属性 [英] C# - Change text property of one Form from a second Form

查看:92
本文介绍了C#-从第二个表单更改一个表单的文本属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有很长时间了.

我使用的是LoginForm,当我单击文本框时,将出现另一个名为KeyboardForm的窗体.当我单击KeyboardForm上的按钮时,它应该发送它的text属性,并将其增加到LoginForm中文本框的text属性.

我一直在使用一种非常不切实际的方法,即使用保存输入的公共字符串,并在关闭时使用KeyboardForm,关闭LoginForm并重新加载它,从而使文本框更改为我插入的指定值. >
但是那样一来,它不会在我单击按钮时更改文本框,而是只会在我重新加载后更改它.

我想知道的是,如果可以的话,可以通过第二种形式在单击KeyboardForm上的按钮时更改LoginForm中的textbox.text值.

抱歉,冗长的描述,谢谢您的耐心配合.

I''ve been having this issue for quite a time.

I''m using a LoginForm that when I click the textbox, a second Form called KeyboardForm appears. When I click a button on the KeyboardForm it''s supposed to send it''s text property and increment it to the text property of the textbox in the LoginForm.

I''ve been using a very unpractical way, that is using a public string that saves the input, and the KeyboardForm when closing, closes the LoginForm and Reloads it, causing the textbox to change to the designated value I inserted.

But that way it wouldn''t change the textbox as I clicked the buttons, but instead would only change it after I reloaded it.

What I wanted to know is, if there is a way, from a second form, to change the textbox.text value in the LoginForm as I click the buttons on the KeyboardForm.

Sorry for the long description, and thank you for the patience.

推荐答案

您的孩子形式

in your child form

//delegate type for the event declare first
public delegate void UpdateEventHandler(object sender, EventArgs e);

public partial class KeyboardForm : Form
{

    //Next, the event itself is declared.
    public event UpdateEventHandler UpdateParent;

    public KeyboardForm()
    {
        InitializeComponent();
    }

    private void UpdateParent_Click(object sender, EventArgs e)
    {
        //Invoking an event
        if (UpdateParent != null)
            UpdateParent(this, null);
    }
}



在您的登录表单中



in your login form

public LoginForm()
{
    InitializeComponent();
}

private void login_Click(object sender, EventArgs e)
{
    KeyboardForm child = new KeyboardForm();
    // subscribe to child event 
    child.UpdateParent += new UpdateEventHandler(child_UpdateParent);
    child.Show();
}

void child_UpdateParent(object sender, EventArgs e)
{
    // increment text box value
    int newValue = int.Parse(textBox1.Text) +1;
    textBox1.Text = newValue.ToString();
}


以第一种形式创建一个函数,通过创建第一种形式的对象从第二种形式调用它.
而不是将字符串值传递给该形式.

否则
您可以通过从第二种形式创建第一种形式的对象来直接调用textbox.text属性.
create an function in first form, call it from second form by creating an object of first form.
than pass the sting value to that form.

or else
u can call directly that textbox.text property through creating an object of first form from second form


这篇关于C#-从第二个表单更改一个表单的文本属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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