Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
--------------------------------------------------------------------------------------


C#
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);
        }

}

}
Posted
Updated 11-Sep-14 5:52am
v2
Comments
[no name] 11-Sep-14 11:41am    
What is this? Why are you creating a new instance of Form1, updating the textbox and then immediately destroying it?
CPallini 11-Sep-14 12:01pm    
Well spotted, my virtual 5.
ZurdoDev 11-Sep-14 11:46am    
Just debug it. Should be easy to find.
Kishore Pogaru 11-Sep-14 11:58am    
When do you want to populate the text? If you create a new Form1 on the fly it will not update the existing forms content. Also i don't see you are calling excel_getCellValue method in your code

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.
 
Share this answer
 
:sigh:

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

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.
 
Share this answer
 
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
}

}

}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900