-
Jan 20th, 2011, 03:13 PM
#1
Thread Starter
Junior Member
Creating, storing and managing forms dynamically
Hi, I'm making a LAN instant messenger and I'm trying to achieve something similar to how MSN will open a new chatting window when you double click on a friend. My main form has a list of friends, and I want to create a new chat window when they double click on one of those friends. I've created a "template" chat form which I want to generate dynamically, and I've run into some problems I need help with. Lets look at the code I already have.
The sub for double clicking on the friend listbox:
Code:
Private Sub lbFriends_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbFriends.DoubleClick
Dim fd() As String = Split(My.Settings.Friends.Item(lbFriends.SelectedIndex), "|"c)
If lbFriends.SelectedIndex >= 0 Then
If IsInClientList(fd(0)) = False Then
Me.Invoke(New del(AddressOf OpenNewChat))
End If
'Connection code
End If
End Sub
The sub for creating a new chat window, adding it to a list, and showing it:
Code:
Private Sub OpenNewChat()
Dim NewChatWind As New ClientWind
CWL.Add(ClientWind)
CWL.Item(CWL.Count - 1).Show()
End Sub
And outside of those subs I have this:
Code:
Private Delegate Sub del()
Dim CWL As New List(Of ClientWind)
The form will only appear once as far as I can tell, and the chat form has events such as button clicks; an error occurs when I try to change something like the text property of a label for example (eg. clicking a button on the chat form changes the text of a label on the chat form will cause an error to occur). The error I actually get is:
Cross-thread operation not valid: Control 'ClientWind' accessed from a thread other than the thread it was created on.
Now, call me a newb if you will, but I really don't know much about threading and all that hocus pocus. Some guidance in the right direction would be great.
EDIT: ClientWind is the chat form.
EDIT: That's weird. It doesn't occur with the actual builds.
Last edited by PSYCHONAUT; Jan 20th, 2011 at 03:56 PM.
-
Jan 20th, 2011, 06:11 PM
#2
Re: Creating, storing and managing forms dynamically
Cross-thread calls are allowed in a Release build but, by default, not in a Debug build. Follow the Codebank link in my signature and check out my submission on Accessing Controls From Worker Threads.
That said, it sounds like you're creating your chat windows on a different thread, which bad, bad, bad. You use Invoke in your code but you don;t show where you're creating new threads, so it's hard tro say where the issue is.
-
Jan 20th, 2011, 06:22 PM
#3
Thread Starter
Junior Member
Re: Creating, storing and managing forms dynamically
I managed to solve the issue by invoking components on the chat form when necessary, but it's a bit of a hassle.
You use Invoke in your code but you don;t show where you're creating new threads, so it's hard tro say where the issue is.
Well I'm not 100% sure what you're asking, but the OpenNewChat sub is located under the same class as the lbFriends_DoubleClick sub if that means anything.
EDIT: BTW, I can still only get one instance of the chat window to open, it has something to do with the fact I'm adding them to a list. When I do it without the list, it will show multiple instances as expected. Any ideas?
EDIT: Never mind, it was a stupid little mistake. Notice I had "CWL.Add(ClientWind)" instead of "CWL.Add(NewChatWind)".
Last edited by PSYCHONAUT; Jan 20th, 2011 at 06:54 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|