You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinForms中Form1文本框数据无法显示在Form2的ListView求助

C# WinForms 数据无法显示到ListView的问题解决

问题分析

  1. 继承类未正确实例化:提交按钮中,无论选中哪个RadioButton,都只创建了基类ElectronicDevice的实例,没有使用定义好的继承类(如SmartPhone),违背了多态设计的初衷。
  2. ListView操作错误:你尝试通过修改控件的Text属性来显示数据,但Form2里是ListView控件,不是TextBox,且直接访问子控件的方式容易引发空引用异常(控件名称错误或找不到时返回null)。
  3. 未校验用户输入状态:如果未选中任何RadioButton就点击提交,ElectronicDevices[pos]会保持null,后续循环遍历数组时会触发NullReferenceException。

分步解决方案

1. 修正提交按钮的实例化逻辑

根据选中的RadioButton创建对应的继承类实例(假设radioButton1对应SmartPhone,radioButton2、3对应另外两个已定义的继承类如Laptop、Tablet):

private void submit_Click(object sender, EventArgs e)
{
    // 校验是否选中设备类型
    if (!radioButton1.Checked && !radioButton2.Checked && !radioButton3.Checked)
    {
        MessageBox.Show("请选择设备类型");
        return;
    }

    if (radioButton1.Checked)
    {
        ElectronicDevices[pos] = new SmartPhone(textBox1.Text, textBox2.Text, textBox3.Text);
    }
    else if (radioButton2.Checked)
    {
        ElectronicDevices[pos] = new Laptop(textBox1.Text, textBox2.Text, textBox3.Text);
    }
    else if (radioButton3.Checked)
    {
        ElectronicDevices[pos] = new Tablet(textBox1.Text, textBox2.Text, textBox3.Text);
    }
    pos++;
}

2. 优化Form2的数据接收方式

先在Form2设计器中配置ListView:

  • 将View属性设为Details
  • 添加「设备类型」「品牌」「型号」「年份」四列

然后在Form2中封装填充数据的方法:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        // 若设计器未配置列,可通过代码添加
        listView1.Columns.Add("设备类型", 120);
        listView1.Columns.Add("品牌", 100);
        listView1.Columns.Add("型号", 150);
        listView1.Columns.Add("年份", 80);
        listView1.View = View.Details;
    }

    public void LoadDevices(ElectronicDevice[] devices, int count)
    {
        listView1.Items.Clear();
        for (int i = 0; i < count; i++)
        {
            var device = devices[i];
            if (device == null) continue;
            
            ListViewItem item = new ListViewItem(device.Type());
            item.SubItems.Add(device.Brand);
            item.SubItems.Add(device.Model);
            item.SubItems.Add(device.Year);
            listView1.Items.Add(item);
        }
    }
}

3. 修正Form1的List按钮逻辑

通过Form2的封装方法传递数据,避免直接操作控件:

private void list_Click(object sender, EventArgs e)
{
    Form2 viewList = new Form2();
    viewList.LoadDevices(ElectronicDevices, pos);
    viewList.ShowDialog();
}

额外优化建议

  • 替换固定长度数组:原数组最多存10条数据,建议改用List<ElectronicDevice>动态存储,避免索引越界:
    // Form1中替换数组定义
    List<ElectronicDevice> electronicDevices = new List<ElectronicDevice>();
    
    // 提交按钮添加数据
    electronicDevices.Add(new SmartPhone(textBox1.Text, textBox2.Text, textBox3.Text));
    
    // Form2修改方法参数
    public void LoadDevices(List<ElectronicDevice> devices)
    {
        listView1.Items.Clear();
        foreach (var device in devices)
        {
            if (device == null) continue;
            // 创建并添加ListViewItem
        }
    }
    
  • 确保控件名称一致:Form2中ListView的名称要和代码中使用的一致,避免因拼写错误引发空引用。

内容的提问来源于stack exchange,提问作者skforiss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 08:35:23