0

I'm trying to pass form name and object as parameters of my public sub because I have 2 forms and they are almost identical to one another.

For example I have richtextbox1 in Form1 and I also have richtextbox1 in Form2, I would like to create a public sub something like this.

Public Sub Sub1(ByVal frm_name As form, Byval obj_name As object)
    frm_name.obj_name.Lines.Length 'Get the length of lines of that richtextbox
End Sub

Then when I want to call it

'Form 1 Codes
Public Class Form1
   Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
       Sub1(me, richtextbox1)
   End Sub
End Class

'Form 2 Codes
Public Class Form2
   Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
       Sub1(me, richtextbox1)
   End Sub
End Class

It is not working the way I want it, is there anything I can do for it to work?

1
  • Use TypeOf the you can use a case to cast the object to the correct control... Commented Jul 29, 2015 at 3:00

1 Answer 1

2

Think about it. You are saying that you're passing the names of things but you're not. You're not passing the name of a form; you're passing the form itself. You're not passing the name of an object; you're passing the object itself. What is the actual point of this? It's to get the number of lines in a RichTextBox, right? So, write a method that takes a RichTextBox as an argument and then call it passing a RichTextBox.

Public Sub Sub1(rtb As RichTextBox)
    Dim lineCount = rtb.Lines.Length

    '...
End Sub

'Form 1 Codes
Public Class Form1
   Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
       Sub1(richtextbox1)
   End Sub
End Class

'Form 2 Codes
Public Class Form2
   Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
       Sub1(richtextbox1)
   End Sub
End Class
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This seems to be working fine, but when I add a timer it says 'enabled is not a member of system.threading.timer' My code is something like this Public Sub Sub1(rtb As RichTextBox, tmr As Timer) Then on my usage tmr.Enabled = True
You're using the wrong type of Timer. In a WinForms project, you should generally be using a System.Windows.Forms.Timer. In some cases, a System.Timers.Timer is appropriate. It is very rare that you should be using a System.Threading.Timer directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.