Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# 文本框由于其保护级别而无法访问_C#_Winforms_Visual Studio 2019 - Fatal编程技术网

C# 文本框由于其保护级别而无法访问

C# 文本框由于其保护级别而无法访问,c#,winforms,visual-studio-2019,C#,Winforms,Visual Studio 2019,课堂作业要求我们创建一个简单的能源账单。1号生成了一个ID号,我想从该ID号复制到第2张表格,但我一直得到以下信息: 错误CS0122“Form1.idgen”由于其保护级别而不可访问 因为我将引用第一个表单中的很多值,所以我只想了解问题是什么,以及如何克服它以备将来使用 这是Form1的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using

课堂作业要求我们创建一个简单的能源账单。1号生成了一个ID号,我想从该ID号复制到第2张表格,但我一直得到以下信息: 错误CS0122“Form1.idgen”由于其保护级别而不可访问
因为我将引用第一个表单中的很多值,所以我只想了解问题是什么,以及如何克服它以备将来使用 这是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 Bill_Generator
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            timer1.Enabled = true;

            Random r = new Random();
            var x = r.Next(0, 1000000);
            string v = x.ToString("000");
            idgen.Text = v;

        }

        public void Timer1_Tick(object sender, EventArgs e)
        {
            date.Text = DateTime.Now.ToString("dddd , MMM dd yyyy  hh:mm:ss");
        }


        public void Gen_button_Click(object sender, EventArgs e)
        {
            Form2 openform = new Form2();
            openform.Show();
        }

        public void Calc_Click(object sender, EventArgs e)
        {
            double preamt = Convert.ToDouble(prebox.Text);
            double curramt = Convert.ToDouble(currbox.Text);
            double billamt = curramt - preamt;
            billbox.Text = Convert.ToString(billamt);
        }
    }
}
这是迄今为止表格2的代码:

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 Bill_Generator
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["Form1"];
            iddisp.Text = ((Form1)f).idgen.Text();

        }

        private void Form2_Load(object sender, EventArgs e)
        {
            date.Text = DateTime.Now.ToString("dddd , MMM dd yyyy  hh:mm:ss");

        }
    }
}

默认情况下,表单控件是私有的。在designer UI中选择控件,并在其属性中将访问级别更改为public


更改了访问权限,错误已更改错误CS1955不可开票成员“TextBox.Text”不能像方法idgen.Text而不是idgen.Text()一样使用。您可以更改访问权限。但是,请参见以下问答:。改变可访问性实际上是最后的手段,永远不需要。