按钮点击 URL + 字符串

Button Click URL + String

我正在尝试让点击事件将 webBrowser 导航到预设位置 + 字符串,但我似乎无法让它工作。

我最大的问题可能是从一个事件获取字符串到点击事件?

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();
        }
    }
}

单击按钮时,它会将域导航到网站上的 "unknown location" 页面(该网站不归我所有)。

想法是单击 DataGridView 中的产品代码单元格,然后单击将产品代码添加到集合 url 的按钮并加载 url+ 字符串网页浏览器。

您的 searchlink 对您的 orderbutton_Click() 不可见。一个解决方案是在 class 的方法之外声明变量 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);

    }

希望我能解决你的问题:)