How to Get value from another Form in C#
By FoxLearn 11/3/2024 2:31:30 PM 8.89K
To get a value from another form in a C# Windows Forms application, you typically use properties or methods in the target form to expose the data you need.
Open your Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "GetValueFromAnotherForm" and then click OK
Design your form as shown below.
Form1
This form will contain Add and Edit buttons to open the second form (frmAddEditStudent) and retrieve a value from it.
frmAddEditStudent
Create a student class to map data
public class Student { public string ID { get; set; } public string FullName { get; set; } }
Add code to handle your form as shown below.
Form1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace GetValueFromAnotherForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnAdd_Click(object sender, EventArgs e) { //Add a student to binding source using(frmAddEditStudent frm = new frmAddEditStudent() { StudentInfo = new Student() }) { if (frm.ShowDialog() == DialogResult.OK) studentBindingSource.Add(frm.StudentInfo); } } private void btnEdit_Click(object sender, EventArgs e) { Student obj = studentBindingSource.Current as Student; if(obj != null) { using(frmAddEditStudent frm = new frmAddEditStudent() { StudentInfo = obj }) { if(frm.ShowDialog() == DialogResult.OK) { studentBindingSource.EndEdit(); btnEdit.Focus(); } } } } } }
frmAddEditStudent
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace GetValueFromAnotherForm { public partial class frmAddEditStudent : Form { public Student StudentInfo { get; set; } public frmAddEditStudent() { InitializeComponent(); } private void frmAddEditStudent_Load(object sender, EventArgs e) { //Init data if (StudentInfo != null) { txtStudentID.Text = StudentInfo.ID; txtFullName.Text = StudentInfo.FullName; } } private void btnOK_Click(object sender, EventArgs e) { StudentInfo.ID = txtStudentID.Text; StudentInfo.FullName = txtFullName.Text; } } }
By using properties and the modal dialog pattern, you can easily pass values between forms in a C# Windows Forms application.
VIDEO TUTORIAL
- How to Print Text in a Windows Form Application Using C#
- How to fill ComboBox and DataGridView automatically in C#
- How to Read text file and Sort list in C#
- How to pass ListView row data into another Form in C#
- How to read and write to text file in C#
- How to make a Countdown Timer in C#
- How to Display selected Row from DataGridView to TextBox in C#
- How to Get all Forms and Open Form with Form Name in C#
Categories
Popular Posts
Material Lite Admin Template
11/14/2024
Freedash bootstrap lite
11/13/2024
RuangAdmin Template
11/17/2024
Responsive Animated Login Form
11/11/2024