Hello everyone,
I'm new around here and yes, I've checked the forum before posting this thread :)
I'm having big troubles with an app (developed for Windows Mobile phones) that has, basically, to control the temperature of a room and send a message to the phone if something's wrong...
Well, the app works great (every recording file works perfectly etc) but textboxes don't want to refresh... To be clear, I'll paste the part of the code that gives me headaches:

public void Insert()
        {
            if (Misurazioni.v_max > 130)
            {
                textBox1.ForeColor = Color.Red;
                textBox2.ForeColor = Color.Red;
                textBox3.ForeColor = Color.Red;
                textBox4.ForeColor = Color.Red;
            }
            else
            {
                textBox1.ForeColor = Color.Green;
                textBox2.ForeColor = Color.Green;
                textBox3.ForeColor = Color.Green;
                textBox4.ForeColor = Color.Green;
            }

            textBox1.Text = Misurazioni.date;
            textBox2.Text = Misurazioni.v_max.ToString();
            textBox3.Text = Misurazioni.unita;
            textBox4.Text = Misurazioni.n_fault.ToString();
            chrono.Write(ref Misurazioni.date, ref Misurazioni.v_max, ref Misurazioni.unita, ref Misurazioni.n_fault);
        }

So... everything is fine: the function calls Write (a method of chrono) regularly and, if you analyze the code when you run it step by step, the textboxes change their content with the new, exact, one... but, when I go to the emulator, they don't refresh so the new values don't appear... I tried textbox.refresh(), textbox.update(), application.doevents() etc but nothing works... I need to end this project before this Sunday and I'm JUST A LITTLE concerned about this problem (I'm trying to fix this since the beginning of last week)... Any help? Pleeeeeeease :(

Edited 3 Years Ago by Nick Evan: Fixed formatting

I actually dont understand what is wrong with your code based on your description.

Do you want to try to add text from some other class to textbox?

Well yes I've tried putting into the textbox different things... I tried even to put texts and numbers that weren't in a variable... The problem is that it doesn't refresh, showing only the first values... A professor told me to use delegates and threads... but I've never used it so I'd love if someone came up with a good idea :'(

to "add" data to textBox you have to glue th text together by using "+=" operators:

textBox1.Text = "some text";
textBox1.Text += " new text";
//out put is "some text new text".

If you work over classes or maybe even threads, yes, you should use delegates to update textBox control.
In this case, create a new method, which will update textBox only. And pass the data to it:

public delegate void TextBoxDelegate(string message);

public void UpdatingTextBox(string msg)
{
    if(textBox1.InvokeRequired)
         textBox1.Invoke(new TextBoxDelegate(UpdatingTextBox), new object[]{ msg });
    else
    {
         textBox1.Text = msg;
         //in case if you want to ADD text to previuos one do:
         //textBox1.Text += msg + " ";
}

//always call this method to show a text in textBox1, like:
string data1 = "some text";
UpdatingTextBox(data1);

//later ones again:
string data2 = "new text";
UpdatingTextBox(data2);

//and so on...

Hope this helps clarifying how to use delegates and updating controls with them.

Edited 5 Years Ago by Mitja Bonca: n/a

Ok, sorry if I appear a little dumb but I need to ask you more questions since you're so kind to answer...
First of all: I use classes, but this method is in the FORM class; so where I have to put the delegate ?
Second thing: I assume that my method INSERT will be, using a delegate, UPDATINGTEXTBOX. Am I wrong ?
Third thing: no, I don't have to concatenate values, I have to REPLACE them. I'm sorry if this line seems rude, I just wanted to be clear :)

So, again, I have to refresh a screen every 10 seconds, with the new valuse into the textboxes... The form refreshes correctly every 10 seconds, but the textboxes don't... this is the problem...

1. all code goes in the class (form) where the contol (textBox) is.
2. Delegate name is optional, you choose it; what ever name you want; just make is reasonable
3. is you have to replace then use only equal mark "=".

Ok, nothing... the textboxes still show the first values... This is what I did:

public delegate void TextBoxDelegate(string data, int val_max, string unita, int num_fault);



public void Insert(string data, int val_max, string unita, int num_fault)
        {
            if (textBox1.InvokeRequired) textBox1.Invoke(new TextBoxDelegate(Insert), new object[] { data, val_max, unita, num_fault });
            else
            {
                if (val_max > 130)
                {
                    textBox1.ForeColor = Color.Red;
                    textBox2.ForeColor = Color.Red;
                    textBox3.ForeColor = Color.Red;
                    textBox4.ForeColor = Color.Red;
                }
                else
                {
                    textBox1.ForeColor = Color.Green;
                    textBox2.ForeColor = Color.Green;
                    textBox3.ForeColor = Color.Green;
                    textBox4.ForeColor = Color.Green;
                }
                textBox1.Text = data;
                textBox2.Text = val_max.ToString();
                textBox3.Text = unita;
                textBox4.Text = num_fault.ToString();
                chrono.Write(ref Misurazioni.date, ref Misurazioni.v_max, ref Misurazioni.unita, ref Misurazioni.n_fault);
                journal.Analisi(ref Misurazioni.v_max, ref Misurazioni.stringr);
            }


Insert(Misurazioni.date, Misurazioni.v_max, Misurazioni.unita, Misurazioni.n_fault);

Did I set something wrong ?!?!?! I'm about to cry... Cry and scream... actually cry and scream and throwing something away xD

Refreshing the value of the control is one of the solutions to your problem:

   label.Text = firstvalue;
   label.Text =secondvalue;
   label.Refresh();

Adapt the above solution for label to your case.

This article has been dead for over six months. Start a new discussion instead.