由于其保护级别,无法访问文本框 [英] Textbox inaccessible due to its protection level

查看:24
本文介绍了由于其保护级别,无法访问文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

课堂作业要求我们创建一个简单的能源账单.身份证号码是在第一张生成的,我想复制到第二张表格,但是我不断收到以下信息:错误 CS0122 'Form1.idgen' 由于其保护级别而无法访问
由于我将参考第一个表格中的许多值,我只想了解问题是什么以及如何克服它以备将来使用这是 Form1 的代码:

Class coursework asked us to create a simple energy bill. an ID number is generated on the 1st from that I want to copy to the 2nd form however I keep getting the following : Error CS0122 'Form1.idgen' is inaccessible due to its protection level
Since I'm going to referencing a lot of values from the 1st Form I just want to learn what the issue is and how to overcome it for future use This is the code for 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 的代码:

This is the code so far for 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");

        }
    }
}

推荐答案

默认表单控件是私有的.在设计器 UI 中选择控件并在其属性中将访问级别更改为公开.

by default form controls are private. In the designer UI choose the control and in its properties change the access level to public.

这篇关于由于其保护级别,无法访问文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