C#将文本传回主表单

时间:2018-01-14 08:14:43

标签: c# .net winforms

我正在尝试打开一个表单获取用户输入并将该输入传递回主表单以添加到List但我无法解决这个问题任何有用的建议都会感激我的意思做错了! 我得到的错误 System.NullReferenceException:'对象引用未设置为对象的实例。

//Character Class
public class Character {
    private string _Name;

    public string Name {
        get { return _Name; }
        set { _Name = value; }
    }

    public override string ToString() {
        return _Name;
    }
}

//Main Form1
        public List<Character> charList = new List<Character>();
        public EventHandler CharAdded; 

        public void GetData(string name) {
         Character c = new Character();
         c.Name = name;
         charList.Add(c);
       }

        private void btnCreateChar_Click(object sender, EventArgs e) {
        UserInputForm uif = new UserInputForm();

        //Subscribe to events
        CharAdded += uif.btnAdd_Click;

        //Call Event
        if (CharAdded != null) {
            CharAdded(this, new EventArgs());
        }

        //Show Form
        uif.Show();
    }

//UserInputForm
    public void btnAdd_Click(object sender, EventArgs e) {
        Form1 mainForm = sender as Form1;
        mainForm.GetData(txtName.Text); //Error on line
    }

3 个答案:

答案 0 :(得分:1)

在你的btnAdd_Click处理程序中,'sender'的类型为Form1。它是对btnAdd的引用,它是Button类型。这一行

Form1 mainForm = sender as Form1;

将mainForm设置为null,因此您的异常

但主要问题是你的平均处理有点倒退。 CharAdded甚至应存在于UserInputForm中,并从UserInputForm上的btnAdd_Click触发。

然后,您在mainform中的代码变为:

private void btnCreateChar_Click(object sender, EventArgs e) {
        UserInputForm uif = new UserInputForm();

        //Subscribe to events
        uif.CharAdded += Uif_CharAdded;

        //Show Form
        uif.Show();
}

// still in Form1
private void Uif_CharAdded(object sender, EventArgs e) {
  UserInputForm uif = (UserInputForm)sender;
  mainForm.GetData(uif.txtName.Text)
}

这段代码仍然不是很好,因为它意味着UserInputForm textName必须公开,但希望它能告诉你编写事件代码的方法。

答案 1 :(得分:0)

您需要为User Input表单的Add按钮设置一个事件处理程序,并且您必须在初始化其自己的表单而不是主表单时添加对此处理程序的订阅。您还可以从子表单访问charList。 然后在“添加”按钮上单击子窗体,可以将新字符添加到charList。

cols = ['SYMBOL', 'EXPIRY_DT', 'TIMESTAMP1']

futidx = futidx.drop_duplicates(cols)[cols + ['CLOSE1']]
optidx = pd.merge(optidx, futidx, on=cols, how='left', suffixes=('','_fut'))
optidx['CLOSE1_fut'] = optidx['CLOSE1_fut'].fillna(0)
print (optidx)

      SYMBOL  EXPIRY_DT  STRIKE_PR OPTION_TYP   OPEN1    HIGH     LOW  CLOSE1  \
0  BANKNIFTY 2018-01-25      25400         CE  388.35  441.20  355.25  418.50   
1  BANKNIFTY 2018-01-25      25500         CE  318.00  373.30  293.80  353.85   
2  BANKNIFTY 2018-01-25      25600         CE  263.95  319.20  245.75  296.65   
3  BANKNIFTY 2018-01-25      25400         PE  224.50  238.00  180.55  190.35   
4  BANKNIFTY 2018-01-25      25500         PE  272.10  279.95  212.25  227.60   
5  BANKNIFTY 2018-01-25      25600         PE  317.70  327.15  252.65  265.95   

   SETTLE_PR  CONTRACTS  VAL_INLAKH  OPEN_INT  CHG_IN_OI TIMESTAMP1  \
0     418.50       1065    10988.48     51880      -7280 2018-01-05   
1     353.85       9110    94123.35    363760       5320 2018-01-05   
2     296.65       1945    20133.22     55960       3080 2018-01-05   
3     190.35       1738    17805.51     26800       2120 2018-01-05   
4     227.60      10552   108693.13    424360      28840 2018-01-05   
5     265.95       1232    12761.12     33200       8560 2018-01-05   

   CLOSE1_fut  
0     25636.5  
1     25636.5  
2     25636.5  
3     25636.5  
4     25636.5  
5     25636.5  

答案 2 :(得分:0)

您的代码会产生一些影响:

1- GetData不明确应该做什么,在UserInputFormtxtName的文字中获取控制名称,似乎您希望将文字传递给{{1} }

2-如果第一个是真的那么你不应该复杂你的代码:

Form1
public partial class UserInputForm : Form { public new Form1 Parent { get; set; }//If you want to replace Parent prop use new keyword otherwise rename Parent prop public UserInputForm(Form1 parent) { Parent = parent; InitializeComponent(); } public void btnAdd_Click(object sender, EventArgs e) { Parent.SetData(txtName.Text); //Error on line } } 中的

Form1

4-但是如果要将控件名称添加到public partial class Form1 : Form { public List<Character> charList = new List<Character>(); public void SetData(string name) { Character c = new Character(); c.Name = name; charList.Add(c); } private void button1_Click(object sender, EventArgs e) { UserInputForm uif = new UserInputForm(this); uif.Show(); } } ,然后获取它们的值 做一些事情,在charList中定义一个道具,就像孩子那样:

Form1中:

From

和UserInputForm:

public partial class Form1 : Form
{
    public List<Character> charList = new List<Character>();
    public UserInputForm Child { get; set; }
    public void SetData(string name)
    {
        Character c = new Character();
        c.Name = name;
        charList.Add(c);
    }
    public Form1()
    {
        InitializeComponent();
    } 
    private void button1_Click_1(object sender, EventArgs e)
    {
        UserInputForm uif = new UserInputForm(this);
        uif.Show();
    }  
}

如果您想要存储在public partial class UserInputForm : Form { public new Form1 Parent { get; set; }//If you want to replace Parent prop use new keyword otherwise rename Parent prop public UserInputForm(Form1 parent) { Parent = parent; parent.Child = this; InitializeComponent(); } public void btnAdd_Click(object sender, EventArgs e) { Parent.SetData(txtName.Name); //Error on line } } 中的控件数据,只需按一下charList中的按钮:

Form1