Adding Controls at Runtime

Answered
0
0

Hi all,

So still working on my little IRC chat application on and off, and I found that if I create controls at runtime via a user action like a button press or on a form load, that works just fine, but if they are created when an IRC message comes in (like a new channel label for an incoming query), the controls do not show up on the form.  This also seems to be a problem with the DataGridView, since it won’t display rows added this way either, and doing so will stop even the user-driven changes from being displayed in the grid/on the form afterwards.  I do have an Application.Update in there, but it does not seem to make a difference.

Any way for me to fix that?

Thanks,

Chris

  • You must to post comments
Best Answer
0
0

Hi Chris,

Yes, found the problem with your app. You call Application.Update(me) after you create the label and add it. It works when you click the button because it’s a normal in-bound request and Wisej has the context in place.

But when the request comes from an independent thread (in  our app it’s the incoming ircClient1_LocalUser_JoinedChannel event), there is no context and it’s impossible to retrieve a session. That’s why we have the Application.Update, RunInContext, StartTask methods. If you simply changed the text or color of an existing control and then called Application.Update(me) it would have worked, but you are creating and adding a new control *before* running in context and Wisej cannot register the control with the session.

The best and safest way to handle this is to wrap the entire out-of-bound code (or event raisers in your class) in context:

 

Application.Update(Me,
Sub()

‘ Here you can do anything in context.

 

End Sub)

 

I have attached the modified file. Look also for the ‘Not needed comments.

HTH

Best,

Luca

 

Attachment
  • Chris Abate
    Thanks Luca. I think I get it, though I don’t see the attachment on this one?
  • Luca (ITG)
    Sorry, added now.
  • Chris Abate
    Thanks Luca!
  • You must to post comments
0
0

Hey Luca,

Must be something different in the threading because your sample works fine and I can only break it by doing something I wouldn’t be doing with the other app. I assume it’s because the your threads are still started by a user input rather than the server.

I’ve attached the IRC app.  Since I had switched the DataGridview I was using for the NickList to a ListBox when it wasn’t working, I don’t still have that part to test, but I do have the regular control addition.  On an event like ircClient1_LocalUser_JoinedChannel, it will call AddNewChanLbl, which will just add a label to the upper left corner of the form.  JoinedChannel will get raised automatically on a login, so you can either login with the uname/pwd combo in the form load, or just change the “autologin” setting in the web.config to true.  In either case, the label will not appear.  If you click the Add Label button though, one will.

Thanks,

Chris

  • You must to post comments
0
0

Please try the attached sample app. It adds a new control and a new row every second in a background task. Let me know if this is different from what you are doing and in case, can you tweak the sample to make it fail? Or can you send me the IRC app? There may be a difference with the threads.

 

  • Chris Abate
    Hey Luca, Did you get a chance to look at my app to see what might the issue? Thanks, Chris
  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.