5 Replies - 1114 Views - Last Post: 15 September 2011 - 08:17 AM Rate Topic: -----

#1 fanle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-September 11

Unable to retrieve data from a combobox.

Posted 15 September 2011 - 07:50 AM

Hey guys,

I am trying to display the result from an if statment which is based upon the option selected within a windows form combo box. I am having issues when the if statment is placed within its own class seperate to the form and is always just returning the else value.
I have tried loading the result into a public static variable and calling that across however I still get the same results.
I have also set the combobox to public. My code is as below, thanks for any help you could give me.

public partial class frmSWChange : Form
    {
        
        public frmSWChange()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            
            string shiftChangeValue;
            label1.Text = mtxtScrapDate.Text;
            xRayData xRayData1 = new xRayData();
            shiftChangeValue = xRayData1.shiftChange();
            label2.Text = shiftChangeValue;
        }

    }



class xRayData
    {
        #region Methods


        public string shiftChange()
        {
            frmSWChange frmSWChange1 = new frmSWChange();

            string shiftLetter;

            if (frmSWChange1.cbShift.SelectedText == "Day")
            {
                shiftLetter = "D";
            }
            else if (frmSWChange1.cbShift.SelectedText == "Night")
            {
                shiftLetter = "N";
            }
            else if (frmSWChange1.cbShift.SelectedText == "Morning")
            {
                shiftLetter = "M";
            }
            else
            {
                shiftLetter = "ERROR";
            }


            return shiftLetter;
        }

        #endregion

    }


This post has been edited by fanle: 15 September 2011 - 07:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Unable to retrieve data from a combobox.

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9492
  • View blog
  • Posts: 35,826
  • Joined: 12-June 08

Re: Unable to retrieve data from a combobox.

Posted 15 September 2011 - 07:55 AM

Try instead of "SelectedText " just use "text".
Was This Post Helpful? 0
  • +
  • -

#3 fanle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-September 11

Re: Unable to retrieve data from a combobox.

Posted 15 September 2011 - 07:57 AM

Produces the same results unfortunatley.
Was This Post Helpful? 0
  • +
  • -

#4 aetion  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 21-May 11

Re: Unable to retrieve data from a combobox.

Posted 15 September 2011 - 08:02 AM

View Postfanle, on 15 September 2011 - 08:20 PM, said:

        public string shiftChange()
        {
            frmSWChange frmSWChange1 = new frmSWChange();
        }



Hey fanle,

What your doing in your shiftChange method, is that you are creating a whole new instance of your form (frmSWChange).
You need to pass the reference of the form, from where you call the shiftChange() method, to the method

public partial class frmSWChange : Form
    {
        
        public frmSWChange()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            
            string shiftChangeValue;
            label1.Text = mtxtScrapDate.Text;
            derpderp1 = cbShift.SelectedText;
            xRayData xRayData1 = new xRayData();
            
            //Pass this forms reference to the method, so it uses the right combo box
            shiftChangeValue = xRayData1.shiftChange(this);
            label2.Text = shiftChangeValue;
        }

    }


class xRayData
    {
        #region Methods


        public string shiftChange(frmSWChange form)
        {
            //Dont create a new instance, get it from the form parameter
            frmSWChange frmSWChange1 = form;

            string shiftLetter;

            if (frmSWChange1.cbShift.SelectedText == "Day")
            {
                shiftLetter = "D";
            }
            else if (frmSWChange1.cbShift.SelectedText == "Night")
            {
                shiftLetter = "N";
            }
            else if (frmSWChange1.cbShift.SelectedText == "Morning")
            {
                shiftLetter = "M";
            }
            else
            {
                shiftLetter = "ERROR";
            }


            return shiftLetter;
        }

        #endregion

    }




Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Unable to retrieve data from a combobox.

Posted 15 September 2011 - 08:04 AM

You are creating a new instance of the form, therefore, the value in the combobox will be blank.

This creates a new form...

frmSWChange frmSWChange1 = new frmSWChange();



This is horrible design. Why are you accessing the Form from a separate class? You should not be passing or trying to use UI controls/forms in a separate class. You should pass the string value from the combobox from the Form to your class.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5629
  • View blog
  • Posts: 12,076
  • Joined: 02-June 10

Re: Unable to retrieve data from a combobox.

Posted 15 September 2011 - 08:17 AM

First.. there is no need for all those if checks. Just take the first letter of whatever the selected text is.

Second... Reaching into another form like this is generally considered just plain wrong. There are lots of threads on it here. Two forms should never be this tightly bound. You should open the second form and raise events that the first form subscribes to. That way whenever the selection changes in the second form, the first is notified. There is no silly games with having to press a button on the first firm to retrieve the value from the second form.
See FAQ #2 - that's how often this comes up.


FAQ (Frequently Asked Questions - Updated Sep 2011

Spoiler




But ok... we'll move on from that.
Quit reaching in to an exact GUI control on form2. That's just evil.

On form2 create a handler for the combobox.SelectedIndexChanged event.

When the combobox selection is changed, you want to update a public property that other forms can ask for. Notice how we have completed eliminated that fugly string of if...else statements as well as kept the GUI control private and only access a public field.
        public string SomeValue;
        private void cboSomeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((ComboBox)sender).SelectedIndex > -1)
            {
                SomeValue = ((ComboBox) sender).SelectedItem.ToString().Substring(0, 1);
            }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1