我正在尝试点击事件将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();
}
}
}
单击按钮时,它会将域导航到网站上的“未知位置”页面(该网站不归我所有)。
想法是单击DataGridView中的产品代码单元格,然后单击将产品代码添加到设置URL的按钮,并在webBrowser中加载url +字符串。
答案 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);
}
我希望我能解决你的问题:)