Reputation: 1173
I am trying to have a click event navigate the webBrowser to a pre-set location + string but I can't seem to get it to work.
My biggest issue is maybe getting the string from one event to the click event?
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();
}
}
}
When button is clicked, it navigates the domain to a "unknown location" page on the website (The website is not owned by me).
The idea is to click the cell in the DataGridView which is a product code, then click the button which adds the product code to the set url and loads the url+string in the webBrowser.
Upvotes: 0
Views: 1306
Reputation: 757
Your vairable searchlink
isn't visible to your orderbutton_Click()
. A solution would be to declare the varible searchlink
outside of the methods in your class. In fact you are using completely different variables (both named searchlink
) inside your methods.
So for example:
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)
}
}
And as bkribbs told me this is called a variable scope. Thanks!
For solving your specific problem here is the new code:
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);
}
I hope I got your problem right :)
Upvotes: 2