簡體   English   中英

Windows窗體部分類從其他窗體聲明時會重置值

[英]Windows Form partial class resets value when declared from another form

我正在嘗試創建一個適用於USB設備的程序。 我遇到的問題是我有一個listbox ,其中包含計算機中每個已連接的USB設備。 選擇設備時,我想打開一個新表單Device_Options ,該表單位於其自己的局部類中。 現在,當我嘗試獲取設備名稱selectedDevice ,從DevicesDevice_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;
        }

我瀏覽了整個網絡,發現了類似的問題,但是似乎沒有任何工作對我有用。 如果有人可以向我指出正確的方向以使其正常工作,我們將不勝感激。 謝謝 :)

它不是因為局部類。 您必須通過構造函數或屬性將所選設備傳遞給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();  

問題是您實際上沒有傳遞任何值。 Device_Options表單中未使用Devices表單的當前實例-您正在創建一個Devices devices = new Devices(); Device_Options Devices devices = new Devices(); Device_Options類中的Devices devices = new Devices(); )。

這樣就不會將所選值傳遞給選項表單。 以父Devices身份傳遞Devices並選擇值:

private readonly Devices _parent;

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

似乎一切都按我預期的那樣工作,您確定您了解部分類的概念嗎? 一旦執行new Device_Options().Show()Device_Options就會創建一個新的和不同Devices實例,這些實例當然會將selectedDevice設置為null字符串!

例如,將您的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();  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM