Click here to Skip to main content
15,887,966 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
GOAL: I have a GUI and a combo box with four values. I am attempting to get the selected amount and have it print to a file with an associated colour.

For example: if "ABC" is selected, then in a file, "Value and Assigned Color ABC red" is written to the line.

I am having this issue because I do not know how to retrieve the value from that combo box to use in my GetHeader class.

EXTRA: I am looking for one of two solutions,

(1) I go through and do the string compare as seen in "retrieveValueFromBox," or

(2) somehow get the index of the selected item and use that index to write the line I need."

My code:

MainWindow.xaml

C++
<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>


MainWindow.xaml.cs

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


OutputFile.cs

C++
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.
    

      }


    }
  }
}


What I have tried:

I am having this issue because I do not know how to retrieve the value from that combo box to use in my GetHeader class.
Posted
Updated 21-Nov-20 20:44pm
v5

Passing Parameters - C# Programming Guide | Microsoft Docs[^]

For example:
C#
class OutputFile
{
    public void ShowComboBoxValue(string selectedValue)
    {
        Console.WriteLine("Selected value is: {0}", selectedValue);
    }
}
C#
private void mybox_SelectedItem(Object sender, Event Args e)
{
    ComboBox list = (ComboBox)sender;
    string selectedText = list.Text;
    OutputFile output = new OutputFile();
    output.ShowComboBoxValue(selectedText);
}

NB: For WPF, you'll probably want to use the MVVM pattern instead of writing code in the view's event handlers:
Patterns - WPF Apps With The Model-View-ViewModel Design Pattern | Microsoft Docs[^]

However, this might be a bit advanced for you if you're still new to the language. :)
 
Share this answer
 
Comments
Member 14995619 19-Nov-20 15:42pm    
So there isn't really a simple way to add a line like

string valueFromBox = SELETEDBOXVALUE;

and add that into the "public bool Write(...)" function and then pass it into GetHeader() (both are in OutPut.cs)?
Richard Deeming 20-Nov-20 3:25am    
Your updated code won't compile. You can't have methods directly within a namespace; they need to be within a class.

To access the selected value from the combobox, you either need to pass the combobox itself to the method, or pass the selected value to the method. A method cannot access things which it doesn't know about, either as parameters to the method or as fields or properties on the containing class.
Member 14995619 20-Nov-20 13:11pm    
Thank you but got it today, almost but it's printing out

"Systyem.Windows.Controls.ComboBoxItem:" with my selection.

Issue was

The CoboBox was not tied into an event. Ended up doubleclicking the ComoBox on the XAML design page, which created an event tied to the button.

Then there was a background worker, working with other information from the GUI, and I threw the variable in there, and kept passing the variable though until it got into the OutputFile.cs, and I am able to grab it from there.




Then in
MainWindow.xaml.cs the event just looks like
Hide   Copy Code
namespace myProgram{  public partial class MainWindow : Window  {    string comboBoxValue;    private void BackgroundWorker_DoWork(Object sender, DoWorkEventArgs e)    {      //Function to use GUI data was in here, just passed it in.    }    private void myComboBox_SelectionCHanged(object sender, SelectionChangedEventArgs e)    {         comboBoxValue = myComboBoxName.SelectedItem.ToString();    }  }}
Thank you, but got it today.

The issue was mainly two things.

(1) The CoboBox was not tied into an event. Ended up doubleclicking the ComoBox on the XAML design page, which created an event tied to the box, at the very bottom of MainWindow.xaml.cs.

(2) Then there was a background worker, working with other information from the GUI, and I threw the variable in there, and kept passing the variable though until it got into the OutputFile.cs, and I am able to grab it from there.

MainWindow.xaml.cs the event just looks like

C++
namespace myProgram
{ 
  public partial class MainWindow : Window
  {
    string comboBoxValue;
    
    //Here is where File I/O pathing information was at, other details from the GUI
    string destinationDir;

    // Stuff

    private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
      var bgw = sender as BackgroundWorker;
      e.Result = _processor.Load( ...args...  , comboBoxValue); //Once here was a matter of tracing though the program, which ended up at the Output file.
    }

    //More Stuff

    Private void myComboBox_SelectionCHanged(object sender, SelectionChangedEventArgs e)
    { 
      comboBoxValue = myComboBoxName.SelectedIndex.ToString();
    }
  }
}



Then there was a background worker, working with other information from the GUI, and I threw the variable in there, and kept passing the variable though until it got into the OutputFile.cs, and I am able to grab it from there.

Since I was only using the index (0,1,2,3, or 4). It was just a matter of using a String to Int, and applying a Case/Switch to that int, and returning the VALUE and assigned color (i.e. "abc red").
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900