Click here to Skip to main content
16,011,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void ButtonGetListOfOPCServers_Click(object sender, EventArgs e)
        {
            
             obj = new tester(); 
            Opc.Server[] servers= obj.Getserver();
            if (servers!=null)
            {
                foreach(Opc.Server server in servers)
                {
                    ListBoxListOfOPCServers.Items.Add(server.Name);
                }
            }
            
         }


private void ListBoxListOfOPCServers_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            
            obj.M_server = ListBoxListOfOPCServers.SelectedItem as Opc.Da.Server;


        }


What I have tried:

i want to assign the server to what I selected in listbox.
but because it is server.Name i display , i cannot cast it back to server.

thank you!
Posted
Updated 16-Nov-17 20:28pm
Comments
Karthik_Mahalingam 17-Nov-17 0:12am    
"Name" is string property?
Member 13462842 17-Nov-17 0:41am    
yes
Karthik_Mahalingam 17-Nov-17 5:40am    
use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

1 solution

You should be able to get some ideas from this "sketch:"
using System;
// using Opc; ?

namespace YourNameSpace
{
    public class YourClass
    {
        private Tester TheTester;
        private Opc.Server[] Servers;
        private Opc.Da.Server CurrentServer;

        private void ButtonGetListOfOPCServers_Click(object sender, EventArgs e)
        {
            TheTester = new Tester();
            Servers = TheTester.Getserver();

            if (Servers == null)
            {
                // handle failure to get servers ?
                return;
            }
            else
            {
                ListBoxListOfOPCServers.DataSource = Servers;
                ListBoxListOfOPCServers.DisplayMember = "Name";
                ListBoxListOfOPCServers.SelectedIndexChanged += ListBoxListOfOPCServers_SelectedIndexChanged;
            }
        }

        private void ListBoxListOfOPCServers_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selNdx = ListBoxListOfOPCServers.SelectedIndex;

            if (selNdx == -1)
            {
                // handle no selection: update prior selection ?
                // throw error ?
                return;
            }

            CurrentServer = ListBoxListOfOPCServers.SelectedValue as Opc.Da.Server;

            if (CurrentServer == null)
            {
                // handle server access faiulure ?
                // throw error ?
                return;
            }

            TheTester.M_server = CurrentServer;
        }
    }
}
 
Share this answer
 
Comments
Graeme_Grant 17-Nov-17 4:10am    
+5 ... gotta love data binding ;)
Karthik_Mahalingam 17-Nov-17 5:41am    
5

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