I am new with C# and this question must be a common question but it really takes my time. With this scenario i cannot go further with my project.
What I need is, I just want to pass the value from form2 listview to form1 textboxes by calling the method in form1 in button_Click event. Whereas, when I click the button that will trigger my code I get nothing .
Problems Encountered:
1: When I declare Form1 f1 = New Form1(), The compiler still compile the code wherein my method is to be called in Button_Click Event in Form2. But When I Clicked the button there's nothing changes happened in my textboxes.
2: When I declare Form1 as Public Form1 f1;, and Clicked the button I'm getting a NullReferenceException .
3: I need my Form2 to be showed as ShowDialog();
Any help will be appreciated.
My code in Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;
namespace Practice_CS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnViewList_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
public void setFields(string sName,string sAge,string sGender) {
txtName.Text = sName;
txtAge.Text = sAge;
txtGender.Text = sGender;
}
}
}
My code in Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Practice_CS
{
public partial class Form2 : System.Windows.Forms.Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
lvlist.Items.Add("Juli");
lvlist.Items[0].SubItems.Add("20");
lvlist.Items[0].SubItems.Add("Male");
lvlist.Items.Add("Mark");
lvlist.Items[1].SubItems.Add("21");
lvlist.Items[1].SubItems.Add("Male");
lvlist.Items.Add("Shiela");
lvlist.Items[2].SubItems.Add("18");
lvlist.Items[2].SubItems.Add("Female");
}
private void btnSelect_Click(object sender, EventArgs e)
{
if (lvlist.Items.Count < 1) { return; }
Form1 f1 = new Form1();
f1.setFields(lvlist.FocusedItem.Text, lvlist.FocusedItem.SubItems[1].Text, lvlist.FocusedItem.SubItems[2].Text);
this.Close();
}
private void lvlist_DoubleClick(object sender, EventArgs e)
{
btnSelect_Click(btnSelect, e);
}
}
}