|
-
Jan 6th, 2008, 07:45 AM
#1
Thread Starter
Addicted Member
[2008] Creating New Forms At RunTime
so Im creating an IRC Client.
If someone sends u a private msg, I need to create a new form having 2 textboxes and a Send button
...
how? should I create a form in design time then use a code
dim NEWFORM as NEW FORM2?
if so, how to write in the textbox of a specific form when a msg is recieved?
-
Jan 6th, 2008, 08:17 AM
#2
Re: [2008] Creating New Forms At RunTime
You have already suggested the best way of doing it. You create the form at design time then create a new instance of it at runtime. By default the control members are declared as Friend meaning their properties are accessible from within the other forms in the project but not outside the project.
If you want to access it from another project then you can either declare them as Public by modifying the modifier property or the better way would be to have to create a public member on the form e.g: setText() to do it for you.
The example below shows how you might declare an instance of the form and modify the controls before displaying it.
Code:
Dim rt As New frmRuntime
rt.txtMbr1.Text = "hello"
rt.txtMbr2.Text = "world"
rt.Show(Me)
You can also use the ShowDialog method which will prevent the use of the parent form until it is closed; however, as you are creating a PM facility I would imagain you wouldn't need it.
-
Jan 6th, 2008, 08:42 AM
#3
Thread Starter
Addicted Member
Re: [2008] Creating New Forms At RunTime
no, thats not my question
here:
lets say User Mike Sends a msg to the client
we open a new form
then Mike sends another msg... that code will open a new form!
I dont want to open a new form, i want to check if a form with the text Mike is opened so I can add to it, if not, we open a new form
how to do that?
-
Jan 6th, 2008, 10:33 AM
#4
Thread Starter
Addicted Member
Re: [2008] Creating New Forms At RunTime
in other words, how to a loop all the already opened forms and check their caption (frm.text)?
if frm.text = new username then add to the text box
else create a new form
-
Jan 6th, 2008, 10:44 AM
#5
Re: [2008] Creating New Forms At RunTime
Create a List(Of Form) to hold all the chat forms. When a new message arrives, check to see if a chat is already opened with the sender of the message, if it is you should just call BringToFront method of the form and pass it the new message. If its not opened you create a new instance of the chat form, add it to the list of chat forms, and somehow "tie" it to the sender of the message (Perhaps using the Tag property?)
-
Jan 6th, 2008, 10:44 AM
#6
Re: [2008] Creating New Forms At RunTime
I would be inclined to use a hash table containing references to the forms each with an index relating to the name of the user. You can then check the users form exists using the ContainsKey and bring it in to focus accordingly.
Below is some sample code:
Code:
Public Class Form1
Private formTable As Hashtable
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
Dim rt As New frmRuntime
If formTable.ContainsKey(txtName.Text.ToLower()) Then
' give the form focus
CType(formTable(txtName.Text.ToLower()), _
frmRuntime).Activate()
Else
rt = New frmRuntime
rt.Text = txtName.Text
rt.Show()
formTable.Add(txtName.Text.ToLower(), rt)
End If
End Sub
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
formTable = New Hashtable
End Sub
End Class
-
Jan 6th, 2008, 12:01 PM
#7
Thread Starter
Addicted Member
Re: [2008] Creating New Forms At RunTime
this is what i got
Code:
Private frmPrivateMsg As New List(Of frmPM)
Private Sub WritePrivateMsg(ByVal Message As String)
For Each cc As frmPM In frmPrivateMsg
If cc.Text = NewUserName Then
cc.txtMain.AppendText(Message)
Exit Sub
End If
Next
Dim NewFrm As New frmPM
NewFrm.Text = NewUserName
NewUserName = ""
NewFrm.txtMain.AppendText(Message)
NewFrm.Show(Me)
frmPrivateMsg.Add(NewFrm)
End Sub
Im getting on NewFrm.Show(Me)
Cross-thread operation not valid: Control 'AsynchronousClient' accessed from a thread other than the thread it was created on.
could u please help?
-
Jan 6th, 2008, 12:25 PM
#8
Re: [2008] Creating New Forms At RunTime
This is a totally different problem. What is your AsynhronousClient control and where are you attempting to access it. Have you explicitly created a new thread as a listener for you chat application?
Have a look at this: http://www.developerfusion.co.uk/forums/p/43081/143922/
-
Jan 6th, 2008, 12:59 PM
#9
Thread Starter
Addicted Member
Re: [2008] Creating New Forms At RunTime
AsynhronousClient is the name of my Class (Class 1) :S
and there are no other classes
-
Jan 6th, 2008, 01:33 PM
#10
Re: [2008] Creating New Forms At RunTime
There are the forms as well as the controls. I do believe that your form contains a property which runs an operation in another thread. See what happens when you change the line: NewFrm.Show(Me) to NewFrm.Show().
-
Jan 6th, 2008, 02:23 PM
#11
Thread Starter
Addicted Member
Re: [2008] Creating New Forms At RunTime
this is weird... it worked, no more errors but
when the form is show .... it freezes
not the whole application, only the new form stops responding...
why?
-
Jan 6th, 2008, 03:29 PM
#12
Frenzied Member
Re: [2008] Creating New Forms At RunTime
Are you using a listener thread?
-
Jan 6th, 2008, 03:34 PM
#13
Thread Starter
Addicted Member
Re: [2008] Creating New Forms At RunTime
no,
i connect to irc, i dont need a listener thread
Code:
Public Class AsynchronousClient
Private client As System.Net.Sockets.TcpClient
Private Const BYTES_TO_READ As Integer = 255
Private readBuffer(BYTES_TO_READ) As Byte
Private Delegate Sub WriteText(ByVal text As String)
Private frmPrivateMsg As New List(Of frmPM)
whats the problem? :S
-
Jan 6th, 2008, 03:36 PM
#14
Frenzied Member
Re: [2008] Creating New Forms At RunTime
The problem is that you are not using a listener thread. Are you really scanning for incoming messages on the same thread as the one containing all the controls?
-
Jan 6th, 2008, 03:43 PM
#15
Thread Starter
Addicted Member
Re: [2008] Creating New Forms At RunTime
Code:
Private Sub DoRead(ByVal ar As System.IAsyncResult)
Dim totalRead As Integer
Try
totalRead = client.GetStream.EndRead(ar) 'Ends the reading and returns the number of bytes read.
Catch ex As Exception
'The underlying socket have probably been closed OR an error has occured whilst trying to access it, either way, this is where you should remove close all eventuall connections 'to this client and remove it from the list of connected clients.
End Try
If totalRead > 0 Then
'the readBuffer array will contain everything read from the client
Dim receivedString As String = System.Text.Encoding.UTF8.GetString(readBuffer, 0, totalRead)
MessageReceived(receivedString)
End If
Try
client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf DoRead, Nothing) 'Begin the reading again.
Catch ex As Exception
'The underlying socket have probably been closed OR an error has occured whilst trying to access it, either way, this is where you should remove close all eventuall connections 'to this client and remove it from the list of connected clients.
End Try
End Sub
the code works fine,
Ive used it alot... is there a problem here?
-
Jan 6th, 2008, 04:04 PM
#16
Re: [2008] Creating New Forms At RunTime
I may be wrong: but I believe that when data is received on a port a new thread is spawned which continues to listen in the background. So it is entirely possible that when you attempt to access the members in the current thread that exception will be raised.
-
Jan 6th, 2008, 04:09 PM
#17
Re: [2008] Creating New Forms At RunTime
I believe that you may need to invoke to the main thread before creating and showing the form. the doRead method is running on a separate thread so creating and displaying a form from it will not work as its already busy with a tight loop.
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
|