generate a mouse click inside my form application - c#

I am making a program that is designed to send mouse clicks to locations (like an auto clicker but more advanced). I need this to be triggered by a click event. Is it possible to make the click only apply inside a the form along the same principle of a runescape bot? I wish to use the same principle but for a different program. Here is the form I wish for it to apply to the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace web_clicker
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void pictureBox3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

I don't really understand your problem. But as I understand you try to get the number of clicks in something on form ?
If yes so that's what I should do :
// Definition counter click
private int counter;
// In the C'tor
counter =0;
// In event click on button or picture or everything else
counter++;
And now you have the number of clicking.
If you don't mean to this answer me I'll try to help you.

Related

Changing the background image of a form when a progress bar expires

I am trying to make a program that once a "fake" progress bar expires, the background image of the form changes to another one. This is my first time using forms as a GUI, so I would really appreciate the help. If possible, please explain how the code works. I'm using Visual Studio 2017. Here's how the code looks like.
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;
namespace Form_Application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.progressBar1.Increment(1);
}
}
}
in this event you have to check if the progressbar value is 100
it'll be like :
private void timer1_Tick(object sender, EventArgs e)
{
if(progressBar1.Value<100) // set progressBar max value to 100
{
// if the value smaller than 100, will increment.
this.progressBar1.Increment(1);
}
else{
timer1.Stop(); // Important to stop the timer
// here you change the background image of the form.
this.BackgroundImage = // choose ur image location.
}
}

Check if all elements on a form is fully loaded C#

I am developing a C# application using Visual Studio 2015
it has 2 forms, on form1 I have a button that when clicked
shows form2, now what I would like to do is print form2
after it has fully loaded, I am using the printform control
on form2 to do this, if I use this on the the form_load event
it prints a blank page and then shows the form, I have
also tried using it on form_Shown, however this prints a box
where the elements are but not the element itself as if they have
not finished loading, there is probably a better way to do this
but I am new to C# so still learning
Below is an example of the code that I have on form2
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;
namespace MyApp
{
public partial class Form2: Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.Shown += new System.EventHandler(this.Form2_Shown);
}
private void Form2_Shown(object sender, EventArgs e)
{
printForm.Print();
}
}
}
The Shown event fires a wee bit too early. The form's frame and background is painted but the rest follows later. Painting is a low priority task that occurs only when nothing else needs to be done, firing the Shown event happens first.
The workaround is simple, just ask the form to finish updating itself by calling its Update() method. Fix:
private void Form2_Shown(object sender, EventArgs e)
{
this.Update();
printForm.Print();
}

button in c# not firing

I am writing a simple code it has 3 buttons 2 that will need to open up other forms and one to close the program. When i start the program the exit button will not work even though i have it coded the same as any other time i have wrote a program.
When i press any of the buttons nothing happens. Im not 100% sure how to use the buttons to open another form but i know the exit button should work as is.
Here is the code:
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;
namespace _3343_ParksJ_Lab02
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void workerButton_Click(object sender, EventArgs e)
{
WorkerForm workersForum = new WorkerForm();
}
private void suppervisorButton_Click(object sender, EventArgs e)
{
SupervisorForm workersForum = new SupervisorForm();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
You have to subscribe your buttons' click events to the event methods before they'll fire correctly.
You can check the Designer.cs file to see if this has already been done, though I'm guessing it hasn't. You'll be looking for something like this:
this.workerButton.Click += new System.EventHandler(this.workerButton_Click);
One way to do so is directly in the constructor:
public MainForm()
{
InitializeComponent();
workerButton.Click += workerButton_Click;
suppervisorButton.Click += suppervisorButton_Click;
exitButton.Click += exitButton_Click;
}
Normally, I'd do this through the designer. Select each Button in turn, then open the properties panel and double-click on the event you wish to subscribe to, which will create the corresponding event method in the code-behind for you.
Look at .Designer.cs file and make sure your button is adding the correct delegate method. In your case it should exitButton_Click.
Sometimes when you change names of a button VS designer does not make the name change correctly in the .Designer file. Very rare but it happens.

Program in C# to read a file:Button not working

Here is my code, for some reason button two doesn't fire, button one does and when I place the code from button 2 into one it works there. What am i missing about the syntax to get buttons one and two to both work on click? I am about 2 weeks into learning c# so this is all new to me, I do not see why this code should not work.
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;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
string filePath = null;
public Form1()
{
InitializeComponent();
}
//Method to check database connection
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("button1.Click was raised.");
}
//Method to select a file
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK)
{
filePath = file.FileName;
}
}
}
}
I assume that the event handler isn't subscribed (anymore). So have a look at the partial class of Form1 in the auto generated file Form1.Designer.cs. There must be somewhere this:
this.button1.Click += new System.EventHandler(this.button1_Click);
// is this missing?
this.button2.Click += new System.EventHandler(this.button2_Click);
How to: Subscribe to and Unsubscribe from Events (C# Programming Guide)
Make sure button2 is bound.
From the designer, select the button then go to the properties window. Click the lightning bolt, and make sure the click event is bound to button2_Click.
Alternative method is right-clicking on InitializeComponent() and select "Go to definition" (brings you to Form1.designer.cs) and look for the following:
button2.OnClick += new EventHandler(button2_Click);
If you've confirmed it's bound, we'll need to see more than what you've shown to determine the problem.

How to create objects dynamically with arbitrary names?

So here's my situation. I am designing a program that requires new Image objects to be created in real-time on a Canvas, when the user clicks a certain Button. Seeing as I don't know exactly how many of said Images any given user will create, I can't assign names in the code for each and every one of them. The Images need to be named "Image0", "Image1", "Image2", etc, depending on how many Images already exist on the page. Kinda like how Visual Studio itself works, where each time you drop a control onto the design view, it automatically appends a number to the name of the control.
Does anyone have a code snippet capable of performing this function?
You don't have to explicitly name all controls. Just add them to the canvas "controls" list.
Create a windows app with a single button and a flowLayoutPanel.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
int i = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var button = new Button { Text = string.Format("Button {0}", i) };
button.Click += new EventHandler(button_Click);
flowLayoutPanel1.Controls.Add(button);
i += 1;
}
void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
button.Text = " clicked!";
}
}
}
I don't have a direct answer to your question, but from the sounds of it, and I could be wrong because I don't know what you're trying to do, you're viewing the problem from a newbie programmer perspective. You might want to consider using a collection of some kind (perhaps a list) and dynamically adding image objects to that. If you want a string name associated with each object, you could consider a dictionary of type Dictionary<string,Image>

Categories