1

I am self-studying C# and trying to build a windows forms app. I don't know what I'm doing wrong here, but I can't see any data when I click the 'List' button in Form1 to see the submitted data from textbox (Form1)

Details about the app:

  • 1 main class and 3 inherited classes
  • First form (Form1) has three radio buttons, three text boxes, three control buttons (save, clear, and view second form)
  • Second form (Form2) has a ListView that shows the user input from the Form1.

--- Form 1 ---

public partial class Form1 : Form
{
    int pos = 0;
    ElectronicDevice[] ElectronicDevices = new ElectronicDevice [10];
    
    public Form1()
    {
        InitializeComponent();
    }

    private void submit_Click(object sender, EventArgs e)
    {

        if (radioButton1.Checked)
        {
            ElectronicDevices[pos] = new ElectronicDevice(textBox1.Text, textBox2.Text, textBox3.Text);
        }

        if (radioButton2.Checked)
        {
            ElectronicDevices[pos] = new ElectronicDevice(textBox1.Text, textBox2.Text, textBox3.Text);
        }
        if (radioButton3.Checked)
        {
            ElectronicDevices[pos] = new ElectronicDevice(textBox1.Text, textBox2.Text, textBox3.Text);
        }
        pos++;


    }

    private void clear_Click(object sender, EventArgs e)
    {
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();

    }

    private void list_Click(object sender, EventArgs e)
    {
        
        Form2 ViewList = new Form2();
         

        for (int i = 0; i < pos; i++)
        {

            // System.NullReferenceException: 'Object reference not set to an instance of an object.' Local2 was null.

                 ViewList.Controls["txtList"].Text +=
                 ElectronicDevices[i].Type() + "\r\n" +
                 ElectronicDevices[i].Brand + " " +
                 ElectronicDevices[i].Model + " " +
                 ElectronicDevices[i].Year + " ";
        }
        ViewList.ShowDialog();
    }


}

--- Main class ---

class ElectronicDevice
{
    public string Brand;
    public string Model;
    public string Year;

    public ElectronicDevice() {
        Brand = "";
        Model = "";
        Year = "";

    }
    public ElectronicDevice(string iBrand, string iModel, string iYear) 
    {
        Brand = iBrand;
        Model = iModel;
        Year = iYear;
    }


    
    public virtual string Type() { return "Device"; }
}

--- One of the inherited classes ---

class SmartPhone : ElectronicDevice
{
    public SmartPhone() : base()
    {

    }

    public SmartPhone(string iBrand, string iModel, string iYear) : base(iBrand, iModel, iYear)
    {

    }

    public override string Type() { return "SmartPhone"; }
}   

Thanks in advance!

skforiss
  • 13
  • 5

0 Answers0