Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows窗体分部类在从其他窗体声明时重置值_C#_String_Partial Classes - Fatal编程技术网

C# Windows窗体分部类在从其他窗体声明时重置值

C# Windows窗体分部类在从其他窗体声明时重置值,c#,string,partial-classes,C#,String,Partial Classes,我正在尝试创建一个与USB设备一起工作的程序。我遇到的问题是我有一个列表框,其中包含计算机中每个连接的USB设备。当您选择一个设备时,我想打开一个新表单,device\u Options,它位于它自己的分部类中。现在,当我尝试获取设备名称时,selectedDevice,从Devices到device\u Options,值重置,我得到一个空字符串。这两个类的代码如下所示: Devices public partial class Devices : Form { public stri

我正在尝试创建一个与USB设备一起工作的程序。我遇到的问题是我有一个
列表框
,其中包含计算机中每个连接的USB设备。当您选择一个设备时,我想打开一个新表单,
device\u Options
,它位于它自己的分部类中。现在,当我尝试获取设备名称时,
selectedDevice
,从
Devices
device\u Options
,值重置,我得到一个空字符串。这两个类的代码如下所示:

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\u Options

 public partial class Device_Options : Form
    {

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

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

我在网上到处找了找,发现了类似的问题,但似乎什么都不适合我。如果有人能为我指出正确的方向,让这项工作,任何帮助将不胜感激。谢谢:)

这不是因为部分课程。您必须通过构造函数或属性将所选设备传递给
设备\u 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();  

问题是您实际上没有传递任何值。
设备
表单的当前实例未在
设备_选项
表单中使用-您正在创建一个新实例(
设备设备=新设备();
设备_选项
类中)

然后,您不会将所选值传递给选项窗体。将
设备作为父项传入并选择值:

private readonly Devices _parent;

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

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

例如,将您的
设备
实例传递给
设备的构造函数

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

//所选设备值应传递给设备选项
。。。为什么?我只是想证明我所做的是正确的:P