Passing variable from one form to another in vb.net - vb.net

I've looked this question up 10 times but each answer is too specific to the question.
I have two public classes, one per form.
The first form has a textbox and two buttons:
Public Class frmAdd
Public addvar As String
Public Sub UltraButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNote.Click
If txtAdd.Text = String.Empty Then
MsgBox("Please complete the notes textbox!")
Else
addvar = txtAdd.Text
MsgBox(addvar)
Close()
End If
End Sub
Public Sub UltraButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
End Class
In the second form I want to take that addvar variable and say
Public Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
frmAdd.show()
me.textbox1.text = addvar
How do I get this to work in vb.net?

You need to read the field out of the frmAdd value
Me.textbox1.Text = frmAdd.addvar
Note that this value won't be available until the form has completed, and is closed (Me.close). Hence you want to use ShowDialog (doesn't return until form is closed) vs. Show (which returns immediately after displaying the form).
frmAdd.ShowDialog()
Me.textbox1.Text = frmAdd.addvar

Related

Passing object as parametar in VB.NET

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

Transfering data form2 radiobutton to form4 label in vb.net

I have two radio buttons in form2 and if one of them is checked, I want to display information in a label in form4. please help
Here is what I have :
Public Class Form4
Public Form2 As New Object
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Me.Form2.RadioButton1 = CheckState.Checked Then
Label2.Text = "You have been successfully registered for the following modules in the first semester."
Else
Label2.Text = "You have been successfully registered for the following modules in the second semester."
End If
Here is an easy way:-
Form 2
Public Class Form2
Dim check As Integer = 0
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
check = 1
Else
check = 0
End If
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton1.Checked = True Then
check = 2
Else
check = 0
End If
End Sub
Public Function sendCheck() As Integer
return check
End Function
End Class
Form 4
Public Class Form4
Dim receivedCheck As Integer = Form2.sendCheck
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If receivedCheck = 1 Then
Label2.Text = "You have been successfully registered for the following modules in the first semester."
ElseIf receivedCheck = 2 Then
Label2.Text = "You have been successfully registered for the following modules in the second semester."
End If
End Sub
End Class
Explanation
The error you may be getting in is the condition in If statement. What you have done is, you have used Me ahead of Form2. Me is used to refer objects within the form and for referring to other forms we use their name.
Remember, a form can never call itself with its name. It will call itself by Me. But here, you have used both Me and Form2 (Which means Form2 is an object within Form4) which we don't have to do.
So, remove the two letters Me and your program will work fine.
But, it would be better if you generate a checkedchanged event. This is how I would have written it, but you can write anyways you want, just the main problem that you may be getting was the use of Me and Form2 both.
This is how I would have written it:
Code And Example
Public Class Form2
Private Sub CheckedChanged () Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
If Form2.RadioButton1.Checked = True Then
Label2.Text = "You have been successfully registered for the following modules in the first semester."
Else
Label2.Text = "You have been successfully registered for the following modules in the second semester."
End If
End Sub
End Class
I hope that you will understand!

