Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CS0120,正在尝试通过单选按钮将数据发送到列表框_C#_Winforms - Fatal编程技术网

C# CS0120,正在尝试通过单选按钮将数据发送到列表框

C# CS0120,正在尝试通过单选按钮将数据发送到列表框,c#,winforms,C#,Winforms,我一直有一些麻烦,试图做我的代码,我试图建立一个程序,为访客注册与自定义对话框,我做了,它有4个单选按钮,我不能做任何事情来修复它现在。 下面是一些表格参考的截图。 我的Form1代码是: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text

我一直有一些麻烦,试图做我的代码,我试图建立一个程序,为访客注册与自定义对话框,我做了,它有4个单选按钮,我不能做任何事情来修复它现在。 下面是一些表格参考的截图。

我的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 _2021415_20T1_L5IS_PP1_Assessment_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }



        private void button2_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("Please enter your name");
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("Please enter your surname");

            }
            if (textBox3.Text == "")
            {
                MessageBox.Show("Please enter your mobile number");

            }
            if (textBox4.Text == "")
            {
                MessageBox.Show("Please enter your email");

            }
            if (numericUpDown1.Text == "0")
            {
                MessageBox.Show("Please enter the time");

            }
            if (numericUpDown2.Text == "0")
            {
                MessageBox.Show("Please enter the time");

            }




            else
                listBox1.Items.Add(comboBox1.SelectedItem + " - John Doe" + " at " + numericUpDown1.Text + ":" + numericUpDown2.Text);

        }
那么我的Form2代码是:

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 _2021415_20T1_L5IS_PP1_Assessment_2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }


        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                Form1.listBox1.Items.Add("");

            }

        }
    }
}

表单是类,它们需要相互调用方法或访问彼此的属性以在它们之间传递数据。他们还需要访问彼此的对象

在您的代码中,Form1创建了Form2的对象并将其作为对话框打开,但Form2不知道打开对话框的Form1的当前实例。所以你需要先解决这个问题

您可以通过在Form2中使用构造函数来实现这一点,该构造函数将Form1的对象作为参数

public partial class Form2 : Form
{
    private readonly Form1 form1;
    public Form2(Form1 form1)
    {
        InitializeComponent();
        this.form1 = form1;
    }
    //
    // Rest of the code of Form2...
    //
}
这里Form2知道Form1的实例,它打开了Form2 as对话框。现在点击OK按钮Form2应该能够将单选按钮的数据传递给Form2

为此,Form1可以有一个方法,将数据作为字符串接受,并将其添加到列表框中

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    public void AddItem(string item)
    {
        listBox1.Items.Add(item);
    }
    //
    // Rest of the code of Form1...
    //
}
现在,Form2可以在单击OK按钮时调用此方法

public partial class Form2 : Form
{
    private readonly Form1 form1;
    public Form2(Form1 form1)
    {
        InitializeComponent();
        this.form1 = form1;
    }
    //
    // Rest of the code of Form2...
    //
    private void button2_Click(object sender, EventArgs e)
    {
        if (radioButton1.Checked)
        {
            this.form1.AddItem("");
        }
    }
}

你忘了解释你面临的问题…哦,对不起,我面临的问题是我无法在表单之间进行通信,我正在尝试使用表单2中的单选按钮添加到我的列表框中,以用于会议的开始表单与任何其他类一样,因此你可以添加任何需要的方法来传递数据。让表单摆弄自己的控件,只传递数据。通过使用选项卡控件,您可以避免整个问题