سؤال

I am using one thread running this code:

try
{
    Form1 f = getForm<Form1>();
    f.changePrice(price);
}
catch (Exception e)
{
    Console.WriteLine("error: " + e);
}

Here is the changePrice method:

public void changePrice(Int32 price)
{
   txtPrice.Text = ""+price;
}

I want to add text to my textbox "txtPrice".

هل كانت مفيدة؟

المحلول

You should change your textbox text at runtime like this.

public void changePrice(Int32 price)
    {
        if (InvokeRequired)
        {
            this.Invoke(new Action<Int32>(changePrice), new object[] { price });
            return;
        }

        txtPrice.Text = ""+ price;          
    }

This will do the trick.

نصائح أخرى

convert it to string as Text property is of type string:

public void changePrice(Int32 price)
{
   txtPrice.Text = price.ToString();

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top