VB 2008-calling a function across forms - vb.net

This is my program.
Public Class Form1
Dim adult As Decimal
Public Function getadult(ByRef adult As Decimal) As Decimal
Return adult
End Function
Public Function setadult(ByVal value As Decimal) As Decimal
adult = value
End Function
Public Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles adultrate.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call setadult(adultrate.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim adult As Decimal
Call getadult(adult)
MsgBox("the rate for adults is " & adult)
End Sub
End Class
I cant successfully call the function adult and its value to another form. It works great if i use it within the same form. Any ideas?

Simplest option is to add a Module to your project, and place your code there.
You should also be using a property...
Dim _adult As Decimal
Public Property adult As Decimal
Get
Return _adult
End Get
Set (byval value As Decimal)
_adult = value
End Set
End Property
Both of your forms will have access to this property.
To set and get your property simply use;
adult = cdec(adultrate.Text)
adultrate.Text = adult
So for your code...
Form1:
Public Class Form1
Public Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles adultrate.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
adult = CDec(adultrate.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
End Sub
End Class
Form2:
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("The rate for adults is " & adult)
End Sub
End Class
Module:
Module Module1
Dim _adult As Decimal
Public Property adult As Decimal
Get
Return _adult
End Get
Set (byval value As Decimal)
_adult = value
End Set
End Property
End Module
Of course, you could just have a public variable in your module too.....

Related

Use a textbox to store a numeric counter in VB.Net

I want to count with a TextBox.
Here is my code:
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
OrainsProgressBar1.Increment(1)
If OrainsProgressBar1.Value = 100 Then
Timer3.Start()
Timer1.Stop()
End If
End Sub
Private Sub OrainsTheme1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrainsTheme1.Click
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Timer1.Start()
Timer2.Start()
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
OrainsProgressBar1.Increment(-1)
If OrainsProgressBar1.Value = 0 Then
Timer1.Start()
Timer3.Stop()
End If
End Sub
Private Sub OrainsButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrainsButton1.Click
OrainsTextBox1.Text += 100
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
OrainsTextBox1.Text += 1
End Sub
End Class
But I have an error with OrainsTextBox1.Text += 1. VB says:
'Conversion from string "" to type 'Double' is not valid.'
What is the problem?
In the .Net world, the data types of things matter a lot. Strings (like the .Text property) are NOT numbers. You need to convert. Even if someone only enters the digits 0-9 into the textbox, that's still a string of numeric characters, rather than a number. And what should happen if someone enters random text into that textbox that won't convert to a number type at all?
For this code, I suggest building a property, like this:
Private _orainsValue As Double
Public Property OrainsValue As Double
Get
Return _orainsValues
End Get
Set
_orainsValue = Value
OrainsTextBox1.Text = _orainsValue.ToString()
End Set
End Property
That will let you write code like this and have the expected result shown to the user:
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
OrainsVale += 1
End Sub
Note that it does mean you will want to mark the TextBox disabled,though, because this doesn't account for user data entry.
Instead of doing like this OrainsTextBox1.Text += 1
do like this OrainsTextBox1.Text = Val(OrainsTextBox1.Text) + 1
Because .Text is string. Which will append the 1s as "11111111"

Cannot pass variable in Private sub BackgroundWorker [duplicate]

Imports SpeechLib
Public Class Form1
Public vox = CreateObject("sapi.spvoice")
Private Sub cmdSpeak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSpeak.Click
Dim text2 As String = "Hello , This is a Text. Hello , This is a Text."
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
vox.pause()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SVEPhoneme As Integer = 64
vox.EventInterests = SVEPhoneme
vox.AlertBoundary = SVEPhoneme
End Sub
Private Sub cmdResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdResume.Click
vox.resume()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
vox.Speak(Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)
End Sub
End Class
How can I pass text2 to vox.speak?
In cmdSpeak_Click, pass text2 as a parameter to RunWorkerAsync
BackgroundWorker1.RunWorkerAsync(text2)
In BackgroundWorker1_DoWork, retrieve the value of the parameter
vox.Speak(DirectCast(e.Argument, String), SpeechVoiceSpeakFlags.SVSFlagsAsync)

VB 2008- runtime issues [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is my program code....
Public Class Form1
Dim adult As Decimal
Public Function getadult(ByRef adult As Decimal) As Decimal
Return adult
End Function
Public Function setadult(ByVal value As Decimal) As Decimal
value = adult
End Function
Public Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call setadult(adult)
TextBox1.Text = adult
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim adult As Decimal
Call getadult(adult)
MsgBox("the rate for adults is " & adult)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
When i enter a value into the textbox and press the "set" button it resets to 0. please help me
Public Function setadult(ByVal value As Decimal) As Decimal
**value = adult**
End Function
should be..
Public Function setadult(ByVal value As Decimal) As Decimal
**adult = value**
End Function
AND
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
**Dim adult As Decimal** <<<< Remove this
But I don't really get it why you are using two functions to set and get the value of adult. Either set it direct or maybe use a property.
For ex.
Public Function getadult(ByRef adult As Decimal) As Decimal
Return adult
End Function
this will just return the value you passed to the the function O.o
Maybe this is what you want to achieve:
Public Class Form1
Dim adult As Decimal
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
adult = CDec(TextBox1.Text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox("the rate for adults is " & adult.toString())
End Sub
End Class
adult no receipt value. TEST Edit button 1 click call setadult(textBox1.text).
Public Class Form1
Dim adult As Decimal
Public Function getadult(ByRef adult As Decimal) As Decimal
Return adult
End Function
Public Function setadult(ByVal value As Decimal) As Decimal
value = adult
End Function
Public Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call setadult(textBox1.text)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim adult As Decimal
Call getadult(adult)
MsgBox("the rate for adults is " & adult)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class

Object reference not set to an instance, public array with..Split(",")

i wonder what is wrong with the following vb.net code.
Public Class Form10
Public IDs() As String = TextBox1.Text.Split(",")
Private Sub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each id In IDs
MsgBox(id)
Next
End Sub
End Class
when i do
Form10.show()
i get an error "Object reference not set to an instance"
You have declared a field in your class to be initialized with a value from a control on the corresponding form that does not yet exist. The control is not initialized and loaded by the time your initializer on your field member is accessed, thus causing the error.
To keep the public IDs declaration, you could remove the initialization from the field declaration, then move the assignment to the Button1_Click event, as follows:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
IDs=TextBox1.Text.Split(",")
' Do something interesting with IDs now...
End Sub
Public Class Form10
Private Sub Form10_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim IDs() As String = TextBox1.Text.Split(",")
Form1.somefunction(IDs)
End Sub
and in Form1
Public Sub somefunction(ByVal IDs() As String)
For Each id In IDs
MsgBox(id)
Next
End Sub

How to pass arguments to a BackGroundWorker

Imports SpeechLib
Public Class Form1
Public vox = CreateObject("sapi.spvoice")
Private Sub cmdSpeak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSpeak.Click
Dim text2 As String = "Hello , This is a Text. Hello , This is a Text."
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub cmdPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPause.Click
vox.pause()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SVEPhoneme As Integer = 64
vox.EventInterests = SVEPhoneme
vox.AlertBoundary = SVEPhoneme
End Sub
Private Sub cmdResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdResume.Click
vox.resume()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
vox.Speak(Text, SpeechVoiceSpeakFlags.SVSFlagsAsync)
End Sub
End Class
How can I pass text2 to vox.speak?
In cmdSpeak_Click, pass text2 as a parameter to RunWorkerAsync
BackgroundWorker1.RunWorkerAsync(text2)
In BackgroundWorker1_DoWork, retrieve the value of the parameter
vox.Speak(DirectCast(e.Argument, String), SpeechVoiceSpeakFlags.SVSFlagsAsync)