Here is my code:
private void button1_Click(object sender, EventArgs e)
{
string x = textBox1.Text;
System.Diagnostics.Process.Start("http://www.google.com/search?q="+x+"&btnI");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
Simple code, but I don't want the program to go to the link of the Textbox1.text on youtube. I want the program to just give me the link of the search back and not to go there.
I want to put a word on the text box and when I press the button it should give me the link of youtube and not to go there (like my program does at the moment it goes to the youtube link).
Couldn't explain better. Hope you guys can understand what I wanna do.
I assume you want to retrieve the URL that is returned by google when the query is fired. One possibility would be to use the HttpClient class in order to retrieve the request and from it the requested URL from the RequestMessage property:
var url = "https://www.google.com/search?q=stackoverflow&btnI";
var http = new HttpClient();
var response = http.GetAsync(url);
Console.WriteLine(response.Result.RequestMessage.RequestUri.AbsoluteUri);
The output is:
http://stackoverflow.com/
Related
Having a WebBrowser control, I roughly do the following steps:
Navigate to "about:blank".
Turn on design mode in DocumentCompleted event handler.
Paste a HTML string with an # as the URL.
Read the document back from the WebBrowser control.
Step 2 is done by this way:
private void webBrowser1_DocumentCompleted(
object sender,
WebBrowserDocumentCompletedEventArgs e)
{
dynamic axObj = webBrowser1.ActiveXInstance;
axObj.document.designmode = "On";
}
Step 3 is done this way:
private void button1_Click(object sender, EventArgs e)
{
var doc = (HTMLDocument)webBrowser1.Document.DomDocument;
var selection = doc.selection;
var range = (IHTMLTxtRange)selection.createRange();
range.pasteHTML("<p>Read more</p>");
}
Step 4 is done this way:
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(this, webBrowser1.DocumentText);
}
What I expect:
I would expect to get a HTML string like this:
<html><body>
<p>Read more</p>
</body></html>
What I actually get:
I get an HTML string where the # URL is prefixed with the current document's URL:
<html><body>
<p>Read more</p>
</body></html>
This happens, no matter whether I navigate to about:blank or e.g. https://www.google.com or any other URL.
My question:
Is there any way to prevent IE/mshtml/WebBrowser control from prefixing the currently loaded URL when pasting anchors?
Update 1:
A possible workaround I can think of is to paste an absolute URL like e.g. http://pseudo-hash.com instead of the # and later when getting the HTML back from the WebBrowser control do a string replace and replace the pseudo placeholder URL back with #.
I have a few buttons that send text string data from a textbox to a textblock on another page. Please see code below.
private void button_Click(object sender, RoutedEventArgs e)
{
Application.Current.Properties["obj1"] = textbox.Text;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Application.Current.Properties["obj2"] = textbox2.Text;
}
If i wanted to use a button click to stop/cancel the information being sent, how would I do this?
The page where the text string is received is below
protected override void OnInitialized(EventArgs e)
{
base.OnInitialized(e);
textBlock1.Text = Application.Current.Properties["obj1"]?.ToString();
textBlock2.Text = Application.Current.Properties["obj2"]?.ToString();
}
Really you can't stop the send, you can only undo the result. The process of the send is out of reach of your code long before the user has time to react to it, it's a limitation of reaction time. Same way you can't unplug the cat5 to stop an email being sent like the old joke, by the time you reconsider sending, it's long gone and out of your hands.
After all the cancel/stop would take close to as long as the original send, and add the half second of human reaction time...
Your best solution is likely to just set the dictionary value for that key to null, but since we don't see any of your usage this is only a best guess on what will work for you.
I was asked by a friend to develop a winform app to be able to extract data. I figured it would be easy enough - how wrong I was!
In my winform, I have included a webbrowser control and some buttons. The URL for the webbrowser is http://www.racingpost.com/greyhounds/card.sd and as you can imagine, it is the place to get data for greyhounds. When on the page above, there are a number of links within this area which are specific to a race time. If you click on any of these, it takes you to that race, and its this data that I need to extract. So, my initial thoughts were to get ALL links off the link above, then store them in a list, then just have a button available to take in whatever link it is, and then take the webbrowser to that location. Once there, I can then look to extract the data and store it as needed.
So, in the first instance, I use
//url = link above
wb1.Url = new Uri(url);
grab the data (which are links for each race on that day)
once I have this, use a further button to go to the specific race
wb1.Url = new Uri("http://www.racingpost.com/greyhounds/card.sd#resultday=2015-01-17&raceid=1344640");
then, once there, click another button to capture the data, after which, return to the original link above.
The problem is, it will not go to the location present in the link. BUT, if I click the link manually within the webbrowser, it goes there no problem.
I have looked at the properties of the webbrowser, and these all look fine - although I can't qualify that tbh!
I know if I try to go to the links manually, I can, but if I try to do it through code, it just wont budge. I can only assume I have done something wrong in the code.
Hope some of that makes sense - first posting, so apologies if I made a mess of it. I will provide all code no problem, but cant seem to figure out how to post the code in 'code format'?
//here is the code
public partial class Form1 : Form
{
Uri _url;
public Form1()
{
InitializeComponent();
wb1.Url = new Uri("http://www.racingpost.com/greyhounds/card.sd");
wb1.Navigated +=new WebBrowserNavigatedEventHandler(wb1_Navigated);
}
classmodules.trackUrl tu;
private void btnGrabData_Click(object sender, EventArgs e)
{
classmodules.utility u = new classmodules.utility();
rtb1.Text = u.GetWebData("http://www.racingpost.com/greyhounds/card.sd");
HtmlDocument doc = wb1.Document;
string innerText = (((mshtml.HTMLDocument)(doc.DomDocument)).documentElement).outerHTML;
innerText = Regex.Replace(innerText, #"\r\n?|\n", "");
rtb1.Text = innerText;
tu = new classmodules.trackUrl();
u.splitOLs(ref tu, innerText);
classmodules.StaticUtils su = new classmodules.StaticUtils();
su.SerializeObject(tu, typeof(classmodules.trackUrl)).Save(#"d:\dogsUTL.xml");
classmodules.ExcelProcessor xl = new classmodules.ExcelProcessor();
xl.createExcel(tu);
}
private void wb1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb1 = sender as WebBrowser;
this.Text = wb1.Url.ToString();
}
void wb1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
_url = e.Url;
}
private void btnGoBack_Click(object sender, EventArgs e)
{
goBack();
}
private void goBack()
{
wb1.Url = new Uri("http://www.racingpost.com/greyhounds/card.sd");
}
private void btnGetRaceData_Click(object sender, EventArgs e)
{
HtmlDocument doc = wb1.Document;
string innerText = (((mshtml.HTMLDocument)(doc.DomDocument)).documentElement).outerHTML;
rtb2.Text = innerText;
}
//###############################
//OK, here is the point where I want to take in the URL and click a button //to instruct the webbrowser to go to that location. I add an initial //counter to 0, and then get the first url from the list, increment the //counter, then when I click the button again, urlNo wil be 1, so then it //tries the second url
int urlNo = 0;
private void btnUseData_Click(object sender, EventArgs e)
{
if (tu.race.Count > urlNo)
{
string url = tu.race[urlNo].url;
wb1.Url = new Uri(url);
lblUrl.Text = url;
urlNo++;
}
else
{
lblUrl.Text = "No More";
}
}
Did you try the Navigate(...) method? In theory, the behavior of Navigate and Url is the same, but I can infer that they behave a bit different.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigate(v=vs.110).aspx
I am using the Gekofx browser because my html files don't work with the default webbrowser control.
So far I was using ObjectForScripting to call javascript code from my C# project. But I was not able to call anything with the Gekofx browser.
I just want to send some data to my html file and display it with the Gekofx browser. Is it possible at all?
For contemplation here is my code:
GeckoWebBrowser myBrowser;
public Form1()
{
InitializeComponent();
String path = #"C:\tools\xulrunner\xulrunner-sdk\bin";
Console.WriteLine("Path: " + path);
Skybound.Gecko.Xpcom.Initialize(path);
myBrowser = new GeckoWebBrowser();
myBrowser.Parent = this;
myBrowser.Dock = DockStyle.Fill;
}
private void btn_go_Click(object sender, EventArgs e)
{
// like the normal browsers
myBrowser.Navigate(tbx_link.Text);
}
private void btn_test_Click(object sender, EventArgs e)
{
// getting the link to my own html file
String path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Webpage");
path += "\\Webpage.html";myBrowser.Navigate(path);
}
I hope you understand what I mean. Thank you!
You can always invoke javascript like this:
mybrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");
Building on #jordy's answer above, call:
mybrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");
prefferably in the Document complete event handler to allow for the page to first load.
void myBrowser_DocumentCompleted(object sender, Gecko.Events.GeckoDocumentCompletedEventArgs e)
{
myBrowser.Navigate("javascript:YourJavascriptFunction('yourArgument1', 'youArgument2')");
}
I think my tittle is inaccurate.
When a user clicks on a button I need it to do this:
Response.Redirect("Login.aspx?userid=XX");
How can I get the "userid?" from ?userid. so I can show a page. Like doing "?page=3" and show page 3, in the same page or something.
The Button code is: (just if you need it)
protected void LoginButton_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
Thanks a lot! Sorry if I didn't ask it good, and sorry for the bad English.
Use Request.QueryString:
First Page Sends them another page with their user id in the url:
Response.Redirect("AfterLogIn.aspx?userid=23");
You then Read it using the below code:
var g = Request.QueryString["userid"] //this value should be 23 now
You could then use this g variable to do any amount of custom things (Hide panels, show controls, etc.)
You can do something like this
protected void LoginButton_Click(object sender, EventArgs e)
{
var id = // whatever userid
Response.Redirect("Login.aspx?userid="+ id);
}
and in the pageload of Login page
var userid = Request.QueryString["userid"];
ASP.NET State Management will explain further.
Hope this helps