Passing Simple Integer to Form1 From Form2 - vb.net

Just trying to figure out how to pass one simple integer(StartingTeam) from Form2 to Form 1.
Form 2 Code
Public Class frmTeamChoose
Public StartingTeam As Integer
Public Sub btnTeam1_Click(sender As Object, e As EventArgs) Handles btnTeam1.Click
StartingTeam = 1
End Sub
Public Sub btnTeam2_Click(sender As Object, e As EventArgs) Handles btnTeam2.Click
StartingTeam = 2
End Sub
End Class
Form 1 is called Form1

Although you need to provide more info I 'll try to help you:
I suppose that you have open Form2 from Form1:
'In form1:
Dim k as integer=Form2.StartingTeam
An improved solution is to create a property:
Form 2 code:
Private miStartingTeam
Public Property StartingTeam As Integer
Get
Return miStartingTeam
End Get
Set(ByVal value As Integer)
miStartingTeam = value
End Set
End Property
Then your code as it is.
In Form1:
'Open Form2
Dim f2 as new Form2
'f2.StartingTeam=1 'if you want to set a value before f2 opening
f2.Show
'Get StartingTeam from f2
dim k as integer=f2.StartingTeam
Let me know if you need anything else

Related

Method for comparing text boxes in vb.net

I need help, so I can compare the text from the text booths that are in these 3 groupboxes.
This is what my form looks like:
Private Sub frmCompareAdress_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim firstGroupBoxFields() As String = {TextBoxVorname1.Text, TextBoxName1.Text, TextBoxStrasse1.Text, TextBoxPLZ1.Text, TextBoxOrt1.Text, TextBoxTelefon1.Text}
Dim secondGroupBoxFields() As String = {TextBoxVorname2.Text, TextBoxName2.Text, TextBoxStrasse2.Text, TextBoxPLZ2.Text, TextBoxOrt2.Text, TextBoxTelefon2.Text}
Dim thirdGroupBoxFields() As String = {TextBoxVorname3.Text, TextBoxName3.Text, TextBoxStrasse3.Text,TextBoxNr3.Text, TextBoxPLZ3.Text, TextBoxOrt3.Text, TextBoxTelefon3.Text}
ComparisonFieldsfirstGroupBoxFields, secondGroupBoxFields, thirdGroupBoxFields)
End Sub
Public Sub ComparisonFields(ByVal firstGBFields() As String, secondGBFields() As String, thirdGBFields() As String)
Dim notCompareFileds = String.Join(", ", firstGBFields.Except(secondGBFields))
'what to do next? I'm a little confused if I'm on the right track
End Sub
Now if, for example, the textBox1Vorname.Text is different from the textBox2Vorname.Text or textBox3Vorname.Text, I want to mark them in red.
I imagined it in a way, to compare as an array, and I put it in 3 array values from textboxes.
Can anyone help me further with this function?
Thank you for your help.
Public Shared Function HaveSameText(ParamArray controls() As Control) As Boolean
Return controls.All(Function(c) c.Text = controls(0).Text)
End Function
Public Shared Sub ColorAllRed(ParamArray controls() As Control)
For Each c In controls
c.ForeColor = Color.Red
Next
End Sub
Public Shared Sub ColorAllRedIfNotSameText(ParamArray controls() As Control)
If Not HaveSameText(controls) Then ColorAllRed(controls)
End Sub
Private Sub frmCompareAdress_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ColorAllRedIfNotSameText(TextBoxVorname1, TextBoxVorname2, TextBoxVorname3)
ColorAllRedIfNotSameText(TextBoxName1, TextBoxName2, TextBoxName3)
ColorAllRedIfNotSameText(TextBoxStrasse1, TextBoxStrasse2, TextBoxStrasse3)
ColorAllRedIfNotSameText(TextBoxPLZ1, TextBoxPLZ2, TextBoxPLZ3)
ColorAllRedIfNotSameText(TextBoxOrt1, TextBoxOrt2, TextBoxOrt3)
ColorAllRedIfNotSameText(TextBoxTelefon1, TextBoxTelefon2, TextBoxTelefon3)
End Sub

VB.NET Pass values to variable of dynamic forms

