【问题标题】:Windows Form partial class resets value when declared from another formWindows 窗体部分类在从另一个窗体声明时重置值
【发布时间】:2013-09-12 09:50:47
【问题描述】:

我正在尝试创建一个适用于 USB 设备的程序。我遇到的问题是我有一个listbox,其中包含计算机中每个连接的 USB 设备。当你选择一个设备时,我想打开一个新表单,Device_Options,它位于它自己的部分类中。现在,当我尝试从DevicesDevice_Options 获取设备名称selectedDevice 时,值会重置,并且我得到一个空字符串。我对这两个类的代码如下:

Devices
public partial class Devices : Form
{
    public string selectedDevice;
    public Devices()
    {
        InitializeComponent();
    }

    private void Devices_Load(object sender, EventArgs e)
    {

        DriveInfo[] loadedDrives = DriveInfo.GetDrives();

        foreach (DriveInfo ld in loadedDrives)
        {
            if (ld.IsReady == true)
            {
                deviceListBox.Items.Add(ld.Name + "         "+ ld.VolumeLabel + "         "+ ld.DriveFormat);
            }
        }
    }

    private void refreshButton_Click(object sender, EventArgs e)
    {
        this.Close();
        new Devices().Show();
    }

    private void backButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    public void deviceSelectButton_Click_1(object sender, EventArgs e)
    {
        string itemSelected = "0";

        if (deviceListBox.SelectedIndex != -1)
        {

            itemSelected = deviceListBox.SelectedItem.ToString();

            deviceListBox.SelectedItem = deviceListBox.FindString(itemSelected);
            selectedDevice = deviceListBox.GetItemText(deviceListBox.SelectedItem).ToString();



//selectedDevice value should be passed to Device_Options

            new Device_Options().Show();  
        }              

       else
        {
            MessageBox.Show("Please Select a Device");
        }
    }
}

然后是我的另一堂课,Device_Options

 public partial class Device_Options : Form
    {

        Devices devices = new Devices();
        public string deviceSettings;

        public Device_Options()
        {
            InitializeComponent();      
            deviceLabel.Text = devices.selectedDevice;
        }

我浏览了整个网络,发现了类似的问题,但似乎没有什么对我有用。如果有人能指出我正确的方向来完成这项工作,任何帮助将不胜感激。谢谢:)

【问题讨论】:

  • //selectedDevice value should be passed to Device_Options...为什么?
  • 我只是想证明我正在尝试做的事情:P

标签: c# string partial-classes


【解决方案1】:

似乎一切都如我所料,你确定你理解部分类的概念吗? 只要您执行new Device_Options().Show()Device_Options 就会创建Devices新的和不同的 实例,它当然会将selectedDevice 设置为空字符串!

例如将您的 Devices 实例传递给 Device_Options 的构造函数:

public partial class Device_Options : Form
{

    readonly Devices devices;
    public string deviceSettings;

    public Device_Options(Devices host)
    {
        InitializeComponent();      
        this.devices = host;
        deviceLabel.Text = devices.selectedDevice;
    }

然后调用

new Device_Options(this).Show();  

【讨论】:

    【解决方案2】:

    这不是因为部分类。您必须通过构造函数或属性将所选设备传递给Device_Options 类。

    public partial class Device_Options : Form
        {
    
            public Device_Options()
            {
                InitializeComponent();      
                deviceLabel.Text = devices.selectedDevice;
            }
    
     public Device_Options(Device selectedDevice)
            {
                InitializeComponent();      
                deviceLabel.Text = selectedDevice;
            }
    }
    

    在设备上

    如下调用,

    new Device_Options(selectedDevice).Show();  
    

    【讨论】:

      【解决方案3】:

      问题是您实际上没有传递任何值。 Devices 表单的当前实例未在 Device_Options 表单中使用 - 您正在创建新实例(Device_Options 类中的Devices devices = new Devices();)。

      那么您没有将所选值传递给选项表单。传入Devices 作为父级并选择值:

      private readonly Devices _parent;
      
      public Device_Options(Devices parent)
      {
        InitializeComponent();   
        _parent = parent;
        deviceLabel.Text = _parent.selectedDevice;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多