Note that there are some explanatory texts on larger screens.

plurals
  1. POWindows Form partial class resets value when declared from another form
    primarykey
    data
    text
    <p>i'm trying to create a program that works with USB devices. The problem i'm having is I have a <code>listbox</code> that contains each connected USB device in the computer. When you select a device, I want to open up a new form, <code>Device_Options</code>, which is located in its own partial class. Now when I try to get the name of the device, <code>selectedDevice</code>, from <code>Devices</code> to <code>Device_Options</code>, the value resets, and I get an empty string. My code for the two classes is as follows: </p> <pre><code>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"); } } } </code></pre> <p>And then my other class, <code>Device_Options</code>:</p> <pre><code> public partial class Device_Options : Form { Devices devices = new Devices(); public string deviceSettings; public Device_Options() { InitializeComponent(); deviceLabel.Text = devices.selectedDevice; } </code></pre> <p>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 :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload