将字符串数据与文本文件中的数据进行匹配,如果匹配,则“检查”已检查列表框的项目

时间:2014-11-20 10:08:08

标签: c#

我已经在表单加载的checkedlistbox中加载了textfile的数据。我必须匹配变量" ip"中收到的IP地址。使用ip地址存在于文本文件中,如果匹配发生,我必须选中标记我之前加载的checkedListBox的相应项。这是我的代码:

public partial class Form4 : Form
{
    public Form4()
    {
        InitializeComponent();
        server_client a=new server_client();
        Form1 b=new Form1();


        for (int i = 0; i < 50; i++)
        {    
            int j;
            string ip;
            IPEndPoint localIpEndPoint = b._clientsockets[i].LocalEndPoint as IPEndPoint;
            ip = IPAddress.Parse(((IPEndPoint)b._clientsockets[i].LocalEndPoint).Address.ToString()).ToString();
            string[] readText = File.ReadAllLines(@"C:\Users\cdac\Desktop\cdac_utility1\WindowsApplication1\TextFile1.txt");
            for (j = 0; j < 50; j++)
            {
                if (readText[j] == ip)
                {


                }
            }
        }

        int counter = 0;
        string line;

        // Read the file and display it line by line.
        System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\cdac\Desktop\cdac_utility1\WindowsApplication1\TextFile1.txt");

        //Now read the file line by line
        while ((line = file.ReadLine()) != null)
        {
            //add the line to CheckedListBox, you need to pass the parameters     "index"     & "string"
            checkedListBox1.Items.Insert(counter, line);

            //increase the index
            counter++;
        }

        //close the file
        file.Close();
    }  

我无法选中标记清单箱中的相应项目,我必须在成功匹配时标记该项目。

1 个答案:

答案 0 :(得分:0)

this.checkedListBox1.SetItemChecked(counter, true);