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
Related
Public Class form1
Dim var1 as String = ""
Dim var2 as Boolean = True
Public Sub New(ByVal parameter1 as String, ByVal parameter2 As Boolean)
var1 = parameter1
var2 = parameter2
InitializeComponent()
End Sub
Private Sub form1_Load(sender as Object, e As EventArgs) Handles MyBase.Load
If var1 = "This String" Then
End If
End Sub
Private Sub btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Save.Click
call form1_load()
end sub
I was unable to call the form load with parameters, can somebody please help on this, thanks in advance
form1_Load is just a method that you can call like any other method. The Form_Load method requires a sender and an EventArgs parameter. You can just pass through the ones you get from Button_Click.
Private Sub btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Save.Click
form1_load(sender, e)
End Sub
But a better solution is to extract the logic into a new method and to call it in both, Form_Load and Button_Click.
Private Sub DoSomething()
If var1 = "This String" Then
End If
End Sub
Private Sub form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles form1.Load
DoSomething()
End Sub
Private Sub btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Save.Click
DoSomething()
End Sub
Note that calling form1_load does not raise the Load event. It only executes the logic inside this method.
I am a beginner and please show me how to pass the parameter object Person to Button.Click event. I'm using vb.net. Here is my code:
Public Class Form1
Public Class MyPersonClass
Public Name As String
Public Age As Integer
Public Title As String
End Class
Public Sub DisplayPerson(ByVal person As MyPersonClass)
Label1.Text = person.Name
Label2.Text = person.Age.ToString()
Label3.Text = person.Title
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class
You don’t – Button1_Click is an event handler, you’re not supposed to call it manually. It gets called, with predefined parameters, when a certain event occurs. You cannot really adapt these parameters, simply because it doesn’t make sense: the event would no longer know how to call the handler.
You can easily write your own method and pass any object to it, of course. And you’ve done exactly that with DisplayPerson.
Private ExamplePerson As MyPerson
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ExamplePerson = New MyPersonClass 'thanks Chris Dunaway for the correction
ExamplePerson.Name = "Test Name"
ExamplePerson.Age = 36
ExamplePerson.Title = "Title Name"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DisplayPerson(ExamplePerson)
End Sub
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)
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.....
When I click form1 button 1 to show form2 but it show The error is:
Object reference not set to an instance of an object.
Form1:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form2. Show()
Me.Hide()
End Sub
End Class
I suggest declaring it outside of the method, this way you can access it from other methods:
Private _Form2 as new Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me._Form2.Show()
Me.Hide()
End Sub
Try this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newform as New Form2
newform.Show()
Me.Hide()
End Sub
You can have more than one of the same form open, so you need to create an 'instance' of the form, before you tell it to show that instance.