如何在C#中更新文本框的值 [英] How to updated the value of a textbox in C#

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

问题描述


我正在尝试更新文本框,但它不起作用。

来自< do something =here =>的值应该不加思索地更新到textbox2。



它编译得很好,但文本框永远不会更新!



br

Nikolaj

--------------------------------- -------------------------------------------------- ---





Hi I'm trying to update a textbox, but it doesn't work.
The value from <do something="" here=""> should immitiately be updated into textbox2.

It compiles fine, but textbox is never updated !

br
Nikolaj
--------------------------------------------------------------------------------------


namespace Excel_form
{
    
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            excel.excel_init(textBox1.Text.ToString());
           
        }

       
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            
        }

      
        public delegate void TextBoxDelegate(string message);

        public void UpdatingTextBox(string msg)
        {
            if (textBox2.InvokeRequired)
                textBox2.Invoke(new TextBoxDelegate(UpdatingTextBox), new object[] { msg });
            else
            {
                textBox2.Text = msg;
            }

            
        }

    }



public static class excel
{

 public static string excel_getCellValue(string lookFor)
        {
            
          do something here (value comes from here)    ............
........................
.....................

                Form1 fm1 = new Form1();
                fm1.UpdatingTextBox(value);
        }

}

}

推荐答案

As Wes Aday 建议您在 excel_getCellValue 方法中错误地创建 Form1 的新实例。

您必须调用现有 Form1的 UpdatingTextBox 方法实例。
As Wes Aday suggested, you are wrongly creating a new instance of Form1 in your excel_getCellValue method.
You have to call the UpdatingTextBox method of the existing Form1 instance.


:叹息:



看看你的代码:

:sigh:

Look at your code:
Form1 fm1 = new Form1();
fm1.UpdatingTextBox(value);



new 关键字有什么作用?

你知道吗:它会创建该类的 新实例 - 然后您将其更新以反映新值。这不会反映在当前显示中,因为这是与您刚刚创建的表单不同的表单实例。



您需要获取excel类更新实际显示 - 通过提供表单处理和更新的已更改事件来最好地处理。

请参阅此处:在两种形式之间传递信息,第2部分:儿童到父母 [ ^ ] - 将您的表单视为父级,将您的excel类视为子级,它应该解释如何做。


What does the new keyword do?
You know that: it creates a new instance of the class - which you then update to reflect the new value. That won't be reflected in the the current display because that is a different instance of the Form from the one you just created.

You need to get the excel class to update the actual display - which its best handled by providing a "Changed" event which the form handles and updates itself.
See here: Transferring information between two forms, Part 2: Child to Parent[^] - think of your form as the Parent, and your excel class as the Child, and it should explain how to do it.


好的,下面的代码正在运行。这是一个可接受的解决方案(代码实践明智)?



----------------------- --------------------



命名空间Excel_form

{



公共部分类Form1:表格

{

public Form1()

{

InitializeComponent();

}



private void Form1_Load(object sender,EventArgs e)

{



}



private void button1_Click(object sender,EventArgs e)

{



Excel.Excel_init( this ,textBox1.Text.ToString());



}





private void textBox1_TextChanged(object sender,EventArgs e)

{



}



private void textBox2_TextChanged(objec t发件人,EventArgs e)

{



}





public delegate void TextBoxDelegate(string message);



public void UpdatingTextBox(string msg)

{

if(textBox2.InvokeRequired)

textBox2.Invoke(new TextBoxDelegate(UpdatingTextBox),new object [] {m​​sg});

else

{

textBox2.Text = msg;

}





}



}







公共静态类Excel

{



公共静态字符串Excel_getCellValue( Form1 abc ,字符串lookFor)

{



在这里做点什么(价值来自这里)............

...................... ..

.....................





abc.UpdatingTextBox(value); //这不起作用

}

< br $>
}



}
Ok, the below code is working. Is that an acceptable solution (code practice wise)?

-------------------------------------------

namespace Excel_form
{

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

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{

Excel.Excel_init(this, textBox1.Text.ToString());

}


private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void textBox2_TextChanged(object sender, EventArgs e)
{

}


public delegate void TextBoxDelegate(string message);

public void UpdatingTextBox(string msg)
{
if (textBox2.InvokeRequired)
textBox2.Invoke(new TextBoxDelegate(UpdatingTextBox), new object[] { msg });
else
{
textBox2.Text = msg;
}


}

}



public static class Excel
{

public static string Excel_getCellValue(Form1 abc, string lookFor)
{

do something here (value comes from here) ............
........................
.....................


abc.UpdatingTextBox(value); // this is not working
}

}

}


这篇关于如何在C#中更新文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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