【问题标题】:Textbox inaccessible due to its protection level [duplicate]文本框由于其保护级别而无法访问[重复]
【发布时间】:2020-05-14 00:22:51
【问题描述】:

课堂作业要求我们创建一个简单的能源账单。第一次生成一个 ID 号,我想将其复制到第二个表格,但是我不断收到以下信息: 错误 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);
        }
    }
}

这是到目前为止 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 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");

        }
    }
}

【问题讨论】:

标签: c# winforms visual-studio-2019


【解决方案1】:

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

【讨论】:

  • 改变了访问,错误也改变了 Error CS1955 Non-invocable member 'TextBox.Text' cannot be used like a method
  • idgen.Text 不是 idgen.Text()
猜你喜欢
  • 2021-10-22
  • 1970-01-01
  • 2015-09-26
  • 2011-09-01
  • 1970-01-01
  • 2011-04-02
  • 2011-02-07
相关资源
最近更新 更多