簡體   English   中英

檢索和使用在 ComboBox 中選擇的值

[英]Retrieving and Using a Value selected in a ComboBox

目標:我有一個 GUI 和一個包含四個值的組合框。 我正在嘗試獲取選定的數量並將其打印到具有關聯顏色的文件中。

例如:如果選擇了“ABC”,則在文件中,“值和分配的顏色 ABC 紅色”將寫入該行。

我遇到這個問題是因為我不知道如何從該組合框中檢索值以在我的 GetHeader 類中使用。

額外:我正在尋找兩種解決方案之一,

(1) 我完成並進行字符串比較,如“retrieveValueFromBox”中所示,或

(2) 以某種方式獲取所選項目的索引並使用該索引編寫我需要的行。” 獲得一些關於如何更好地使用或學習 C# 的體面建議也很棒。

我的代碼:

主窗口.xaml

<ComboBox x:Name="myBox" ...alignment information...>
  <ComboBoxItem Content="abc" ...alignment information...>
  <ComboBoxItem Content="def" ...alignment information...>
  <ComboBoxItem Content="ghi" ...alignment information...>
  <ComboBoxItem Content="jkl" ...alignment information...>
</ComboBox>

主窗口.xaml.cs

private void selectedItemFromBox(object sender, EventArgs e)
{
  ComboBox selectedValue = (ComboBox)sender;
  string selectedText = selectedValue.Text;
  OutputFile output = new OutputFile();
  output.retreiveValueFromBox(selectedText);
}

輸出文件.cs

namespace myApplication
{
  class OutputFile
  {
    // Stuff
  }


  //public bool Write( ...args...  ) makes the call to this function.  See below
  private void GetHeader(StreamWriter w, string year, string evt)
  {
    //How do I get the value from the combobox here so I can use it.
    // If it is better practice to grab the value in public bool Write() then see below
    //  Yes I understand that GetHEader's args will need to be changed

    w.WriteLine("Line 1");
    w.WriteLine("Line 2");
    w.WriteLine("Value and Assigned Color " + retreiveValueFromBox(comboBoxItem));
    w.WriteLine("Line N");

  }

  private string retreiveValueFromBox(string boxText)
  {
    string value_Color = "";

    //If I can just get the index of the string, I can just do a case/switch.  
    //And dump the String.Euals()

    
    if (String.Equals(boxText, "abc"))
    {
      value_Color = boxText + " red");
    }
    else if bString.Equals(boxText, "def")
    {
      value_Color = boxText + " blue");
    }
    else if String.Equals(boxText, "ghi")
    {
      value_Color = boxText + " green");
    }
    else if String.Equals(boxText, "jkl")
    {
      value_Color = boxText + " orange");
    }
    else 
    {
      value_Color = "UNKNOWN yellow");
    }

    return value_Color;
  }


  public bool Write( ...args...  )
  {

    //Stuff
    for (; idx < eventNum; )
    {

      // Or Get the ComboBox Value here

      GetHeader(sWrite, year(), evt); //And pass value in here.
    

    }


}

我得到了我需要的東西。

我這邊的問題。

組合框未正確綁定到事件。 修復方法是雙擊 XAML 設計窗口中的組合框,這會在 WindowWindow.xaml.cs 的最底部創建一個與該框相關聯的事件。

private void myComboBoxName_SelectionChanged(Object sender, SelectionChangedEventArgs e)
{
  comboxValue = myComboBoxName.SelectedIndex.ToString();
}

我注意到,我以前沒有注意到,還有另一個函數處理來自 GUI 的信息(文件 I/O 信息)。 因此,我將變量“comboboxValue”放入該函數中,並繼續傳遞它,直到它一直將我帶入 Outputfile.cs。

因為我只使用索引 (0, 1, 2, 3, 4)。 我只是使用 Case/Switch 打印出值和指定的顏色。

暫無
暫無

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

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