WebBrowser URI in Listbox - c#

How to get the URI from webbrowser in listbox??
this code add a 20 URI not 1:
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
radListControl1.Items.Add(webBrowser1.Url.AbsoluteUri.ToString());
}
or
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
radListControl1.Items.Add(webBrowser1.Url.AbsoluteUri.ToString());
}

if (!radListControl1.Items.Contains(webBrowser1.Url.ToString()))
radListControl1.Items.Add(webBrowser1.Url.ToString());
Because this event is fired multiple times in single page load...

you need to check the URI provided by the event against the one in browser:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.Equals(webBrowser1.Url))
// this is the real one
}
EDIT: actually, it has already been answered.

Related

TimerTick event is not firing when hooked inside !IsPostBack

New to asp.net.
My motive is,
"User has to be taken to some page after a predefined set of the time interval.
Session should not be used."
So, I thought to use Timer and inside the timer tick event, I can do a Server.Redirect. This timer is inside the user control page, which is common across all pages.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
redirectTimer.Interval = 20000;
redirectTimer.Tick += new EventHandler<EventArgs>(redirectTimer_Tick);
}
}
void redirectTimer_Tick(object sender, EventArgs e)
{
Server.Transfer("~/SomePageGoesHere.aspx");
}
Case 2:
protected void Page_Load(object sender, EventArgs e)
{
redirectTimer.Interval = 20000;
redirectTimer.Tick += new EventHandler<EventArgs>(redirectTimer_Tick);
}
void redirectTimer_Tick(object sender, EventArgs e)
{
Server.Transfer("~/SomePageGoesHere.aspx");
}
But in this case, it worked.
My question is,
Whether "!IsPostBack" is having something to do with timer? (Case 1 and 2).
Is there any better approach available other than this timer, session or cookies. etc?
Could someone share some input here?

How To Clear Rich Text box Using A Button

How do you clear a rich text box using a button?
private void clearButton_Click(object sender, EventArgs e)
{
presentedCSRRichText.Text.Clear();
}
it's probably really simple to do and i can't think of how to do it.
private void clearButton_Click(object sender, EventArgs e)
{
presentedCSRRichText.Clear();
}

Transfer a method to another method

I'm new here and I have a doubt. It is possible to transfer a method to another method?
private void button1_Click(object sender, EventArgs e)
{
}
private void c_Click(object sender, EventArgs e)
{
MessageBox.Show("Transfer OK!!!");
}
private void button2_Click(object sender, EventArgs e)
{
// c_Click ????? ------> button1_Click
}
By clicking on Button2, via code is included c_Click the content within the button1.
Finally, clicking on button1, I need to bring up the "Transfer OK" message. Is this possible?
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Default Message");
c.PerformClick();
}
private void c_Click(object sender, EventArgs e)
{
MessageBox.Show("Transfer OK!!!");
}
Button.PerformClick Method used to call button click event in any method
I think you can use something like this inside your button2_Click:
button1.PerformClick();
or
c_Click(sender, e);
private void button1_Click(object sender, EventArgs e)
{
c_Click(sender, e);
}
I honestly don't know if I understand your question but based on your comments:
I do not want to run "PerformClick ()" or anything similar. Basically,
it would delete the content of the button1_Click and include C_Click
content within the button1_Click.
Clicking on button1, I need to bring up the "Transfer OK" message.
Button2 will have code that will perform the deletion of button1
content and will include the contents of C_Click event. I guess it's
something using "Delegates" or similar.
You're probably not using the word "Content" in the Windows Forms sense of the word. What I get from that is that when you click button2, you want button1 to start acting like the c_Click button, correct?
If I get you correctly you need to simply remove button1's EventHandler for button1_Click and replace it with c_Click, like so:
private void button1_Click(object sender, EventArgs e)
{
}
private void c_Click(object sender, EventArgs e)
{
MessageBox.Show("Transfer OK!!!");
}
private void button2_Click(object sender, EventArgs e)
{
// c_Click ????? ------> button1_Click
button1.Click -= new EventHandler(button1_Click);
button1.Click +=new EventHandler(c_Click);
}
So that after you click button2, the next time you click on button1, it will do c_Click() instead of button1_Click()

Create event handler for OnScroll for web browser control

Has any one successfully trapped the event of mouse scroll in a web browerser component?
I have two web browser controls i would like to scroll at the same time.
But there are no scroll events for web browsers.
I would like to create an event something like this below? has any one done or seen this before?
private void webCompareSQL_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Document.Window.AttachEventHandler("OnScroll");
}
Here i would call my event and proceed with the code.
private void windowEvents_OnScroll()
{
int nPos = GetScrollPos(webCompareSQL.Handle, (int)ScrollBarType.SbVert);
nPos <<= 16;
uint wParam = (uint)ScrollBarCommands.SB_THUMBPOSITION | (uint)nPos;
SendMessage(WebPrevSQL.Handle, (int)Message.WM_VSCROLL, new IntPtr(wParam), new IntPtr(0));
}
I have found this code but don't know how to use it. its an event.
webCompareSQL.Document.Window.Scroll
I was able to get this working as follows. This example assumes that both web browser controls are navigating to the same Url. I am also syncing the horizontal scrollbar in addition to the vertical - this can be omitted if it is not required.
webBrowser1.DocumentCompleted
+= new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser2.DocumentCompleted
+= new WebBrowserDocumentCompletedEventHandler(webBrowser2_DocumentCompleted);
NavigateToPage("www.google.com");
....
private void NavigateToPage(string url)
{
webBrowser1.Navigate(url);
webBrowser2.Navigate(url);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Window.AttachEventHandler("onscroll", OnScrollEventHandler1);
}
private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser2.Document.Window.AttachEventHandler("onscroll", OnScrollEventHandler2);
}
public void OnScrollEventHandler1(object sender, EventArgs e)
{
webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollTop
= webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop;
webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollLeft
= webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollLeft;
}
public void OnScrollEventHandler2(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop
= webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollTop;
webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollLeft
= webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollLeft;
}
I note your comment in How to retrieve the scrollbar position of the webbrowser control in .NET, relating to this operation
webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop
not working. I can confirm that this definitely works on my machine, so if this code does not work on yours I can look into alternatives.
The real event name is "onscroll" not "OnScroll".
MSDN:http://msdn.microsoft.com/en-us/library/ie/ms536966(v=vs.85).aspx
Following code is firing the method when event occured.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Window.AttachEventHandler("onscroll", OnScrollEventHandler);
}
public void OnScrollEventHandler(object sender, EventArgs e)
{
}

Button1 not being clicked on startup

I am having an issue with my WinForm application.
Below I have my code for my button that I want to click.
private void button1_Click(object sender, EventArgs e)
{
// Do code.
}
Now I want to run the program on start up, so I have this code below:
private void form_Load(object sender, EventArgs e)
{
this.button1_Click(object sender, EventArgs e);
}
But this does not work. It has red lines under the words: "sender", "EventArgs e"
What am I doing wrong, Please help me?
Any help would be greatly appreciated, thanks!
First if you want to click button that way you should do:
private void form_Load(object sender, EventArgs e)
{
button1.PerformClick();
}
Second,
it is not a good idea to do it anyway, better approach is to create common method that is call in button_click event and form_load event.

Categories

Resources