I have below code,when i search from my winform textbox and click search button but webpage text box onclick event not fired. How to do this? here is :http://www.heathrow.com/arrivals when i click search button page still same position, show in 2nd number image. internet explorer 11 i have installed
private void button2_Click(object sender, EventArgs e)
{
HtmlDocument doc = webBrowser1.Document;
HtmlElement HTMLControl2 = doc.GetElementById("searchInput");
if (HTMLControl2 != null)
{
// HTMLControl2.Style = "display: none";
HTMLControl2.InnerText = textBox1.Text;
HTMLControl2.Focus();
SendKeys.SendWait("{ENTER}");
textBox1.Focus();
}
}
Here is the code:
Form1.cs:
private void textBox1_TextChanged(object sender, EventArgs e)
{
HtmlDocument doc = webBrowser1.Document;
HtmlElement HTMLControl2 = doc.GetElementById("searchInput");
if (HTMLControl2 != null)
{
// HTMLControl2.Style = "display: none";
HTMLControl2.InnerText = textBox1.Text;
HTMLControl2.Focus();
SendKeys.SendWait("{ENTER}");
textBox1.Focus();
}
}
Anything else is default.
HtmlDocument doc = webBrowser1.Document;
HtmlElement HTMLControl2 = doc.GetElementById("searchInput");
if (HTMLControl2 != null)
{
// HTMLControl2.Style = "display: none";
HTMLControl2.InnerText = textBox1.Text;
SendKeys.Send("{ENTER}");
textBox1.Focus();
HTMLControl2.Focus();
HTMLControl2.InnerText = textBox1.Text;
SendKeys.Send("{ENTER}");
textBox1.Focus();
HTMLControl2.Focus();
}
Related
I made a program that opens a Google Translate window when F1 is pressed.
I want the "source" textbox to get selected (focused on)
I tried this:
formMain.Activate();
formMain.panelMain.Enabled = false;
formMain.panelMain.Focus();
formMain.panelMain.Select();
formMain.panelMain.Enabled = true;
formMain.webBrowserMain.BringToFront();
formMain.webBrowserMain.Select();
formMain.webBrowserMain.Focus();
formMain.ActiveControl = formMain.webBrowserMain;
if (formMain. WindowState != FormWindowState.Minimized)
{
Program.DoMouseClick((uint)formMain.PointToScreen(formMain. webBrowserMain.Location).X + 10, (uint)formMain.PointToScreen(formMain.webBrowserMain.Location).Y + 10);
}
HtmlElement textArea = formMain.webBrowserMain.Document.GetElementById("source");
if (textArea != null)
{
textArea.Focus();
}
But it only sometimes gets selected!
Try this code :
This includes injecting custom Js in the site and calling that function , works for me in a similar case (Always do operations on a webpage after it has completed rendered/loaded):
private void webBrowser1_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function trig_focus() { document.getElementById(\"theIdOfInputBox\").focus(); }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("trig_focus");
}
What i want to do is:
When the user start typing anything in the textBox2 first make it empty and then start showing what the user is typing.
Then if the user deleting what he was typing then show the original text again.
Now what it does is that i need to delete on my own the text inside but then when i try to type anything i see the original text again since it's empty.
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (textBox2.Text != "" || textBox2.Text == "Enter Email Account")
{
textBox2.Text = "Enter Email Account";
button2.Enabled = false;
}
else
{
button2.Enabled = true;
}
}
You should be using a Watermark. But in case you wish to do it as you started, you set the default value in properties.
You need first of all to create your event handlers during the Enter and Leave event;
private void Form1_Load()
{
textBox1.Text = "Enter Email Account";
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
textBox1.LostFocus += new EventHandler(textBox1_Leave);
}
Then on Enter, the watermark text should be deleted;
protected void textBox1_GotFocus(object sender, EventArgs e)
{
if (textBox1.Text == "Enter Email Account")
{
textBox1.Text = "";
}
}
Then you re-check on Leave event;
protected void textBox1_Leave(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
textBox1.Text = "Enter Email Account";
}
}
I create a custom control inside a tab control which contains a flowLayoutPanel on every tab by dragging files on the selected tab. I have a context menu to rename and delete tab pages, but i want also to be able to delete a button created when I right click on it and select "remove"... I cannot find a way to delete only the selected button..
This is what I have to create the buttons:
public void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string s in fileList)
{
var button = new Button();
path_app = String.Format("{0}", s);
string filename = path_app;
file_name = Path.GetFileName(path_app);
Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filename);
Bitmap bmp = icon.ToBitmap();
CustomControl custom_btn = new CustomControl(button, new Label { Text = file_name });
button.Tag = path_app;
button.BackgroundImage = bmp;
button.BackgroundImageLayout = ImageLayout.Stretch;
FlowLayoutPanel selectedFLP = (FlowLayoutPanel)tabControl1.SelectedTab.Controls[0];
selectedFLP.Controls.Add(custom_btn);
button.Click += new EventHandler(button_Click);
ContextMenu cm2 = new ContextMenu();
cm2.MenuItems.Add("Remove", new EventHandler(rmv_btn_click));
custom_btn.ContextMenu = cm2;
}
}
private void rmv_btn_click(object sender, System.EventArgs e)
{
foreach (Control X in fl_panel.Controls)
{
fl_panel.Controls.Remove(X);
}
}
How do I get the button which I right click on it as sender in the rmv_btn_click event to know which one to delete?
If I understand what you mean, You need to use something like this.
private void rmv_btn_click(object sender, System.EventArgs e)
{
fl_panel.Controls.Remove(sender as Button);
}
private void rmv_btn_click(object sender, System.EventArgs e)
{
Button btn = new Button();
Label lbl = new Label();
CustomControl cst_btn = new CustomControl(btn, lbl);
cst_btn = sender as CustomControl;
DialogResult dialogResult = MessageBox.Show("Are you sure that you want to remove this object?", "Remove object", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
cst_btn.Dispose();
}
else if (dialogResult == DialogResult.No)
{
//do nothing
}
}
public EventHandler handlerGetter(CustomControl button)
{
return (object sender, EventArgs e) =>
{
rmv_btn_click(button, e);
};
}
I'm new to C# so I looked for this topic in other questions but they weren't for me. What I am trying to do is I currently try to login to my school's servers using a c# program(Which I'm trying to implement). What I'm trying to do is I know the code of the page, so I am using web browser of c# to navigate then I just want to write name and password to the input boxes and this is where I stuck. Can you please give me any advices?
If you want to look at page: https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin
Thanks for your advices.
Here how I used the code(Edit: Added eventhandler but this is my first time using so it promts me "object reference not set to a instance of an object"):
private void buttonGo_Click(object sender, EventArgs e)
{
try
{
string input = "https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin";
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
webBrowser1.Navigate(input);
HtmlDocument doc = webBrowser1.Document;
HtmlElement userName = doc.GetElementById("UserID");
HtmlElement pass = doc.GetElementById("PIN");
HtmlElement submit = doc.GetElementById("Login");
userName.SetAttribute("value", textID.Text);
pass.SetAttribute("value", textPASS.Text);
submit.InvokeMember("Click");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webBrowser = sender as WebBrowser;
webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
MessageBox.Show(webBrowser.Url.ToString());
}
}
}
Finally I solved problem I cheated a little but managed to solve. Here is the working code:
private void buttonGo_Click(object sender, EventArgs e)
{
try
{
string input = "https://suis.sabanciuniv.edu/prod/twbkwbis.P_SabanciLogin";
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
webBrowser1.Navigate(input);
HtmlDocument doc = webBrowser1.Document;
//HtmlElement userName = doc.GetElementById("UserID"); These not worked because ID of the elements were hidden so they are here to show which of these did not work.
//HtmlElement pass = doc.GetElementById("password");
HtmlElement submit = webBrowser1.Document.Forms[0].Document.All["PIN"].Parent.Parent.Parent.NextSibling.FirstChild;
//userName.SetAttribute("value", textID.Text);
//pass.SetAttribute("value", textPASS.Text);
webBrowser1.Document.Forms[0].All["UserID"].SetAttribute("value", textID.Text);
webBrowser1.Document.Forms[0].All["PIN"].FirstChild.SetAttribute("value", textPASS.Text);
submit.InvokeMember("Click");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var webBrowser = sender as WebBrowser;
webBrowser.DocumentCompleted -= WebBrowser_DocumentCompleted;
MessageBox.Show(webBrowser.Url.ToString());
}
You need to find the input boxes of the username and password fields as ID's or nodes first. Then assign them as such:
HtmlDocument doc = webBrowser1.Document;
HtmlElement email = doc.GetElementById("email");
HtmlElement pass = doc.GetElementById("pass");
HtmlElement submit = doc.GetElementById("LoginButton");
email.SetAttribute("value", "InsertYourEmailHere");
//Same for password
submit.InvokeMember("Click");
I have created 5 text boxes in a button click event and i have to get the values in the text boxes when the dynamically generated button is clicked.
protected void Button1_Click(object sender, EventArgs e)
{
for(int i=0;i<5;i++)
{
HtmlGenericControl tr = new HtmlGenericControl("tr");
HtmlGenericControl td = new HtmlGenericControl("td");
HtmlGenericControl tdbtn = new HtmlGenericControl("td");
TextBox txt=new TextBox();
txt.ID="txt_"+i.ToString();
td.Controls.Add(txt);
Button btn=new Button();
btn.ID="btn_"+i.ToString();
btn.Click+=new EventHandler(btnpay_Click);
btn.Text="Pay";
tdbtn.Controls.Add(btn);
tr.Controls.Add(td);
tr.Controls.Add(tdbtn);
PlaceHolder1.Controls.Add(tr);
}
}
But i couldn't get the Values in the text boxes at btnpay_Click
protected void btnpay_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn = sender as Button;
string[] splitvaues = btn.ID.Split('_');
string identity = splitvaues[1];
TextBox txt = new TextBox();
txt =PlaceHolder1.FindControl("txt_" + identity) as TextBox;
}
Can Anybody tell me a way to solve this problem?
Your problem is that FindControl doesn't recurse down the control tree. It only searches the controls directly in the ControlCollection of the container.
This method will find a control only if the control is directly
contained by the specified container; that is, the method does not
search throughout a hierarchy of controls within controls.
You need to write a recursive FindControl. Something like:
public static Control FindControlRecursive(this Control control, string id)
{
if (control == null || control.ID == id) return control;
foreach (var c in control.Controls)
{
var found = c.FindControlRecursive(id);
if (found != null) return found;
}
return null;
}
try this code.....
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
createcontrol();
}
}
private void createcontrol()
{
for (int i = 0; i < 5; i++)
{
HtmlGenericControl tr = new HtmlGenericControl("tr");
HtmlGenericControl td = new HtmlGenericControl("td");
HtmlGenericControl tdbtn = new HtmlGenericControl("td");
TextBox txt = new TextBox();
txt.ID = "txt_" + i.ToString();
td.Controls.Add(txt);
Button btn = new Button();
btn.ID = "btn_" + i.ToString();
btn.Click += new EventHandler(btnpay_Click);
btn.Text = "Pay";
tdbtn.Controls.Add(btn);
tr.Controls.Add(td);
tr.Controls.Add(tdbtn);
plh1.Controls.Add(tr);
}
}
protected void btnpay_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn = sender as Button;
string[] splitvaues = btn.ID.Split('_');
string identity = splitvaues[1].ToString();
TextBox txt = new TextBox();
txt = plh1.FindControl("txt_" + identity) as TextBox;
string q = txt.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
createcontrol();
}