Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Changing text in textbox

Status
Not open for further replies.

mastermosley

Programmer
Jan 1, 2008
9
0
0
CA
Ok I've got a function

Code:
public void Log(string Text)
{
                        
  string CurrTime;
  CurrTime = DateTime.Now.ToString("dd/MM/yyyy h:MMtt");
  TextOut = "[" + CurrTime + " " + "] " + Text;
  textBox1.Text = "test" + textBox1.Text + TextOut + "\n";
                      
}

its in the same class as the form. I have an on click button event call the procedure like so in the same class as well:

Code:
public void button1_Click(object sender, EventArgs e)
        {
          Log("test");  
        }
this works sucesfully and puts the values into the textbox, but when I call it from a diffrent class it doesnt work. I set it up: Form1 Form1 = new Form1(); to make reference and it appears in the IDE menu thingy.

so I use

Code:
Aleme.Form1 Form1 = new Aleme.Form1();
Form1.Log("Test");

I no the call is working because if I use:

Code:
public void Log(string Text)
{
                        
  string CurrTime;
  CurrTime = DateTime.Now.ToString("dd/MM/yyyy h:MMtt");
  TextOut = "[" + CurrTime + " " + "] " + Text;
  textBox1.Text = "test" + textBox1.Text + TextOut + "\n";
  [b]MessageBox.Show(TextOut); //ADDED THIS[/b]
                      
}
the messagebox pops up with the Log but nothing is typed to the textbox. What is going on am I not making reference to something? Note that the log function is in the same class as the form so I don't need to make reference to the textbox1.text. And it is set to public.

 
1. could we have a little more code? how are you showing form1? is it on a different thread?

2. try reporting MessageBox.Show(textBox1.Text) instead.



mr s. <;)

 
Could the confusion be because of the naming of Form1?

Try changing:
Code:
Aleme.Form1 Form1 = new Aleme.Form1();
Form1.Log("Test");
To
Code:
Aleme.Form1 myForm = new Aleme.Form1();
myForm.Log("Test");

Age is a consequence of experience
 
Yes it is on a diffrent thread is that the problem?. I'm running a constant loop which processes the information taken from telent as this is a telnet server. And it would freeze my main form so I ran it in a diffrent thread.
 
Code:
    public void button1_Click(object sender, EventArgs e)
    {
            
      ThreadStart job = new ThreadStart(Connection.Start);
      Thread thread = new Thread(job);
      thread.Start();
           
     }

Calls the procedure Start() which is located in the same class, but using a diffrent thread.
 
There is a limitation in Windows Forms (and Windows in general) that when you update a Window, it must be done on the same thread as what created the Window.

In order to get back onto the correct thread, you generally use the InvokeRequired and BeginInvoke methods (you do it slightly differently in WPF)

This blog post by someone has a decent implementation:

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Ok I set up my log function like this:

Code:
public void Log(string Text)
{
     string CurTime;
     Form1 myForm = new Form1();
     if (myForm.textBox1.InvokeRequired)
     {
     myForm.textBox1.BeginInvoke(new StringParameterDelegate(Log), new object[] { Text });
                return;
      }
     // Must be on the UI thread if we've gotten this far
     MessageBox.Show("THIS SHOULD NOT COME B4 TEST");
     CurTime = DateTime.Now.ToString("dd/MM/yyyy h:MM tt");
     TextOut = "[" + CurTime + "] " + Text;
     myForm.textBox1.Text = myForm.textBox1.Text + TextOut;
 }

I call this function from a diffrent thread but it bypasses the if (myForm.textBox1.InvokeRequired). What am I doing wrong?
 
from the MSDN:


true if the control's Handle was created on a different thread than the calling thread (indicating that you must make calls to the control through an invoke method); otherwise, false.

As we cam see from your code, it was created on this thread.

At this point no Invoke is required.

Hope this helps

Age is a consequence of experience
 
Ya but I'm calling the code after I do a threadstart so its in a diffrent thread when I call the Log Functions.
 
If it was on the same thread then the textbox.text should be changed but its not. thats the problem.
 
Ok I think I figured something out here.

Code:
public void Log(string Text)
{
    string CurTime;
    Form1 myForm = new Form1();
    if (myForm.textBox1.InvokeRequired == true)
    {
        MessageBox.Show("TEST");
        //We're not in the UI thread, so we need to call BeginInvoke
        myForm.textBox1.BeginInvoke(new StringParameterDelegate(Log), new object[] { Text });
        return;
     }
     [b]if (myForm.textBox1.IsHandleCreated == false)
     {
          MessageBox.Show("DAMMIT!");
          return;
                
      }[/b]
       // Must be on the UI thread if we've gotten this far
       CurTime = DateTime.Now.ToString("dd/MM/yyyy h:MM tt");
       TextOut = "[" + CurTime + "] " + Text;
       myForm.textBox1.Text = myForm.textBox1.Text + TextOut;
           
}

I read that invokerequired will return false if the controls handle hasnt been created so I added the IsHandleCreated. I'm going to start off from the beginning again. The form is started it is all drawed out whatever and a start button is on the form.

Code:
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "Telnet Server Starting....";
            Thread t = new Thread(new ThreadStart(server.Start));
            t.IsBackground = true;
            t.Start();
        }
The textbox.Text is changed and a new thread is created which starts the server.

Now at the beginning of the start procedure it says:

Code:
procedures pro = new procedures();
pro.Log("Dedicated Telnet Thread Created");

The box isn't changed, and the MessageBox for telling me the handle hasn't been created pops up. How can it not be created if I just used it??? WTF this is stressing :p
 
If you just want to display a text (passed as param in the Log() ) in that form then try to pass it in the ctor of the form and in the Load event assign it to the text box. You should have not any problem due to the thread.
If you want to update the Text box in that form during the running then you should not create the form each tine. Instead, create it one time and use events to communicate between other classes and that form. Pass the string to be displayed in a class derived from EfventArgs.
obislavu
 
I am only creating the form once. I'm calling the log procedure from a different thread then the main thread which the text box was created in thats a problem. As you can see by my code if the handle for the text box is created then I should have no problem, how do I create a handle for my text box.
 
Your threads shouldn't rely on having a control created... In fact, you should be populating data in a controller and then the controller should be responsible for notifying gui items.

You may want to reconsider your architecture.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top