我在将所有需要的数据从一种表单传递到另一种表单时遇到问题。下面你可以看到我想传递的数据,但是会发生什么,它只传递(_12nc,BID,设备,orderNr)而不是List<>步骤。
Steps stepsForm = new Steps(ref steps, _12nc, BID, device, orderNr);
但如果我尝试仅传递List<>步骤,则传递步骤:
Steps stepsForm = new Steps(ref steps)
如何传递List和单个值?
这是我要传递数据的代码:
private void btn_start_Click(object sender, EventArgs e)
{
List<Step> steps = new List<Step>();
int device;
string _12nc;
int BID;
int orderNr;
device = Convert.ToInt32(txt_device.Text);
_12nc = txt_12nc.Text;
BID = Convert.ToInt32(txt_BID.Text);
orderNr = Convert.ToInt32(txt_OrderNr.Text);
// Get list of steps (ID and Description)
steps = myDBHelper.GetSteps(_12nc,device, BID);
// Open new form and pass steps to it
Steps stepsForm = new Steps(ref steps, _12nc, BID, device, orderNr); //, _12nc, BID, device, orderNr
stepsForm.Show();
this.Hide();
}
这就是我想把它传递给的地方:
public partial class Steps : Form
{
private List<Step> steps = new List<Step>();
private List<Parameter> param = new List<Parameter>();
private String _12nc;
private int BID;
private int device;
private int orderNr;
public Steps(ref List<Step> steps, string _12nc, int BID, int device, int orderNr)
{
InitializeComponent();
this.steps = steps;
this._12nc = _12nc;
this.BID = BID;
this.device = device;
this.orderNr = orderNr;
RefreshLabels();
}
答案 0 :(得分:0)
根据您的要求修改下面的代码,我已经检查它是否正常工作。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<Step> steps = new List<Step>();
Step objStep = new Step();
int device;
device = 32;
objStep._12nc = "dsfdsF";
objStep.BID = 3;
objStep.orderNr = 12;
// Get list of steps (ID and Description)
//steps = myDBHelper.GetSteps(_12nc, device, BID);
steps.Add(objStep);
// Open new form and pass steps to it
Form2 stepsForm = new Form2(steps, objStep, device); //, _12nc, BID, device, orderNr
stepsForm.Show();
this.Hide();
}
}
第二种形式
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private List<Step> steps = new List<Step>();
private List<Parameter> param = new List<Parameter>();
private String _12nc;
private int BID;
private int device;
private int orderNr;
public Form2(List<Step> steps, Step objStep, int device)
{
InitializeComponent();
this.steps = steps;
this._12nc = objStep._12nc;
this.BID = objStep.BID;
this.device = device;
this.orderNr = objStep.orderNr;
//RefreshLabels();
}
}
public class Step
{
public string _12nc { get; set; }
public int BID { get; set; }
public int orderNr { get; set; }
}
public class Parameter
{
}