我正在尝试创建一个适用于 USB 设备的程序。我遇到的问题是我有一个listbox
包含计算机中每个连接的 USB 设备的设备。当您选择一个设备时,我想打开一个新表单,Device_Options
它位于它自己的部分类中。现在,当我尝试获取设备的名称时selectedDevice
,从Devices
to Device_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_Options
:
public partial class Device_Options : Form
{
Devices devices = new Devices();
public string deviceSettings;
public Device_Options()
{
InitializeComponent();
deviceLabel.Text = devices.selectedDevice;
}
我浏览了整个网络,发现了类似的问题,但似乎没有什么对我有用。如果有人能指出我正确的方向来完成这项工作,任何帮助将不胜感激。谢谢 :)