I have two Windows Forms. I have read data from the database and load data into Listbox1
on Form1(FrmSelection)
. Now what i want is, when a user select a value from Listbox1
on Form1
and click process button, the selected value must be loaded on Form2(FrmProcessOrder) Listbox2
. I have tried different examples but am not get it to work. here are the steps below
1.Run Application
2.FrmSelection Opens with empty listbox,then click process button which opens FrmSelection
3.FrmSelections opens up,this form contains one listbox and data the i have read from the database.
4.Select one item from the listbox
5.Click Addforms button after selecting the item from listbox on FrmSelections.
Problem - Am getting error on FrmProcessorder form when i click Addforms button after selecting the item(object not set to an instance of an object)
here is my code below..
Form1(FrmSelection)
public partial class FrmProcessOrder : Form
{
public FrmProcessOrder()
{
InitializeComponent();
}
private void btnProcess_Click(object sender, EventArgs e)
{
Program._FrmSelection = new FrmSelection();
Program._FrmSelection.Show();
}
public void AddList(ListBox _Listing)
{
ListBoxForms.Items.AddRange(_Listing.Items);
}
}
Form 2(FrmProcessOrder )
FrmProcessOrder _FrmProcessOrder;
public FrmSelection()
{
InitializeComponent();
LoadData();
}
//Method which reads data from the db and laod data on listview
public void LoadData()
{
_connection = "Data Source=MILESTONE-PC;Initial Catalog=Jack;Integrated Security=True";
_sql = "Select * from tblJackon";
_conn = new SqlConnection(_connection);
_conn.Open();
_comm = new SqlCommand(_sql, _conn);
_adapt.SelectCommand = _comm;
_adapt.Fill(_dataset, _sql);
DataTable _tbl = _dataset.Tables[0];
_dtrow = null;
foreach (DataRow _dataVariable in _tbl.Rows)
{
_dtrow = _dataVariable;
ListBoxSelection.Items.Add((_dtrow["FormNames"]));
}
}
private void btnAddForms_Click(object sender, EventArgs e)
{
_FrmProcessOrder.AddList(ListBoxSelection); // It throws error on this line after select from listbox so that the item must to FrmProcessOrder listbox
_FrmProcessOrder = new FrmProcessOrder();
}
Edit (after the question is edited):
Check this:
FrmProcessOrder _FrmProcessOrder; //no object declared
private void btnAddForms_Click(object sender, EventArgs e)
{
_FrmProcessOrder.AddList(ListBoxSelection); // This is null exception
_FrmProcessOrder = new FrmProcessOrder(); //it should not be put here
}
Your _FrmProcessOrder
is used before it is initialized properly. Do this instead:
FrmProcessOrder _FrmProcessOrder = new FrmProcessOrder(); //Now it is declared, only once
private void btnAddForms_Click(object sender, EventArgs e)
{
_FrmProcessOrder.AddList(ListBoxSelection); // Now it should be ok
}
Original:
One of the simplest way would be to put your input as a field (or create a method call to do it) rather than as a constructor argument.
Change this:
public FrmProcessOrder(ListBox _List)
{
InitializeComponent();
ListBoxForms2.Items.AddRange(_List.Items);
}
Into this
public FrmProcessOrder()
{
InitializeComponent();
}
public void AddList(ListBox _List){
ListBoxForms2.Items.AddRange(_List.Items);
}
Then when you could call your Form
Method whenever you need
_FrmProcessOrder.AddList(listBox);
In this line
Application.Run(new FrmProcessOrder());
you're trying to instantiate a new FrmProcessOrder
, but you have one constructor like this:
public FrmProcessOrder(ListBox _List)
so you can't instantiate without a parameter ListBox
. If you want this you have to add to Form1
the empty constructor:
public FrmProcessOrder()
{
}
I believe you have wrongly copied the code blocks(swapped Form2 & Form1).
This line Application.Run(new FrmProcessOrder());
should be replaced by Application.Run(new FrmSelection());
which would load the data from the database.
The compilation error you are seeing is since you do not have an empty constructor. As per the current code, to instantiate 'FrmProcessOrder' you have to provide an instance of ListBox.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.