2014-01-08 40 views
1

我只是封閉這一個在列表中的IP主機的關係,到處搜尋,但有一個艱難的一個發現填寫一個文本框與

在我windowsform C#應用程序,我在VS正在開發快遞2012我想實現以下目標:

我IP主機名關係以純文本列表,像這樣:

99999 10.10.10.10 
88888 10.10.10.11 

1567線共

在表單中我有兩個文本框,一個帶有自動完成功能(項目列表只是主機名),第二個應該顯示關於輸入主機名的IP,只要用戶從自動填充中選擇主機名收視率 - 邀請

很抱歉,但我堅持這一個,幫助請提前

+0

我不確定你在問什麼...你需要幫助解析文本?或者在文本框中使用自動完成功能? –

+0

是的,在解析文本!我真的不知道如何管理_「查看關係列表並顯示與主機名相關的IP」_ – mfgarcia

回答

1

謝謝你不能只創建一個類?像這樣

class ips 
{ 
    public int ident; 
    public string ip; 
    public ips(int Ident, string Ip) 
    { 
     this.ident = Ident; 
     this.ip = Ip; 
    } 
} 
//fill the List 
const int MAXELEMENTS = 512; 

ips[] ipList = new ips[MAXELEMENTS]; 
ips ipa = new ips(1111, "10.10.10.1"); 
ips ipb = new ips(2222, "20.20.20.1"); 

然後激發SelectionChangeCommitted事件?

public Form1() 
{ 
    ipList[0] = ipa; 
    ipList[1] = ipb; 

    InitializeComponent(); 
    cBox1.AutoCompleteMode = AutoCompleteMode.Append; 
    for(int i = 0; i < ipList.Count(); i++) 
    { 
     if(ipList[i] != null) 
      cBox1.Items.Add(ipList[i].ip); 
    } 
} 

private void cBox1_SelectionChangeCommitted(object sender, EventArgs e) 
{ 
     tBox1.Text = Convert.ToString(ipList[cBox1.SelectedIndex].ident); 
} 

我希望這有助於你

1

對,所以我們要你的例子:

99999 10.10.10.10 
88888 10.10.10.11 

並解析這些到字典中,像這樣:

private Dictionary<string, string> ParseText(string text) 
{ 
    Dictionary<string, string> hostnameDictionary= new Dictionary<string, string>(); 
    foreach(var line in text.Trim().Split(Environment.NewLine))) 
    { 
     string[] textParts = line.Trim().Split(' '); 

     if(textParts.Length == 2 && !hostnameDictionary.ContainsKey(textParts[0]) 
     { 
      hostnameDictionary.Add(textParts[0], textParts[1]); 
     } 
    } 
    return hostnameDictionary; 
} 

請注意,只有當其他地方沒有空間時,上述纔會起作用,所以如果主機名中有一個空格,那麼上面就不會解析它。

使用上面的方法,你會得到它使用的主機名作爲鍵的字典,所以你可以很容易地找到IP屬於要什麼有什麼字典,因此,例如打字hostnameDictionary["99999"]將返回字符串"10.10.10.10"

如果這不清楚,或者你需要另一部分的幫助,讓我知道,我會更新答案。