Apologies if this is newbie question as i'm a newbie on .net myself. So here it goes, I have 2 Forms. Form 1 and Form 2. I'm currently in form 1 and I want to pass a value to a variable on form 2. Here's the declaration on forms.
Public Sub Form1
Dim TempForm as Form = Form2
End Sub
Public Sub Form2
Dim Id as Integer
End Sub
So I have the Form2 that can be accessed using TempForm and I know there is a variable named Id in Form 2 and I want to pass a value to it before opening. Please note that I cannot pass the value to the Form 2 directly and it should be to Temp form as I intend to make it dymamic so I can also use it to open other forms. Thanks a lot!
Use this code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim NewForm As New Form 'make a new form
'assign the value 50 to the variable ID
Dim IDx As New NewForm(50)
End Sub
End Class
'for new form
Public Class NewForm
Public ID As Integer
'Here's the class constructor
Public Sub New(Value As Integer)
ID = Value
'use this ID however you want
End Sub
End Class

How can i pass parameters from datagridview to an open or main form without opening new form

I was thinking how can i pass parameters using constructors from datagridview which is in another form to my main form without opening new one. here's my code
-----------Main form constructor----------------------------------------
Public Sub New(customerID As Integer, fullName As String, phoneNumber As String, emailID As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_customerID = customerID
_fullName = fullName
_phoneNumber = phoneNumber
_emailID = emailID
End Sub
-----------------------------second form edit button click ----------------------------
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
Dim formMain As New frmMain(CInt(Me.dgvCustomerInfo.Item(0, Me.dgvCustomerInfo.SelectedRows(0).Index).Value),
Me.dgvCustomerInfo.Item(1, Me.dgvCustomerInfo.SelectedRows(0).Index).Value,
Me.dgvCustomerInfo.Item(2, Me.dgvCustomerInfo.SelectedRows(0).Index).Value,
Me.dgvCustomerInfo.Item(3, Me.dgvCustomerInfo.SelectedRows(0).Index).Value)
Me.Hide()
'formMain.Hide()
formMain.Show()
Thanks
You could create a property in your second form which is a list of the type of value that you want to pass and then when you create the form, assign your values to the property. Finally process the property using code in the form2.Shown event like this
Public Class Form1
Private Sub test()
Dim f2 As New form2
f2.formparameters = {4, 5, 6, 7}
f2.Show()
End Sub
End Class
And in your 2nd form write something like this..
Public Class form2
Friend Property formparameters() As Integer()
Private Sub form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'process parameter data here
Me.Update()
End Sub
End Class

Passing parameters between two forms in VB.Net

I currently have about 5 forms in my application. I'm building a 6th form - frmSummary however, I'd like to be able to access it from all forms. in frmSummary I am planning to add a DataGridView, where I'll be displaying data related to that form. I'm thinking that I should either create a global variable such as
dim FrmName as String
In each form I would have a cmdSummary button so that On click_event, I would do something like
frmName ="CustomerInfo"
Currently the way my application is set up is that I hve a mdiForm and within it, each form is a child so on opening new forms I do something like...
Private Sub cmdSummary_Click(sender As Object, e As EventArgs) Handles cmdSummary.Click
Dim NewMDIChild As New frmClientEligibilityReferral()
frmName = "CustomerInfo" --since this will be comeing from frmCustomerInfo
NewMDIChild.MdiParent = MDIform1
NewMDIChild.Show()
MDIForm1.Show()
End Sub
So I do something like that on opening my new form. My question is how can I pass the parameter to my form frmSummary....here's currently what I'm trying to accomplish....
Private Sub FrmSummary_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.MdiParent = MDIForm1
InitializeComponent()
'Here I want to call a function to load the datagridView(with g_frmName)see below...
call LoadDataGrid(frmName)
End Sub
Is something like that a smart idea? Or should I/Can I directly call the function from the previous form?
Just trying to see if I'm on the right track, if not, how can i do it in a sound way?
If there is only one frmSummary, you could make it a singleton.
In frmSummary, put the following code:
Private Shared _instance As frmSummary
Private Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Public Shared Function GetInstance() As frmSummary
If _instance Is Nothing Then
_instance = New frmSummary()
End If
Return _instance
End Function
Public Sub PutDataInGrid(data As Object)
Me.DataGridView1.' put data in it
End Sub
And you would access it from other forms like this
Dim myFrmSummary = frmSummary.GetInstance()
myFrmSummary.PutDataInGrid(myData)
If I understand the question correctly....
You can just set the required parameters in the New declaration sub (Where InitializeComponent() is supposed to be). On your form, declare variables and set one to each of the parameter values, and set up your form this way..
An example might be;
Public Class frmSummary
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 frmSummary_Load(sender as Object, e As EventArgs) Handles MyBase.Load
If var1 = "This String" Then
If var2 = False Then
sql = "SELECT * FROM myTable"
' Rest of your code to get the DGV data
DataGridView1.DataSource = Dt
Else
End If
End If
End Sub
Again, I may have misunderstood the question, so apologies if that is the case.

Can't Remove Listview Item From Second Form

I am having an issue when trying to delete ListView Items from a second form.
For example, if I use the following command on Form1 it works:
Listview1.SelectedItems(0).Remove
However, if I attempt to remove from Form2 like so:
Form1.Listview1.SelectedItems(0).Remove
I get the following error:
"Invalid argument=value of '0' is not valid for 'index'. Parameter name: index"
I then tried to get a count of items from the listview on Form2 and it gives me a return of 0
Form1.Listview1.Items.Count
I'm not sure what my problem is.
Update
I have posted a brief example of my code (using your suggestion as I can understand it):
frmShowMessages
Private Sub ViewMessage()
Dim frm As New frmViewMailMessage
frm.Show()
End Sub
Public Sub DeleteItem(ByVal index As Integer)
lsvReceivedMessages.Items(index).Remove()
End Sub
frmViewMessage
Private instanceForm as frmShowMessages
Private Sub frmViewMailMessage_Load(sender As Object, e As EventArgs) Handles MyBase.Load
instanceForm = New frmShowMessages()
End Sub
Private Sub cmdDelete_Click(sender As Object, e As EventArgs) Handles cmdDelete.Click
instanceForm.DeleteItem(_index)
End Sub
Hopefully my code can help identify where my issue is.
In VB.net usually you get a default Form instance for each of your Form. Probably you are creating an instance of Form1 and then you are trying to access ListView1 of default instance.
E.g.
Sub ButtonClick()
Dim f As New Form1()
f.Show()
' at this point if you access f's ListView you will get correct count
f.ListView1.Items.Count
' however if you try to access default instance it will NOT have any item
Form1.ListView.Items.Count
End Sub
It means your instance f is NOT equal to default Form1 instance.
Solution can be, make the f variable as class level variable and use it everywhere. Or if Form1 will have only 1 instance, then you can use the default instance everywhere.
Personally I would NOT go with direct control accessing over forms. I would create a Public method which should return the data as list to the caller, in this case your Form2.
UPDATED-2:
As per your given scenario, I am simplifying things for you, and doing implementation using Event.
Public Class frmShowMessages
Private Sub btnOpenMessage_Click(sender As System.Object, e As System.EventArgs) Handles btnOpenMessage.Click
Dim frmView As New frmViewMessage(Me.ListView1.SelectedItems(0).Index)
AddHandler frmView.MessageDeleted, AddressOf DeleteMessageHandler
frmView.Show()
End Sub
Private Sub DeleteMessageHandler(sender As Object, e As frmViewMessage.MessageDeletedEventArgs)
Me.ListView1.Items.RemoveAt(e.MessageIndex)
End Sub
End Class
Public Class frmViewMessage
' a class which will be used for Event communication
Public Class MessageDeletedEventArgs
Inherits EventArgs
Public Property MessageIndex As Integer
Public Sub New(ByVal iIndex As Integer)
MyBase.New()
Me.MessageIndex = iIndex
End Sub
End Class
' main event which will alert the parent that a message deletion should be done
Public Event MessageDeleted As EventHandler(Of MessageDeletedEventArgs)
' private variable that will hold the MessageIndex
Private Property MessageIndex As Integer
' method that is responsible to raise event
Protected Overridable Sub OnMessageDeleted()
RaiseEvent MessageDeleted(Me, New MessageDeletedEventArgs(Me.MessageIndex))
End Sub
' we want to create this Form using the MessageIndex of ListView
Public Sub New(ByVal iMessageIndex As Integer)
Me.InitializeComponent()
Me.MessageIndex = iMessageIndex
End Sub
' the delete button will raise the event to indicate parent that
' a deletion of message should be done
Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
Me.OnMessageDeleted()
End Sub
End Class