Error when I try to get another form value - vb.net

I'm using Visual Studio 2013. My issue is that I can't get any value of my form1 from any class.
This is my code
Public MyHour as DateTime
Public SomeValue as string
Public MyClass as SomeClass
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MyHour = datetime.now()
Somevalue = "asjdasd"
MyClass = new Someclass()
End Sub
Here is the button1 click event code in form1:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Myclass.mysub()
End sub
When I call a sub on my class I can't access to anything of my form1:
public sub mysub()
Dim datetovalidate as datetime= form1.actualhour
Dim stringtovalidate as string = form1.somevalue
messagebox.show(datetovalidate.tostring)
messagebox.show(stringtovalidate)
end sub
From my class:
Datetovalidate shows me: 01/01/0001 00:00:00
Stringtovalidate shows me : ""
Even If I try to access to a property of a control in my form1 from my class nothing happens, and there is no exception being fired and no errors show me up for this situation. What can I do?

I recommend you pass the parameters you need into the subroutine:
Public Sub MySub(ByVal myHour as DateTime, ByVal someValue as String)
messagebox.show(myHour.tostring)
messagebox.show(someValue)
End Sub
Then, you'll call it from Form1
Myclass.MySub(MyHour, SomeValue)
You could instead pass the whole Form1 in there, but that's less recommended in case you want to call the method from a different form, or from something that isn't a form at all.

Related

VB.NET Set text value of a textbos of another form

what i need is when form2 is opened from form1 cliking on a button then on form2 i click on anothe butto and i set the value of a textbox of form1.
if i set the type of application as windows form application it is all ok but if i set as class library i have the error reference to a non-shared member requires an object reference.
if i reference to Dim frm = New form2 it open a second form2 and i dont want it.
how can resolve this?
thank you.
here is the code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.ShowDialog()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.TextBox1.Text = "aaaa"
Me.Close()
End Sub
End Class
When you build vb.net winforms project, some extra code is generated in the project. For example static instance of the Form class, so you can access instance methods through the class name Form1.ShowDialog().
This were done for VB6 programmers to make shifting from VB to VB.NET easier.
When you change project to be a library project, this code not generated anymore and Form1 is just a class and you can not access instance methods directly, but need to instantiate an instance "manually".
Instead of using this "hidden" shared instance, create form instances manually. You can pass instance of form1 to the constructor of form2 and update form1 from there.
Because you are using ShowDialog, I would suggest to make Form2 not depend on the Form1 and instead of updating Form1 textbox directly(you would like to avoid making Form controls public), return value as result of the dialog.
Public Class Form2
Public Property ResultValue As String
Private Sub Button1_Click(s As Object, e As EventArgs) Handles Button1.Click
ResultValue = "Value from Form 2";
DialogResult = DialogResult.OK; ' This suppose to close the form
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(s As Object, e As EventArgs) Handles Button1.Click
Using form As New Form2()
Dim result As DialogResult = form.ShowDialog()
If result = DialogResult.OK Then
TextBox1.Text = form.ResultValue
End If
End Using
End Sub
End Class
Add the following to form1
Public static sub changeTitle(myTextBox as Object,title as string)
myTextBox.Text=title
End sub
Call above function in form2
Form1.changeTitle(Form1.TextBox1, "new title")

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

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

vb.net Public Shared String loose value when open second form

I'm declaring two strings into my main form this way:
Public Shared SerNum As String = vbNullString
Public Shared SKey As String = vbNullString
Then I give some values to them. After that, I open another form and I try to get values from the two variables but only SerNum preserves his value while SKey turns out to be Nothing.
I repeatedly checked my code but I didn't found a reason for this to happen.
The second form is showed immediatly after giving values.
What can I check to find the error?
At the moment I solved by using a Public Shared Dictionary(of String, String) and putting both strings into it, but I would like to understand where I'm wrong.
EDIT
I found the mistake: SKey was also declared into my sub so the value wasn't assigned to the Public Shared variable but to the local variable.
I thought I had 'commented' that row...
I have a proposition for acceding to your variable from other form :
In From main let's name it "From1" must be like :
Public Class Form1
Public SerNum As String = vbNullString
Public SKey As String = vbNullString
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fr As New Form2(Me)
SerNum = "ValueNum"
SKey = "ValueKey"
fr.Show()
End Sub
End Class
In your seconde from "Form2" :
Public Class Form2
Dim fr As New Form1
Public Sub New(fr As Form1)
InitializeComponent()
Me.fr = fr
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox(fr.SKey)
MsgBox(fr.SerNum)
End Sub

Creating form name and object name as parameters of a function

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