如何更改一个文本框的文本在Visual C#另一种形式吗? [英] How to change text in a textbox on another form in Visual C#?

查看:194
本文介绍了如何更改一个文本框的文本在Visual C#另一种形式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual C#,当我点击一个按钮,我想加载另一种形式。但这种形式加载之前,我想,以填补一些文字文本框。我试图把一些命令显示表单之前要做到这一点,但我得到一个错误说该文本框无法访问由于其保护级别。

如何设置文本框的形式之前,我表现出来?

 私人无效button2_Click(对象发件人,EventArgs的发送)
    {        fixgame changeCar​​ds =新fix​​game();
        changeCar​​ds.p1c1val.text =3;
        changeCar​​ds.Show();
    }


解决方案

当您创建的按钮单击事件处理程序中的新形式,实例化一个新的表单对象,然后调用它的show方法。

一旦你的表单对象,你也可以调用上该类present任何其他方法或属性,包括设置文本框的值的属性。

因此​​,code下方添加了一个属性,设置文本框的(其中TextBox1的是你的文本框的名称)的窗体2类。我preFER这种方法比方法使文本框本身,因为它给你更好的封装,以确保您对文本框的使用方式控制修改其访问修饰符可见。

 公共部分类窗体2:表
{
    公共窗体2()
    {
        的InitializeComponent();
    }    公共字符串TextBoxValue
    {
        {返回textBox1.Text;}
        集合{textBox1.Text =值;}
    }
}

和的第一个表单你可以有code的按钮点击事件,如:

 私人无效的button1_Click(对象发件人,EventArgs的发送)
{
    Form2的窗口2 =新Form2的();
    form2.TextBoxValue =someValue中;
    form2.Show();
}

In Visual C# when I click a button, I want to load another form. But before that form loads, I want to fill the textboxes with some text. I tried to put some commands to do this before showing the form, but I get an error saying the textbox is inaccessible due to its protection level.

How can I set the textbox in a form before I show it?

 private void button2_Click(object sender, EventArgs e)
    {

        fixgame changeCards = new fixgame();
        changeCards.p1c1val.text = "3";
        changeCards.Show();


    }

解决方案

When you create the new form in the button click event handler, you instantiate a new form object and then call its show method.

Once you have the form object you can also call any other methods or properties that are present on that class, including a property that sets the value of the textbox.

So, the code below adds a property to the Form2 class that sets your textbox (where textbox1 is the name of your textbox). I prefer this method method over making the TextBox itself visible by modifying its access modifier because it gives you better encapsulation, ensuring you have control over how the textbox is used.

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public string TextBoxValue
    {
        get { return textBox1.Text;} 
        set { textBox1.Text = value;}
    }                       
}

And in the button click event of the first form you can just have code like:

private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.TextBoxValue = "SomeValue";
    form2.Show();
}

这篇关于如何更改一个文本框的文本在Visual C#另一种形式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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