2012-07-09 22 views
-1

我一直在尋找所有這方面的互聯網,並發現類似的,但沒有一個會爲我工作。我發現的所有東西都假設列表框位於主表單上,而不是輔助表單,或者代碼是針對C#或Visual Studio的舊版本(我正在使用VS2008)。需要幫助獲取列表框項目文本從輔助表格到主表格

我正在創建一個Web瀏覽器,它在主窗體上有一個按鈕(稱爲frmMyBrowser),用帶書籤URL的列表框(lstBookmark)打開一個對話框(frmBookmarks)。我需要能夠雙擊一個項目(書籤的URL)並將文本粘貼到主窗體的地址欄(cmbAddress)中。

任何幫助將非常感激。

+0

請發表你到目前爲止的代碼,這將有助於別人理解的錯誤,並幫助你得到你的答案。 – 2012-07-09 09:58:02

+0

您可能需要一些客戶端編程來從frmBookmarks中檢索值,並將其複製到cmbAddress。 或者您可以在服務器端執行此操作,並在mainform上使用updatepanel來設置新值。 – Maarten 2012-07-09 10:16:16

+0

也許你可以將對話的結果存儲在一個可以從主窗體訪問的字符串中?還有一點需要注意的是,這與使用匈牙利符號的常見C#命名約定相反。例如,你的'lstBookmark'應該叫'Bookmark(List)Box'。 – lesderid 2012-07-09 10:30:59

回答

0

我想,如果我在運行時創建窗口,我可以讓它工作。如果有人感興趣,我使用的代碼如下。

公共無效frmMyBrowser_ShowFavorites(對象發件人,EventArgs的)

{ 

     frmFavorites.ShowIcon = false; 
     frmFavorites.ShowInTaskbar = false; 
     frmFavorites.MinimizeBox = false; 
     frmFavorites.MaximizeBox = false; 
     frmFavorites.ControlBox = false; 
     frmFavorites.Text = "Bookmarks"; 
     frmFavorites.Width = 500; 
     frmFavorites.Height = 350; 
     frmFavorites.Controls.Add(lstFavorites); 
     frmFavorites.Controls.Add(btnRemoveFavorite); 
     frmFavorites.Controls.Add(btnAddFavorite); 
     frmFavorites.Controls.Add(btnCloseFavorites); 
     frmFavorites.Controls.Add(txtCurrentUrl); 
     lstFavorites.Width = 484; 
     lstFavorites.Height = 245; 
     btnRemoveFavorite.Location = new Point(8, 280); 
     btnAddFavorite.Location = new Point(8, 255); 
     btnCloseFavorites.Location = new Point(400, 255); 
     txtCurrentUrl.Location = new Point(110, 255); 
     txtCurrentUrl.Size = new Size(255, 20); 
     btnAddFavorite.Text = "Add"; 
     btnRemoveFavorite.Text = "Remove"; 
     btnCloseFavorites.Text = "Close"; 
     txtCurrentUrl.Text = wbBrowser.Url.ToString(); 
     btnAddFavorite.Click += new EventHandler(btnAddFavorite_Click); 
     btnRemoveFavorite.Click += new EventHandler(btnRemoveFavorite_Click); 
     frmFavorites.Load += new EventHandler(frmFavorites_Load); 
     btnCloseFavorites.Click += new EventHandler(btnCloseFavorites_Click); 
     lstFavorites.MouseDoubleClick += new MouseEventHandler(lstFavorites_MouseDoubleClick); 
     frmFavorites.Show(); 
} 

public void btnCloseFavorites_Click(object sender, EventArgs e) 
{ 
if (lstFavorites.Items.Count > 0) 
{ 
using (StreamWriter writer = new System.IO.StreamWriter(@Application.StartupPath + "\\favorites.txt")) 
{ 
for (int i = 0; i < lstFavorites.Items.Count; i++) 

{ 

writer.WriteLine(lstFavorites.Items[i].ToString()); 

} 

writer.Close(); 
} 

} 
    frmFavorites.Hide(); 
} 

public void btnAddFavorite_Click(object sender, EventArgs e) 

    { 
     string strFavoriteAddress = wbBrowser.Url.ToString(); 
     if (!lstFavorites.Items.Contains(strFavoriteAddress)) 
     { 
      lstFavorites.Items.Add(strFavoriteAddress); 
      MessageBox.Show("Favorite Added", "Message"); 

     } 
     else if (lstFavorites.Items.Contains(strFavoriteAddress)) 
     { 
      MessageBox.Show("This site already exists in your Favorites list!", "Error"); 

     } 
     else 
     { 

     } 


    } 
    public void btnRemoveFavorite_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      lstFavorites.Items.RemoveAt(lstFavorites.SelectedIndices[0]); 

     } 

     catch 
     { 
      MessageBox.Show("You need to select an item", "Error"); 

     } 

    } 
    public void frmFavorites_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      using (StreamReader reader = new System.IO.StreamReader(@Application.StartupPath + "\\favorites.txt")) 
      { 
       while (!reader.EndOfStream) 
       { 
        for (int i = 0; i < 4; i++) 
        { 
         string strListItem = reader.ReadLine(); 
         if (!String.IsNullOrEmpty(strListItem)) 
         { 
          lstFavorites.Items.Add(strListItem); 
         } 
        } 

       } 
       reader.Close(); 
      } 



     } 
     catch 
     { 
      MessageBox.Show("An error has occured", "Error"); 
     } 

    } 
    private void lstFavorites_MouseDoubleClick(object sender, MouseEventArgs e) 
    { 
     string strSelectedAddress = lstFavorites.Text.ToString(); 
     cmbAddress.Text = strSelectedAddress; 
     wbBrowser.Navigate(strSelectedAddress); 
     frmFavorites.Hide(); 
    }