i'm a newbie in C#. My boss has asked me to get co-ordinates of cursor when it's over some button outside the form (e.g on Desktop or any other random Window, like below).
I will then have to access the button properties for some computations (e.g width and height of button or it's name). I'm not sure if it's possible, my boss says it is as he's done something like that in the past. Gave me a hint that it can be done by knowing if the element is clickable and that there's an API for it. Thanks in advance!
Code Below
public partial class Form1 : Form
{
public string label2Y;
public string label1X;
public Form1()
{
InitializeComponent();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = Cursor.Position.X.ToString();
label2.Text = Cursor.Position.Y.ToString();
}
private void Form1_Click(object sender, EventArgs e)
{
label3.Text = Cursor.Position.X.ToString();
label4.Text = Cursor.Position.Y.ToString();
}
}
Related
So here is my setup so you can better understand what I am trying to do. I have the following:
Windows Form 1 (Toolbox) with a textbox for the Google Search and a Search Click button that loads the secondary Windows Form (Search Engine). I am wanting to do the following.
Type the search criteria in my text box and clicking on the Search button which will open the secondary Windows Form (Search Engine) which will load the google search on the (Search Engine) form using "WebBrowser1"
Any assistance would be greatly appreciated. If you can provide code instead of where to go that would be best :)
Updated Code: Which opens the Google Search Engine but does not display on the Webbrowser1. Any additional assistance would be appreciated
Form 1 Search button click Code:
namespace Plumchoice_Toolbox
{
public partial class plumchoice_Form1 : Form
{
public plumchoice_Form1()
{
InitializeComponent();
}
private void plumchoice_Form1_Load(object sender, EventArgs e)
{
}
//Search Engine Form 1 added
//// Search Button that opens the Search Engine
private void button18_Click(object sender, EventArgs e)
{
Google_Search.SearchEngineForm1 frm2 = new Google_Search.SearchEngineForm1();
frm2.searchAddress = "http://www.google.com/webhp?hl=en&tab=ww#hl=en&tbo=d&output=search&sclient=psy-ab&q=" + textBox3.Text.Replace(" ", "+");
frm2.Show();
}
Webbrowser1 Code
namespace Google_Search
{
public partial class SearchEngineForm1 : Form
{
public SearchEngineForm1()
{
InitializeComponent();
}
public string searchAddress;
private void PlumChoiceToolboxForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(searchAddress);
}
You can do this: In form1 search button click
This code is for Form1 that has a search text box and a button for start searching. I think (but not sure) it is same as your Toolbox form.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Search_Click(object sender, EventArgs e)//When user clicks on search button
{
SearchEngineForm1 frm2 = new SearchEngineForm1();
frm2.searchAddress = "https://www.google.com/search?q=" + txtSearch.Text.Replace(" ", "+");
frm2.Show();
}
}
And below is the Form2 or SearchEngineForm1 that will open after search button click.
public partial class SearchEngineForm1 : Form
{
public SearchEngineForm1()
{
InitializeComponent();
}
public string searchAddress;
private void SearchEngineForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url= new Uri(searchAddress);
}
}
In your code you have a mistake:
private void PlumChoiceToolboxForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(searchAddress);
}
What is this void? This will never fire. You need to double click on SearchEngineForm1 form (a blank space on form) to add this void to your code:
private void SearchEngineForm1_Load(object sender, EventArgs e)
{
}
Do not forget double click i said. Pasting this only does not help you!
then enter the webBrowser1.Url= new Uri(searchAddress); between its braces {} to have this exactly:
private void SearchEngineForm1_Load(object sender, EventArgs e)
{
webBrowser1.Url= new Uri(searchAddress);
}
Let's say I have 2 movable forms and when I click a button in one form (the fist one which has it's start position CenterScreen) it brings me to another one (but the second one has still centerscreen start position) and let's say I move the first form before I press the button and when I click the button I want to position the new form where I moved my first form... how can I do this?
use this following code,
set StartupPosition = Manual then the Left and Top values (Location) set via the this code
Code for the Form1
using System;
using System.Windows.Forms;
namespace PositioningCs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void myButton_Click(object sender, EventArgs e)
{
Form2 frm2=new Form2();
frm2.setLocation(this.Top,this.Left);
frm2.show();
}
}
}
Code for the Form2
using System;
using System.Windows.Forms;
namespace PositioningCs
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private int top_val=0;
private int left_val=0;
public void setLocation(int top_val,int left_val)
{
this.top_val=top_val;
this.left_val=left_val;
}
private void Form2_Load(object sender, EventArgs e)
{
this.Top = top_val;
this.Left = left_val;
}
}
}
Remember the position of a form after it's moved and re-use it later.
private static Point location = null;
private void MyForm_Move(object sender, EventArgs e)
{
location = this.Location;
}
private void MyForm_Load(object sender, EventArgs e)
{
if (location != null) { this.Location = location; }
}
you can relate position with position of parent form...
//if "this" is Form1, in the button event to call Form2...
...
Form2.Position = this.Position;
Form2.ShowDialog(this);
in this link you can find more details: Link
i hope this help you
I have multiple forms and i can make them all go to a certain form through the click of one button, but how do I give one button the ability to go back to the last page the user was on, and not just the form before the current one? This is for my 'help' form, as users can visit the form through many other forms. My coding was as follows. I am using Winforms
namespace Spanish_Quiz
{
public partial class Help : Form
{
public Help()
{
InitializeComponent();
CenterToScreen();
}
private void btnBackToStart_Click(object sender, EventArgs e)
{
Homescreen Homescreen = new Homescreen();
Homescreen.Show();
Homescreen.Activate();
this.Hide();
}
private void btnBack_Click(object sender, EventArgs e)
{
/*This is where my problem is,
what code do I write here that could be similar to
my 'back to start' button's code?*/
}
}
}
I am trying to create a form that can keep track of a users vote. I have two buttons that a user can select to vote for which selection they like best. I want to have something in the middle that inputs the vote automatically when a selection is made.I'm not sure what tool would work best and what the code should be like?
So far I just have the messages for the votes, but I haven't been able to find what would work best to tally them when the button is selected:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Eyes2_Click(object sender, EventArgs e)
{
MessageBox.Show("Thank you for your vote!");
}
private void Eyes1_Click(object sender, EventArgs e)
{
MessageBox.Show("Thank you for your vote!");
}
}
Sounds like you need two counters (yes/no?), and some kind of control to display the current tallies?
How about starting simple with a Label control?
Label controls can also be used to add descriptive text to a Form to
provide the user with helpful information.
Something like:
public partial class Form1 : Form
{
private int Yes_Tally = 0;
private int No_Tally = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
UpdateTally();
}
private void Eyes1_Click(object sender, EventArgs e)
{
Yes_Tally++;
UpdateTally();
}
private void Eyes2_Click(object sender, EventArgs e)
{
No_Tally++;
UpdateTally();
}
private void UpdateTally()
{
lblTally.Text = String.Format("Yes: {0}, No: {1}", Yes_Tally, No_Tally);
}
}
Declare a counter, increase that counter each time the button is clicked.
i have a really simple counter application, I made in C#.
Now what i want to know is it possible to clone the form design and code, so there are 2 counter's instead of one. with a button.
they both have to be working.
i'm an beginner.. so that's why i ask if this is possible.
So from this (this is what i currently have, without clone button):
http://i.stack.imgur.com/ASMY4.jpg
to this:
http://i.stack.imgur.com/acluZ.jpg
this is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void counteradd()
{
int current1 = Convert.ToInt32(totaltb.Text);
current1++;
totaltb.Text = Convert.ToString(current1);
}
public void counterreduce()
{
int current2 = Convert.ToInt32(totaltb.Text);
current2--;
totaltb.Text = Convert.ToString(current2);
}
public void counterreset()
{
totaltb.Text = ("0");
}
private void reducebttn_Click(object sender, EventArgs e)
{
counterreduce();
}
private void resetbttn_Click(object sender, EventArgs e)
{
counterreset();
}
private void addbttn_Click(object sender, EventArgs e)
{
counteradd();
}
}
Simply duplicating the controls and laying them out on the form will result in messy code. The "clone' that you are referring to would be to build the functional piece as a user-control, and then add that as a control to your form.