VB.Net Add value from datagridview to listbox in another form - vb.net

I have a trouble on VB.Net desktop programming, I have a Form with a ListBox, Called Form1, this form have a button that will appear another form with DataGridView inside that new Form, we will call this as Participant. Then I have a scenario, when User click the Button, it will call the Participant Form, and when user click the datagridview row in the Participant Form, it will get the value from that row and add the value to the ListBox in Form1, I have done getting the value from the row, but I can't add the value to the ListBox. This is my code to help you understand what I have done.
This is the code that will call the Participant Form :
Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
Dim myForm As New Participant
If myForm Is Nothing Then
myForm = New Participant
End If
myForm.Show()
End Sub
And this is the code that will add the value from DataGridView in Participant Form to the ListBox in Form1
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim yourColumnIndex As Int32 = 1
If e.ColumnIndex = yourColumnIndex Then
participant_Share = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
Form1.ListBox1.Items.Add(participant_Share)
End If
End Sub
But this code cannot insert the value to the ListBox, Please help me if you know how to solve this.
Thank you

You can change Participant constructor to accept a ListBox as parameter and store it in a local variable.
Private yourListBoxReference As ListBox
Public Sub New(ByVal yourListBox As ListBox)
InitializeComponent()
yourListBoxReference = yourListBox
End Sub
So you can call this form as:
myForm = New Participant(yourListBox)
And use the local variable to add an element to your ListBox:
yourListBoxReference.Items.Add(participant_Share)

Related

When a ComboBox value is selected, output particular text (Visual Basic)

