c# - Button Click URL + String -
i trying have click event navigate webbrowser pre-set location + string can't seem work.
my biggest issue maybe getting string 1 event 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 sure want update 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 clicked, navigates domain "unknown location" page on website (the website not owned me).
the idea click cell in datagridview product code, click button adds product code set url , loads url+string in webbrowser.
your vairable searchlink
isn't visible orderbutton_click()
. solution declare varible searchlink
outside of methods in class. in fact using different variables (both named searchlink
) inside methods. example:
class testclass { string teststring1 = ""; //visible in both methods private void testmethod1() { string teststring2 = ""; //only visible in method teststring1 = "it works!"; } private void testmethod2() { teststring2 = "this won't compile"; //teststring2 not visible here teststring1 = "it works, too"; //but doing is: string teststring2 = ""; //new variable (not related teststring2 above) } }
and bkribbs told me called variable scope. thanks!
for solving specific problem here 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 got problem right :)
Comments
Post a Comment