Creating form name and object name as parameters of a function - vb.net

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?

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

Related

Trying to call a sub on another part but it won't accept the parameters [duplicate]

Is it possible to Call a Sub with events?
I have the following sub, and when I press a button, I will trigger a Msgbox.
Public Class SelectLevel
Public Shared Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click
MsgBox("Test")
End Sub
End Class
Can I Call this sub in another Class? Something along the lines of:
Public Class TestClass
Private Sub Testing
Call Button_Start_Click ' I tried this first, but it didn't work.
Call SelectLevel.Button_Start_Click ' I tried this aswell but didn't work.
End Sub
End Class
Thank you!
You need to pass the parameters too.
This should work:
Public Class TestClass
Private Sub Testing
Call SelectLevel.Button_Start_Click(Nothing, System.EventArgs.Empty)
End Sub
End Class
Your code is correct except in one place. While calling, you have to give the required parameters
Your Sub Procedure is : Public Shared Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click
But you're calling as :Call SelectLevel.Button_Start_Click (without any values to be sent)
Call like this:
Private Sub Testing()
SelectLevel.Button_Start_Click(Button_Start, Nothing) ' I tried this aswell but didn't work.
End Sub
This will do your work
If the event you need is only click event then there is an easy way PerformClick function
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Button1.PerformClick()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox("pressed")
End Sub
End Class

Having problem in passing variables between two forms in vb.net

I'm almost a beginner of vb.net and I have to do some codes. I want to call a button click in form 2 from another class, i.e., form 1 and the problem is that the variables which defined as pubic, their values don't pass between two forms. My code simply presented as follows:
Public class form2
Public Property ResponseTime1 As String
Public Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
Responsetime2 = 20
End sub
End class
Pub class form1
Dim Resposetime as string
Dim z as new form2
Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
z.button1_click(sender, e)
ResponseTime = form2.ResponseTime2
MsgBox(ResponseTime) '' show nothing????????????
End sub
End class
I would be appreciated if someone helps me
The first problem I see is that Responsetime2 is never declared. A few spelling errors are further messing up the code. Even if I stick a Dim in front of Responsetime2 = 20 it is still not visible to from1. Just because a method is public doesn't mean that vaiables inside the method are visible. Even if it was visible, `Responsetime2 is an Integer and you have declared Responsetime as a String.
It is not a good idea to call an event procedure from another class. Event procedures are meant to respond to an event and are private by default. Make the code a separate public method and call it from both form2.button1_Click and form1.
Public Class form25
Public Property ResponseTime2 As Integer
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
SetResponseTime2()
End Sub
Public Sub SetResponseTime2()
ResponseTime2 = 20
End Sub
End Class
Public Class form15
Dim Responsetime As Integer
Dim z As New form25
Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
z.SetResponseTime2()
Responsetime = z.ResponseTime2
MsgBox(Responsetime.ToString)
End Sub
End Class
Hi dear I have no idea why you made this Public Property ResponseTime1 As String most probably you have a reason for it but i am going to show for you how i would do it
Form1 Code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
form2.show
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(Form2.ResponseTime)
End Sub
End Class
Form2 code
Public Class Form2
Public ResponseTime As String
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ResponseTime = "test"
End Sub
End Class
I tested this one and its working
Hope i could help to you

VB.Net - Call a Sub with events

Is it possible to Call a Sub with events?
I have the following sub, and when I press a button, I will trigger a Msgbox.
Public Class SelectLevel
Public Shared Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click
MsgBox("Test")
End Sub
End Class
Can I Call this sub in another Class? Something along the lines of:
Public Class TestClass
Private Sub Testing
Call Button_Start_Click ' I tried this first, but it didn't work.
Call SelectLevel.Button_Start_Click ' I tried this aswell but didn't work.
End Sub
End Class
Thank you!
You need to pass the parameters too.
This should work:
Public Class TestClass
Private Sub Testing
Call SelectLevel.Button_Start_Click(Nothing, System.EventArgs.Empty)
End Sub
End Class
Your code is correct except in one place. While calling, you have to give the required parameters
Your Sub Procedure is : Public Shared Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click
But you're calling as :Call SelectLevel.Button_Start_Click (without any values to be sent)
Call like this:
Private Sub Testing()
SelectLevel.Button_Start_Click(Button_Start, Nothing) ' I tried this aswell but didn't work.
End Sub
This will do your work
If the event you need is only click event then there is an easy way PerformClick function
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Button1.PerformClick()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox("pressed")
End Sub
End Class

How to pass an integer value to other forms? [duplicate]

This question already has answers here:
Passing variables between windows forms in VS 2010
(3 answers)
How to pass value from Form1 to Form2
(2 answers)
Closed 5 years ago.
Is it possible to use a label? I'm planning to display a scoring system, like every correct answer the score will add 10pts.
Use an instance of a class within a form. The Form can call methods of the Class. The Class can raise events to the Form. Don't talk directly from a Class to a Form. Or from one Form to another.
Form1 with TextBox1:
Public Class Form1
Private myClass1 As Class1
Private myForm2 As Form2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myClass1 = New Class1()
myForm2 = New Form2(myClass1)
myForm2.Show()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
myClass1.SetText(TextBox1.Text)
End Sub
End Class
Form2 with Label1:
Public Class Form2
Private WithEvents myClass1 As Class1
Public Sub New(instance As Class1)
InitializeComponent()
myClass1 = instance
End Sub
Private Sub myClass1TextSet(value As String) Handles myClass1.TextSet
Me.Label1.Text = value
End Sub
End Class
Class1:
Public Class Class1
Private text As String = ""
Public Event TextSet(value As String)
Public Sub SetText(value As String)
Me.text = value
RaiseEvent TextSet(value)
End Sub
End Class
Form2.Label1 will update as you type in Form1.TextBox1. You may change it around as you need to fit your application, but try to keep this structure.
Form >> instance >> Class
Class >> events >> Form
The form instantiating the other form is for simplicity of this example. Larger scale projects could have a form loader factory responsible for making forms.
In your Form2 add a public shared integer
Public Shared score As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Then in Form1 increase it by adding to it
Form2.score += 50
for example
Add two forms to project
in Form2 define Label1 control
and in Form1
Public Class Form1
Private intScore As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Form2.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Label1.Text = intScore.ToString
End Sub
End Class

Switching between different forms

I have some forms in my application and need to switch between them. I dont want to open another form over other form, in short I want the user to get a feel that he is working on one form only. Right now I am doing something like this but this is not what I want
Public Class Form1
Dim fo As Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.TopLevel = False
Me.Panel1.Controls.Add(Form2)
Form2.Show()
End Sub
End Class
I also need to send data to other form like list of some class.
Look into UserControls(Composite Controls). They will allow you create a custom layout with events and properties and add it to your panel. I have used this to swap in/out edit pages for my applications.
Here is a very simplistic example:( 1 Form, 2 Panels, 2 UserControls)
Form1
Public Class Form1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub UserControl11_SendText(value As String) Handles UserControl11.SendText
UserControl12.SetText(value)
End Sub
Private Sub UserControl12_SendText(value As String) Handles UserControl12.SendText
UserControl11.SetText(value)
End Sub
End Class
UserControl
Public Class UserControl1
Public Event SendText(ByVal Text As String)
Public Sub SetText(value As String)
Label1.Text = value
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RaiseEvent SendText(TextBox1.Text)
End Sub
End Class
Have you considered using a TabControl? This will let you create different screens, and you (or the user) can switch between them easily.