Question

i'm trying to create a program that works with USB devices. The problem i'm having is I have a listbox that contains each connected USB device in the computer. When you select a device, I want to open up a new form, Device_Options, which is located in its own partial class. Now when I try to get the name of the device, selectedDevice, from Devices to Device_Options, the value resets, and I get an empty string. My code for the two classes is as follows:

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

And then my other class, Device_Options:

 public partial class Device_Options : Form
    {

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

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

I've looked around all through the web, and i've found similar issues, but nothing seems to be working for me. If someone could point me in the right direction to get this working, Any help will be greatly appreciated. Thanks :)

Was it helpful?

Solution

It seems like everything works as I would expect, are you sure you understand the concept of partial classes right? As soon as you do new Device_Options().Show(), Device_Options will create a new and different instance of Devices, which will of course have selectedDevice set to the null-string!

E.g. pass your Devices instance to Device_Options's constructor:

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

and then call

new Device_Options(this).Show();  

OTHER TIPS

Its not because of partial classes. You have to pass the selected device to the Device_Options class through constructor or a property.

public partial class Device_Options : Form
    {

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

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

On Devices

Call as follows,

new Device_Options(selectedDevice).Show();  

The problem is you are actually not passing any value. The current instance of Devices form is not used in Device_Options form - you're creating new one (Devices devices = new Devices(); in Device_Options class).

Then you are not passing the selected value to options form. Pass in the Devices as parent and select the value:

private readonly Devices _parent;

public Device_Options(Devices parent)
{
  InitializeComponent();   
  _parent = parent;
  deviceLabel.Text = _parent.selectedDevice;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top