Add a checkbox to allow the labels to be updated whenever the input text changes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am very new to programming language and am in my first Programming class at my university. For a project we are creating a VERY simple project where the user enters words and the program puts them together. It's day one stuff I know but I'm trying to go for the extra credit and "Add a check box to allow the labels to be updated whenever the input text changes".
I have a program that when you type in two separate words in separate text boxes it displays each word individually and then also the two words combined at the bottom. Our professor wants my to add an check box option at the bottom that when clicked with make it so when the users types the two words they display automatically without clicking the display button.
I know this is easy stuff, but any help would be much appreciated.
Thank you for your help.
Public Class form1
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblLeft.Click
End Sub
Private Sub lbl2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblRight.Click
End Sub
Private Sub txtBoxLeft_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBoxLeft.TextChanged
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim strTxtBoxLeft As String
strTxtBoxLeft = txtBoxLeft.Text
Dim strTxtBoxRight As String
strTxtBoxRight = txtBoxRight.Text
lblLeft.Text = strTxtBoxLeft
lblRight.Text = strTxtBoxRight
lblCombo.Text = strTxtBoxLeft & " " & strTxtBoxRight
End Sub
Private Sub chkbox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox.CheckedChanged
If chkbox.CheckState = False Then
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Try this:
Public Class Form1
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
combineText()
End Sub
Private Sub chkbox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkbox.CheckedChanged
If chkbox.CheckState = False Then
lblCombo.Text = ""
End If
End Sub
Private Sub txtBox_TextChanged(sender As Object, e As EventArgs) Handles txtBoxRight.TextChanged, txtBoxLeft.TextChanged
If chkbox.CheckState Then
combineText()
End If
End Sub
Private Sub combineText()
lblCombo.Text = txtBoxLeft.Text & " " & txtBoxRight.Text
End Sub
End Class
Note: I have consolidated your logic down to just grab the text from the text boxes themselves. There is also one handler that handles text changing in either of the text boxes. Finally, there is a single method that handles combining the values of the two text boxes together, either via the text changed event if the check box is checked or if the user clicks the update button. Also, if the user unchecks the check box, then it will clear the results. Then when the user clicks update it will display the combined text again.

TextBox1 doesn't show the listbox1 value

I'm Visual Basic beginner, yesterday i wrote a dictionary that give you the opposite of the entered word, so i designed the form to look like this
[url]http://img651.imageshack.us/img651/6115/errorbp.jpg[url]
by the way i made a two list boxes as databases so the code will compare if the textbox1.text = listbox1.text then it will command textbox2 to append the value of the listbox : textbox2.appendtext(listbox2.text) but nothing happens
my code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TnsBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = TextBox3.ToString Then
TextBox2.AppendText(ListBox2.Text)
ElseIf TextBox1.Text = TextBox4.Text Then
TextBox2.AppendText(ListBox1.ToString)
End If
End Sub
Private Sub AddBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Add(TextBox3.Text)
ListBox2.Items.Add(TextBox4.Text)
End Sub
End Class
the point of the code is ok cuz yesterday i finished the coding and the programs works fine but i forget to save it so i coded again and every thing above happens
this is the yesterday program
http://www.mediafire.com/?tavne7xjyth7y7v
virustotal link:
https://www.virustotal.com/file/1d39429ae1498a744e1556188b7e8914526b7e2fbb2d4904c2b4ea22fb278dc7/analysis/1346676641/
Initially you are setting the textbox text to "ListBox" without choosing anything specific so it is calling ToString() on the listbox which is why you get that.
I would change the method so that you have a Dictionary variable like so:
Public Sub Translate(input As String)
TextBox2.Text = OppositeDictionaires(input)
End Sub
Public OppositeDictionary As New Dictionary(Of String, String)
'Call as Add(TextBox3.Text, TextBox4.Text)
Public Sub Add(input As String, opposite As String)
OppositeDictionary.Add(input, opposite)
End Sub
Call add from your event and then Translate from your translate event. You should then get your output as intended, still add them to the listboxes if you want to display to the user but handle the translation in the code behind through a dictionairy object.
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

How to retrieve the value from one form to another in vb.net

I have the problem to retreive the string from one form to another. here is my code: What's wrong with this?
Public Class Form3
Dim unit As String
Public itmname As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj1 As New Form4
itmname = tb1.Text
MessageBox.Show(itmname)
obj1.Label1.Text = itmname
obj1.Show()
End Sub
End Class
Public Class Form4
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Form3
MessageBox.Show("item name:" + .itmname)
Label1.Text = .itmname
End With
End Sub
End Class
You shouldn't have to do any special code in Form4 to set the value. you already set the textbox value from from3's button click event. Setting again is just overwriting it with a blank value from a newly instantiated form. Just clear all the code you have listed in Form4's load event and it should work.