I'm creating a decision interface for clinical support with numerous amounts of combo boxes with boolean values 'Yes|No". However I want it so if the user choses either yes or no, a button can be clicked at the bottom and then another windows form appears and says whether they have cancer or not.
For example, if the user clicks 'yes' in the comboBox and then clicks the 'submit' button, another form will appear and the text box will say whether they have cancer or not. Would anyone be able to supply an example of how this would work? I have the form with the combo boxes, a button that links to another form, and a textbox inside the second form....but I can't get the information between the two forms.
The code I have at the moment is
Private Sub submit_Click(sender As Object, e As EventArgs) Handles submit.Click
If (RectalBleeding.SelectedItem = "Yes") Then Outcome.OutcomeBox.Text = "you have cancer" End If Outcome.Show()
End Sub
Particular combo box im trying to link is called 'RectalBleeding'. The button on the decision interface form is called 'submit' the second form is called 'outcome'. The box inside outcome is called 'outcomeBox' I want it so that if 'Yes' was chosen in the comboBox, the user clicks 'submit' second form appears and in the text box it says "you have cancer"
Thanks!
You can use public properties and constructors to pass values between two forms. Below is some code that uses these tools for your purpose.
Here is the code for the main form:
Public Class Form1
Private frm As Outcome
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddDecision()
End Sub
Private Sub AddDecision()
RectalBleeding.Items.Add("Yes")
RectalBleeding.Items.Add("No")
End Sub
Private Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
If RectalBleeding.Text = "Yes" Then
frm = New Outcome("You have cancer")
Else
frm = New Outcome("You don't have cancer")
End If
frm.Show()
End Sub
End Class
and here for the outcome form
Public Class Outcome
Private _Decision As String
Public Property Decision() As String
Get
Return _Decision
End Get
Set(ByVal value As String)
_Decision = value
End Set
End Property
Private Sub Outcome_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillTextBox()
End Sub
Public Sub New(ByVal results As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Decision = results
End Sub
Private Sub FillTextBox()
outcomeBox.Text = Me.Decision
End Sub
End Class

passing data from textbox in form1 to textbox in opened form2 in visual basic

i have form1 to enter movies details
in this form1 i have a textbox called NVideosGenres
through this textbox i can open the form2 with a space
the form2 contains 5 combobox to let the users choose the genres if there is more then one
when user choose the genres they will be applied to a textbox in the same form like this way
for example i choose from three combobox
action - war - western
so now i have a problem because i know that to pass value i should do this
Videos.NVideosGenres.text = me.FinalGenres.text
me.close()
when i click on the button the form2 will close but the data don't pass to NVideosGenres in the form1
any help??
There are many, many ways to do this. One of the easiest ways would be to create a property on Form2 that holds a reference to your main calling form. Then you can set the values of the textboxes (or whatever) directly from Form2. This is definitely not the best way to do it, but it certainly is quick and easy.
Private Sub btnShowForm2_Click(sender As System.Object, e As System.EventArgs) Handles btnShowForm2.Click
'Create a new instance of Form2 (called frmDetail in this example).
Dim frm2 As New frmDetail()
'Set the MyParentForm property of Form2 to this form.
frm2.MyParentForm = Me
'Show the form.
frm2.ShowDialog()
End Sub
Now in Form2, when you click the "OK" button to close the form, you can use the property that references your parent form to set the values directly.
' Property to hold a reference to the calling form.
Private mParentForm As frmMain
Public Property MyParentForm() As frmMain
Get
Return mParentForm
End Get
Set(ByVal value As frmMain)
mParentForm = value
End Set
End Property
' When I click the OK button, I will store the value of my various textboxes to properties.
Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
'Set the value of the Genre textbox on the parent form to the value
'of the textbox on my current form.
MyParentForm.txtGenre.Text = txtGenre.Text
Me.Hide()
End Sub

How can I pass value from DataGridView to another DataGridView on another form?

How can I pass values from a DataGridView to another DataGridView on another form? Please I need this answer deadly.
I have a form with a DataGridView, I am using this as invoice so at the event of end_edit for the field of item name I make a search by the first letters of this item name and put the result in another DataGridView on another form. I need to when I choose the item that I need from the form number 2(search form) to move to the same cell in the form number 1(invoice form) ?????
Thank you
You have to be more specific about your Question
given this scenario
form1 with a datagridview and form2 with a datagridview
you open form2 from form1 you would go to form2 and put sub new in it with reference to the main form like
Private f1 As Form1
Public Sub New(ByRef f As Form)
InitializeComponent()
Me.f1 = f
End Sub
on Form1 show your Form
Dim f2 As New Form2(Me)
f2.Show()
then on form 2 you have access to form1
assuming both tables have the same Datasource` bound
you can go on the ClickCell event from your Datagridview on Form2
and put in something like this to be at the same position on both DGV's
Private Sub xDataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles xDataGridView.CellClick
f1.xBindingSource.Position = Me.yBindingSource.Position
End Sub
this however will only work if you don't allow sorting of the columns otherwise it will be in defferent rows.
if you want to pass a value from dgv on form2 to dgv in form1
then you can do something like this
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button2.Click
f1.xDataGridView.CurrentRow.Cells("MyCell").Value = Me.xDataGridView.CurrentRow.Cells("MyCell").Value
End Sub

Passing variables between windows forms in VS 2010

I have two forms. Form 1 allows the user to pick an employee from a dropdown combo box. That employee is then passed onto Form 2 where the user enters additional information regarding that employee. That data will then be passed into a SQL table.
On form 1 I have:
Dim ChangeJobInfo As New Form2(Employee)
ChangeJobInfo.Show()
On Form 2 I have:
Public Sub New(ByVal Employee As String)
MsgBox(Employee)
End Sub
The variable passes just fine. The issue is that nothing shows up on the new form. When I setup Form2, I added a combobox, date picker, two text boxes, submit button, etc., but when the form loads it is completely blank. No errors, the MsgBox returns the right result, but none of my gui elements show up. If I change the code on form 1 to Form2.show() I see the form as laid out in the designer.
Any ideas on how to get those items to show up?
Change your code in Form2.vb for the New sub to this:
Public Sub New(ByVal Employee As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
MsgBox(Employee)
End Sub
If you don't call InitializeComponent(), your complete GUI is not going to render.
You don't even have to use the InitializeComponent or New functions.
I have made an example to show how easily this can be done.
Clicking "Show Form" results in the below:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Form2.Show()
End Sub
which is simply used to display the second form.
By clicking "Pass Data" results in the following code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Form2.Label1.Text = TextBox1.Text
End Sub
As shown above you can pass the data directly from control to control. The same idea can be used with variables too.
I am late but I think this answer can help.
For example, Form1 named "menu" opens and passes variable to Form2 named "ordine".
The variable to pass is "hotel"
In menu on button_click
Dim ordinef As New Ordine()
If ordinef Is Nothing Then
'control that form is not opened yet if open close before
ordinef = New Ordine()
Else
ordinef.Close()
ordinef = New Ordine()
End If
ordinef.hotel = hotel
ordinef.Show()
In Form2 (Ordine):
Private Sub Ordine_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
public hotel as string
msgbox hotel
That's it!!

combobox not being populated

I have a windows form project with a main form. There is a textbox leave event that opens a new form. In that new forms load event i have a combobox item loop that populates the combobox items. It works perfectly fine if run on the main form but doesnt work on the second form. Why doesn't the comboboxes on the secondary form populate when that form is opened via a textbox_leave event from the main form?
this is the leave event
Private Sub tbChartTitle_Leave(sender As Object, e As System.EventArgs) Handles tbChartTitle.Leave
If Not tbChartTitle.Text = Nothing Then
frmTitleAttributes.Show()
End If
End Sub
This is the code that populates one of the comboboxes on the second form (it works if run on a combobox on the main form)
Private Sub frmTitleAttributes_Load(sender As Object, e As System.EventArgs) Handles Me.Load
InitializeComponent()
AddFonts()
End Sub
Private Sub AddFonts()
' Get the installed fonts collection.
Dim allFonts As New Drawing.Text.InstalledFontCollection
' Get an array of the system's font familiies.
Dim fontFamilies() As FontFamily = allFonts.Families
' Display the font families.
For i As Integer = 0 To fontFamilies.Length - 1
cbxTitleFonts.Items.Add(fontFamilies(i).Name)
Next
End Sub
make sure that the Load handler is hit after you show your form (use break point)
also you can try to call it in the Shown event
Private Sub frmTitleAttributes_Shown(sender as Object, e as EventArgs) _
Handles frmTitleAttributes.Shown
AddFonts()
End Sub