Passing the data between windows forms - vb.net

I have a windows application.
In which I want to call one form from another and take Yes/No option from user
and that Yes/No choice again passed to the parent form.
How to do this ?
I have tried by creating object but it dont work.
Please check below image...
I have Call conformation form on click of final button, when user choose ok/Cancel that value passed to the again parent form and will take desired action depends on choice.
how to take input from this child form ?

The easiest way is to set a variable to Public when you instantiate it.
Public myVariable as String = ""
Then you would access it from anywhere.
From your own form
Me.myVariable = "" 'Whatever you would like to set
From another Form
Form1.myVariable = "" 'Whatever you would like to set
'or
FormName.Variable = ""

Dim confirmModal = new ConfirmModal
Dim result = confirmModal.ShowDialog()
If result = OK then resltValue = confirmModal.ResultValue
ResultValue is whatever you want to pass to the parent.

You can use a public variable in a module or in the parent form that is accessed by both forms to store the yes/no value.
public returnCode as boolean
If the variable is in the parent form, it can be referenced in the child form using the parent form's name:
form1.returnCode = True

Related

How to get selected radio button value in Visual Basic

I have a lot of radio buttons named after numbers between 0 and 20. And pretty much the same code to run when they're selected. If I could somehow use something like "checkedRadioButton.value" things would be a lot easier.
I have some pictures to show, when radiobutton1 is selected picture1 will be shown. I get the pictures with
OpenFileDialog1.Multiselect = True
OpenFileDialog1.ShowDialog()
and want to use something like
PictureBox1.ImageLocation = OpenFileDialog1.FileNames(checkedRadioButton.value)
One option is to set the Tag property of each control to the associated value. Tag is a general-purpose property intended to store arbitrary data, so you can use it for whatever you like. It is type Object, so you'll need to cast whatever you get from it. To get the associated Integer value from the checked RadioButton on the form you could do this:
Dim number = CInt(Me.Controls.OfType(Of RadioButton)().First(Function(rb) rb.Checked).Tag)
If your RadioButtons are in some other container, e.g. a Panel or GroupBox, then you must use that container's Controls collection instead.
EDIT:
As the end game here seems to be to get a file path, it would make sense to assign the file path to the Tag property in the first place. You can then get the file path directly from the RadioButton, e.g.
PictureBox1.ImageLocation = CStr(Me.Controls.OfType(Of RadioButton)().First(Function(rb) rb.Checked).Tag)
Another option is to create your own derived control and add a dedicated property, e.g.
Public Class FilePathRadioButton
Inherits RadioButton
Public Property FilePath As String
End Class
You can then use that class instead of the standard RadioButton class and use the FilePath property instead of the Tag property. There's then no need for a cast:
PictureBox1.ImageLocation = Me.Controls.OfType(Of RadioButton)().First(Function(rb) rb.Checked).FilePath

How to find parent form name ? vb.net

I want the name of the parent form in vb.net . Where I came from.
Like
`if parent-form.name = 'something' then
do something.
else
do something else.`
It's not a MDI form.
Edit:
I want exactly like that:
parentForm.vb:
chidform.showdialog()
In childform there is textbox
childForm.vb:
if parentForm is parentForm1 than fill textbox.text = 2
else fill textbox.text = 3
You will understand what I want.
Assumption: I worked under the assumption that this is an MDI application and you would want to read the name of the top parent and for reasons unknown you cannot refer to a singleton reference to the form (for instance current form runs from a library).
I wrote an extension mode to get the top parent since the immediate parent of an MDI child window is an MDIClient without a name.
<Extension>
Public Function GetTopParent(currentControl As Control) As Control
Dim parent As Control = Nothing
Do While currentControl IsNot Nothing
parent = If(currentControl.Parent, parent)
currentControl = currentControl.Parent
Loop
Return parent
End Function
Then when you need the name of the parent you can do the following.
MessageBox.Show(Me.GetTopParent().Name) ' This just shows the name but you can do your comparison here.

refer to name with variable in visual studio 2010 vb

I'm trying to assign text from "comp" in the form "home" to a textbox with the name "d1" in the form "home".
but this needs to be done with a counter in the form "home".
The code is in a module.
What I've tried=
home.controls("d" & home.counter).text = home.comp.text
I keep getting an error:
use the new keyword to create an object instance ==> the textbox exists in the form
check to determine if the object is null before calling the method ==> the textbox is empty
get general help for this exception
You could use Controls.Find:
Dim controls = home.Controls.Find("d" & home.counter, True)
If controls.Length > 0 Then
Dim txt = TryCast(controls(0), TextBoxBase)
If txt IsNot Nothing Then
txt.Text = home.comp.text
End If
End If
However, normally i would not use this approach since it's error-prone. Why don't you provide a public property in the Home-form that you can access? This property would get/set the TextBox' Text.
For example:
Public Property HomeCompText As String
Get
Return txtHomeComp.Text
End Get
Set(value As String)
txtHomeComp.Text = value
End Set
End Property
Now you can use this clear, safe and maintainable code:
home.HomeCompText = home.comp.text
You could even change the underlying control.

Pass variable From child to parent, Like a Function VB.NET

I have created 2 forms (Parent & Child), i want to store the unique textbox value to a variable into the parent form.
Also like this:
Parent Code:
dim passed_value = new childform()
passed_value.show()
On close:
refresh passed_value variable using childform textbox value.
You can Do the Following to accomplish the Task :
Declare a String variable in the child form.
Public value As String
Use ShowDialog() in the Main Form to show the Child form.
Dim frm As New Form2
frm.ShowDialog()
[Set the value in your form as per your needs]
value = "New Value"
Now get the value variable from the Child form and set the textbox text according to it.
TextBox1.Text = frm.value
I would make it a property and give it the necessary access, which to me looks to be ReadOnly.
Private _myValue As DataType
Public ReadOnly Property MyValue() As DataType
Get
Return _myValue
End Get
You could access it as such: myForm.MyValue.

Passing Data between VB.NET forms

I have a form that has a button, when clicked it pops up a Dialog Form. Within this dialog form the user needs to select some data and when the user is finished they click the OK button. Once they click the OK button it needs to return an integer back to the previous form.
I created a Dialog Form and tried calling it via the code below:
Dim intResult as Integer = frmData.ShowDialog()
Debug.Writeline(intResult)
However, it seems I can only return DialogResults (Abort, Cancel, Ignore...)
I was wondering how I can try this without having to create a public variable and storing the result there.
Create a property on the Dialog that will return the value.
If frmData.ShowDialog() Is Not DialogResult.Cancel
Dim value as integer = frmData.MyProperty
...
Endif
Create an event on your dialog form, subscribe to it on your main form, and raise it on your dialog with the appropriate data contained within the event arguments.
Create a cutom Dialog to your project(add/new element/Windows Forms/Dialog).
Then create an instance from it, call showDialog and check if its DialogResult is Windows.Forms.DialogResult.Ok. You can access all of its controls, for example:
Dim d As New Dialog1
Dim result As DialogResult = d.ShowDialog(Me)
If result = Windows.Forms.DialogResult.OK Then
Dim selectedText As String = d.ComboBox1.SelectedText
End If