开发者

Unable to retrieve data from combobox.

开发者 https://www.devze.com 2023-04-05 14:15 出处:网络
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 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 made the combobox public. My code is as follows.

public void button1_Click(object sender, EventArgs e)
{
    xRayData xRayData1 = new xRayData();
    string shiftChangeValue;
    shiftChangeValue = xRayData1.shiftChange();
    label2.Text = shiftChangeValue;
}
开发者_Go百科
public string shiftChange()
{
    frmSWChange frmSWChange1 = new frmSWChange();

    string shiftLetter;

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


    return shiftLetter;
}


Use Selected... for getting the selected item in combobox

frmSWChange1.cbShift.SelectedItem // gets the binded item
frmSWChange1.cbShift.SelectedText // gets the display text of the selected item
frmSWChange1.cbShift.SelectedValue // gets the value of the selected item


Take a look at this line in your method

frmSWChange frmSWChange1 = new frmSWChange();

you are creating a new instance of the form and this one has nothing to do with the selection you have made, this will not have any Text selected in the combobox. What you need is the reference to the current instance of the form wherein these selections are being made.


frmSWChange.cs

namespace X_Ray
{
public partial class frmSWChange : Form
{

    public frmSWChange()
    {
        InitializeComponent();
       frmSWChange1
    }

    private void btnReturnToMainMenu_Click(object sender, EventArgs e)
    {
        new frmMainMenu().Show();
        this.Close();
    }

    public void button1_Click(object sender, EventArgs e)
    {

        string shiftChangeValue;
        label1.Text = mtxtScrapDate.Text;
        derpderp1 = cbShift.SelectedText;
        xRayData xRayData1 = new xRayData();

        shiftChangeValue = xRayData1.shiftChange();
        label2.Text = shiftChangeValue;
    }

}
}

xRayData.cs

namespace X_Ray
{
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
}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消