Results 1 to 17 of 17

Thread: [2008] Creating New Forms At RunTime

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    [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?

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    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?

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    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

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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?)
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    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?

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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/
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [2008] Creating New Forms At RunTime

    AsynhronousClient is the name of my Class (Class 1) :S
    and there are no other classes

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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().
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    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?

  12. #12
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2008] Creating New Forms At RunTime

    Are you using a listener thread?

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    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

  14. #14
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    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?

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    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?

  16. #16
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  17. #17
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width