0

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);
        }
    }
}

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");

        }
    }
}
Whitti1
  • 55
  • 8
  • 1
    You can change accessibility. However, see these Q&A: https://stackoverflow.com/questions/8566/best-way-to-access-a-control-on-another-form-in-windows-forms, https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp. Changing the accessibility is really the last resort, and should never be needed. – Peter Duniho May 14 '20 at 00:49

1 Answers1

0

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

enter image description here

pm100
  • 48,078
  • 23
  • 82
  • 145