简体   繁体   中英

populating text boxes from selected item in combo box

I have added a new interface to an existing utility that will read data stored on an external device. This requires taking XML response and deserializing them to a list. Here is the xml and the code for list creation (to copy all element, attribute, type declarations would be too lengthy here.):

XML:

<?xml version="1.0" encoding="UTF-8"?>
<Device>
<Response>
    <command>Get</command>
    <argument>AllMethod</argument>
</Response>
<AllMethod>
<Method>
    <nMethodNumber>1</nMethodNumber>
    <sMethodName>Method-8</sMethodName>
    <fFlowRate>3.00</fFlowRate>
    <nTubeTemp>250</nTubeTemp>
    <nTestTime>150</nTestTime>
</Method>
<Method>
    <nMethodNumber>2</nMethodNumber>
    <sMethodName>Method-8</sMethodName>
    <fFlowRate>3.00</fFlowRate>
    <nTubeTemp>250</nTubeTemp>
    <nTestTime>150</nTestTime>
</Method>
<Method>
    <nMethodNumber>3</nMethodNumber>
    <sMethodName>Method-8</sMethodName>
    <fFlowRate>3.00</fFlowRate>
    <nTubeTemp>250</nTubeTemp>
    <nTestTime>150</nTestTime>
</Method>
<Method>
    <nMethodNumber>4</nMethodNumber>
    <sMethodName>Method-8</sMethodName>
    <fFlowRate>3.00</fFlowRate>
    <nTubeTemp>250</nTubeTemp>
    <nTestTime>150</nTestTime>
</Method>
<Method>
    <nMethodNumber>5</nMethodNumber>
    <sMethodName>Method-8</sMethodName>
    <fFlowRate>3.00</fFlowRate>
    <nTubeTemp>250</nTubeTemp>
    <nTestTime>150</nTestTime>
</Method>
<Method>
    <nMethodNumber>6</nMethodNumber>
    <sMethodName>Method-8</sMethodName>
    <fFlowRate>3.00</fFlowRate>
    <nTubeTemp>250</nTubeTemp>
    <nTestTime>150</nTestTime>
</Method>
<Method>
    <nMethodNumber>7</nMethodNumber>
    <sMethodName>Method-8</sMethodName>
    <fFlowRate>3.00</fFlowRate>
    <nTubeTemp>250</nTubeTemp>
    <nTestTime>150</nTestTime>
</Method>
<Method>
    <nMethodNumber>8</nMethodNumber>
    <sMethodName>Method-8</sMethodName>
    <fFlowRate>3.00</fFlowRate>
    <nTubeTemp>250</nTubeTemp>
    <nTestTime>150</nTestTime>
</Method>   
</AllMethod>
</Device>

Create List:

public class AllMethod
{
[XmlElement("Method")]
//Puts Methods into list for deserialization.
public List<Method> methodlist = new List<Method>();
}

I have created the XML document locally to test the functionality of the program before going live. So in my Read Methods button event I have the following code:

private void btnReadMethod_Click(object sender, System.EventArgs e)
{
        string stringRequestXML = string.Empty;
        string stringResponseXML = string.Empty;

{
        //Will deserialize xml response and populate combo box with method numbers.
        //try
        //{
        XmlSerializer deserializer = new   XmlSerializer(typeof(ReadMethodResponse));
        StreamReader reader = new StreamReader(@"C:\XML\GetAllXml.xml");
        object obj = deserializer.Deserialize(reader);
        ReadMethodResponse XmlData = (ReadMethodResponse)obj;
        reader.Close();


        //}
        //catch (Exception ex)
        //{
        // throw ex;
        //}

        cmbMethodNumber.Items.Clear();
        foreach (Method details in XmlData.allMethods.methodlist)
        {
        cmbMethodNumber.Items.Add(details.MethodNumber);
        }



        }

        }

    private void cmbMethodNumber_SelectedIndexChanged(object sender, EventArgs e)
    {
        int MethodDetailsIndex = cmbMethodNumber.SelectedIndex;
        Method selectedMethod = xmlData.allMethods.methodlist[MethodDetailsIndex];
        txtMethodName.Text = selectedMethod.MethodName.ToString();
        txtFlow.Text = selectedMethod.FlowRate.ToString();
        txtTubeTemp.Text = selectedMethod.TubeTemp.ToString();
        txtTestTime.Text = selectedMethod.TestTime.ToString();
    }


}

I have a combobox to display method number and three textboxes to display method name, flow rate, test time, and temp. I am trying to populate the text boxes with the correct information based on the selected value (Method Number) of the combobox. My code for deserializing the xml is functioning fine but I am having trouble populating the text boxes with the corresponding data. I cannot access the list from the cmbMethodNumber_SelectedIndexChanged. This is probably fairly simple but I am fairly new to c# and google is not my friend today, any advice?

There isn't a lot of meaningful information to go on, but it seems like the problem is just variable scoping. Essentially, you're doing this:

private void btnReadMethod_Click(object sender, System.EventArgs e)
{
    var xmlData = SomeFunctionToGetData();
}

private void cmbMethodNumber_SelectedIndexChanged(object sender, EventArgs e)
{
    var someValue = xmlData.SomeValue;
}

Naturally, xmlData doesn't exist in the second function, so it can't be referenced. It was declared in the scope of the first function, and once that function ended it was removed from scope. This doesn't have anything to do with your XML or the overall business problem you're trying to solve, it's just a variable scope error.

In order for multiple handlers to access this data (that is, in order for the scope of the data to last across multiple events on the form), it needs to be a class-level object. Something more like this:

private TheTypeOfTheObject xmlData;

private void btnReadMethod_Click(object sender, System.EventArgs e)
{
    xmlData = SomeFunctionToGetData();
}

private void cmbMethodNumber_SelectedIndexChanged(object sender, EventArgs e)
{
    var someValue = xmlData.SomeValue;
}

I've added the variable to class-level scope, and removed the declaration in the first function. Now the first function sets the class-level value, and the second function reads the class-level value. Of course, this makes a few assumptions:

  • The second function will never run unless the first function has successfully run before it.
  • Other handlers aren't also modifying the value.

You probably want to add some error checking and exception handling if there are scenarios where these assumptions aren't true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM