Results 1 to 6 of 6

Thread: Passing a RTB as a Paramater

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2003
    Location
    Eden Prairie Minnesota
    Posts
    301

    Passing a RTB as a Paramater

    I have a Function in a module called Functions

    The function is:

    VB Code:
    1. Public Sub AddChat(ByRef RichTextBox As System.Windows.Forms.RichTextBox, ByVal ParamArray Text() As Object)        With RichTextBox
    2.             .SelectionStart = 99999999
    3.             .SelectionLength = 0
    4.             .SelectionColor = Color.White
    5.             .SelectedText = "[" & TimeOfDay & "] "
    6.             .SelectionLength = 0
    7.         End With
    8.         For I As Byte = LBound(Text) To UBound(Text) Step 2
    9.             With RichTextBox
    10.                 .SelectionStart = 99999999
    11.                 .SelectionLength = 0
    12.                 .SelectionColor = Text(I)
    13.                 .SelectedText = Text(I + 1) & Microsoft.VisualBasic.Left(vbCrLf, -2 * CLng((I + 1) = UBound(Text)))
    14.                 .SelectionStart = 99999999
    15.             End With
    16.         Next
    17.     End Function

    When i try and use this procedure, i can't reference my Rich Text Box

    This is where i call it:

    VB Code:
    1. Public Function Connect(ByVal server As String, ByVal port As Integer) As Boolean
    2.         Dim hostEntry As IPHostEntry = Nothing
    3.  
    4.         hostEntry = Dns.Resolve(server)
    5.  
    6.         Dim address As IPAddress
    7.  
    8.         For Each address In hostEntry.AddressList
    9.             Dim endPoint As New IPEndPoint(address, port)
    10.             Dim tempSocket As New Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
    11.             Try
    12.                 tempSocket.Connect(endPoint)
    13.             Catch Err As SocketException
    14.                 AddChat(rtbChat, Color.Red, "Error #: " & Err.ErrorCode & ", " & Err.Message)
    15.             End Try
    16.             If tempSocket.Connected Then
    17.                 Socket = tempSocket
    18.                 MsgBox("Connected")
    19.                 Exit For
    20.             End If
    21.         Next address
    22.         Return True
    23.     End Function

    RichTextBox is underlined in blue, with the error "Name 'rtbChat' is not declared.

    Why do i get this? Any help would be GREATLY apreciated

    P.S. Are my functions ok?
    Last edited by BaDDBLooD; Jan 16th, 2005 at 04:09 PM.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Passing a RTB as a Paramater

    try passing the richtextbox ' ByRef ' not ByVal , eg:
    VB Code:
    1. Public Function AddChat([COLOR=Red]ByRef[/COLOR] RichTextBox As System.Windows.Forms.RichTextBox, ByVal ParamArray Text() As Object) As Object
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2003
    Location
    Eden Prairie Minnesota
    Posts
    301

    Re: Passing a RTB as a Paramater

    yeah, i already tried that. STill nothing

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2003
    Location
    Eden Prairie Minnesota
    Posts
    301

    Re: Passing a RTB as a Paramater

    The function works if you use it in the Class frmMain

    But i can't seem to Reference the RTB of frmMain when calling it in a different class.

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: Passing a RTB as a Paramater

    you need to pass a reference to your main form ( the form with the Richtextbox ) to your other class when creating it , eg:
    VB Code:
    1. '/// The Class
    2. Public Class Class1
    3.  
    4.     Private frm1 As Form1
    5.  
    6.     Public Sub New(ByVal frm As Form)
    7.         frm1 = DirectCast(frm, Form1)
    8.     End Sub
    9.  
    10.     Public Sub test(ByVal s As String)
    11.         '/// put the string ' s ' in to Form1's Richtextbox
    12.         frm1.RichTextBox1.AppendText(s)
    13.     End Sub
    14. End Class
    to use ( As a Test in this case ) ...
    VB Code:
    1. '/// the Form that holds the richtextbox
    2.     Private cls As Class1
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         cls = New Class1(Me) '/// Me being this form
    6.         cls.test("some text")
    7.     End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2003
    Location
    Eden Prairie Minnesota
    Posts
    301

    Re: Passing a RTB as a Paramater

    You are GOD

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