How to Pass a Value from a Child Back to the Parent Form

Pass value from Child Form to Parent Form

after searching and trying many ways, this how i solve the problem

adding module to the solution

and then add public variables

Module Module1
Public x1 As String
Public x2 As String
End Module

the main form Form1 :

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f2 As New Form2
f2.ShowDialog()
End Sub
End Class

and finally the child form Form2 :

Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
x1 = TextBox1.Text
x2 = TextBox2.Text

Form1.TextBox1.Text = x1
Form1.TextBox2.Text = x2

Me.Close()
End Sub
End Class

How can I pass a value from child form to parent form and back?

Define a Property for child form

// In Parent Form
addTriple a1 = new addTriple();

a1.G = graphicdata //assign graphic data here
a1.BringToFront();
a1.ShowDialog();
g.SaveToFile(this.s1);


// In Child Form
public Graph G
{
get { return gr; }
set { gr = value; }
}

now you can have access to this anywere and assign data to it before showing the form

Passing a variable from Child Form to Parent Form

Maybe you can use an event. So each time the database name changes on the child form you can get a call back on the parent form

Child

public partial class Child : Form
{
public event DatabaseChangeHandler DatabaseChanged;
public delegate void DatabaseChangeHandler(string newDatabaseName);

public Child()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//When the database changes
if (this.DatabaseChanged != null)
{
this.DatabaseChanged("The New Name");
}
}
}

Parent

public partial class Parent : Form
{
private Child childForm;

public Parent()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
// Open the child form
childForm = new Child();
childForm.DatabaseChanged += childForm_DatabaseChanged;
childForm.ShowDialog();
}

void childForm_DatabaseChanged(string newDatabaseName)
{
// This will get called everytime you call "DatabaseChanged" on child
label1.Text = newDatabaseName;
}
}

How to pass form values from child component to parent in react

You component works fine if we define an onSubmit listener and handler for the form. We also need to call event.preventDefault() to stop the page from refreshing, that way the values actually get passed up to parent component when you call this.props.changeValue()

See codesandbox: https://codesandbox.io/s/gallant-ellis-76bdv

import React from "react";

class FormView extends React.Component {
constructor(props) {
super(props);
}

handleChange(e){
e.preventDefault()
this.props.changeValue(
e.target.name.value,
e.target.age.value,
e.target.gender.value
);
};

render() {
return (
<form onSubmit={this.handleChange.bind(this)}>
<label>
Name:
<input type="text" name="name" />
</label>
<label>
Age:
<input type="number" name="age" />
</label>
<label>
Gender:
<select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</label>
<input type="submit" value="Submit" />
</form>
);
}
}

export default FormView;

Transfer Value from childform to Parentform c#.net

You can make id a public property and assign the value in child form.

In parent form

//Declare public property 
public string ID { get; set; }

//Create child form in some method.
Form2 form2 = new Form2(this);
form2.Show();

In child form

Form1 m_parent ;
public Form2(Form1 frm1)
{
InitializeComponent();
m_parent = frm1;
}

m_parent.ID = textbox1.Text;

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

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


Related Topics



Leave a reply



Submit