2015-04-22 89 views
0

我想要一個點擊事件瀏覽webBrowser到預設位置+字符串,但我似乎無法讓它工作。按鈕點擊URL +字符串

我最大的問題是可能從一個事件獲取字符串到click事件?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace Ink 
{ 
    public partial class inkForm : Form 
    { 

     public inkForm() 
     { 
      InitializeComponent(); 
     } 

     string searchlink; 

     private void searchbutton_Click(object sender, EventArgs e) 
     { 
      this.AcceptButton = searchbutton; 

      int itemrow = -1; 
      String searchValue = searchtextBox.Text.ToUpper(); 

      if (searchValue != null && searchValue != "") 
      { 
       foreach (DataGridViewRow row in inkGridView.Rows) 
       { 
        if (row.Cells[1].Value.ToString().Equals(searchValue)) 
        { 
         itemrow = row.Index; 
         break; 
        } 
        else if (row.Cells[1].Value.ToString().Contains(searchValue) && itemrow == -1) 
        { 
         itemrow = row.Index; 
        } 
       } 
       if (itemrow == -1) 
       { 
        searchtextBox.BackColor = Color.Red; 
       } 
       else 
       { 
        searchtextBox.BackColor = Color.White; 
        inkGridView.Rows[itemrow].Selected = true; 
        inkGridView.FirstDisplayedScrollingRowIndex = itemrow; 
       } 
      } 
     } 

     private void inkForm_Load(object sender, EventArgs e) 
     { 
      this.hPTableAdapter.Fill(this.inkDataSet.HP); 

     } 

     private void updatebutton_Click(object sender, EventArgs e) 
     { 
      DialogResult dr = MessageBox.Show("Are you sure you want to update the stock level?", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); 
      if (dr == DialogResult.Yes) 
      { 
       this.hPTableAdapter.Update(inkDataSet.HP); 
       inkGridView.Refresh(); 
       MessageBox.Show("Record Updated.", "Success!"); 
      } 

     } 
     private void inkGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
      inkGridView.Columns["tonerInkDataGridViewTextBoxColumn"].ReadOnly = true; 
     } 

     private void inkGridView_SelectionChanged(object sender, EventArgs e) 
     { 
      if (inkGridView.SelectedCells.Count > 0) 
      { 
       int selectedrowindex = inkGridView.SelectedCells[0].RowIndex; 

       DataGridViewRow selectedRow = inkGridView.Rows[selectedrowindex]; 

       string searchlink = Convert.ToString(selectedRow.Cells["tonerInkDataGridViewTextBoxColumn"].Value); 
      } 
     } 

     private void orderbutton_Click(object sender, EventArgs e) 
     { 
      string link; 
      string searchlink = "blahblah"; 
      link = "http://store.tindallsb2b.co.uk/storefront/evolution_ProductResults.html?strSearch=" + searchlink; 
      webBrowser.Url = new Uri(link); 

     } 

     private void urlcheckertextbox_TextChanged(object sender, EventArgs e) 
     { 
      urlcheckertextbox.Text = webBrowser.Url.ToString(); 
     } 
    } 
} 

當按鈕被點擊時,它將導航到網站上的「未知位置」頁面(該網站不屬於我)。

這個想法是單擊DataGridView中的一個產品代碼中的單元格,然後單擊該按鈕將產品代碼添加到設置的url並在webBrowser中加載url +字符串。

+0

你有一個字符串searchlink =「」;但你永遠不會對這個變量產生任何價值。 –

+0

我不確定將要進入這個。 –

+0

從哪裏你應該得到你的網址的後綴? –

回答

2

您的可伸縮searchlink對您的orderbutton_Click()不可見。解決辦法是在類中的方法之外聲明變量searchlink。實際上,您在方法中使用了完全不同的變量(都命名爲searchlink)。 因此,例如:

class testclass 
{ 
    string teststring1 = ""; //visible in both methods 

    private void testmethod1() 
    { 
     string teststring2 = ""; //only visible in this method 
     teststring1 = "it works!"; 
    } 

    private void testmethod2() 
    { 
     teststring2 = "this won't compile"; //teststring2 is not visible here 
     teststring1 = "it works, too"; 

     //but what you are doing is: 
     string teststring2 = ""; //new variable (not related to teststring2 from above) 
    } 
} 

而作爲bkribbs告訴我,這就是所謂的變量範圍。謝謝!

對於這裏解決您的具體問題是新代碼:

string searchlink = ""; 

private void inkGridView_SelectionChanged(object sender, EventArgs e) 
    { 
     if (inkGridView.SelectedCells.Count > 0) 
     { 
      int selectedrowindex = inkGridView.SelectedCells[0].RowIndex; 

      DataGridViewRow selectedRow = inkGridView.Rows[selectedrowindex]; 

      searchlink = Convert.ToString(selectedRow.Cells["tonerInkDataGridViewTextBoxColumn"].Value); 
     } 
    } 

    private void orderbutton_Click(object sender, EventArgs e) 
    { 
     string link; 

     link = "http://store.tindallsb2b.co.uk/storefront/evolution_ProductResults.html?strSearch=" + searchlink; 
     webBrowser.Url = new Uri(link); 

    } 

我希望我得到了你的問題吧:)

+0

評論不用於擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/75969/discussion-on-answer-by-jibbow-button-click-url-string)。 – Taryn