I am a beginner in C# and WPF and I am building this project in which I have to trigger when the mouse is moved. Under some conditions, I have to use it as a background worker. I want to call the mouse_Moved method in the background, but I don't know how to actually do that . Can anyone help me please? This is my code so far:
public MainWindow()
{
InitializeComponent();
mouse = new MouseInput();
mouse.MouseMoved += mouse_MouseMoved;
}
void mouse_MouseMoved(object sender, EventArgs e)
{
//The code that I need
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
//where I want to call the mouse_Moved method
}
Create a method and call it from both:
void mouse_MouseMoved(object sender, EventArgs e)
{
DoMouseMovedWork();
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
DoMouseMovedWork();
}
private DoMouseMovedWork()
{
//The code I need
}
Related
In my C# form I have two buttons
button1.Hide()
private void button2_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
The button1 is hidden at form loading, I want the logic behind button1 to be perfomed when it's hidden too.
Just let the function outside become another function, then you can call function although you hidden the button1.
private void button1(object sender, EventArgs e)
{
_button1();
}
private void button2(object sender, EventArgs e)
{
_button1();
}
//Here is the function
void _button1()
{
...
}
If your Button is hidden, it seems that you need the functionality behind not or just in special cases. Keeping functionality out of events is often a simple solution to avoid problems in the future.
private void btn_Reload_Click(object sender, EventArgs e)
{
// reload here - maybe you reload all your employees from a datasource
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
// you can use functionality here from a another button and call the
btn_Reload_Click(this, EventArgs.Empty); // DON'T DO THIS IN MY OPINION
// ....
}
Maybe this solution is better even if you need the functionality at other workflows.
private void btn_Reload_Click(object sender, EventArgs e)
{
Reload();
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
Reload();
Calculate();
}
void Reload() { }
void Calculate() { }
I have a form with multiple instances of a user control on it.
I've assigned the following:
Switch.armySwitchCloseButton.Click += armySwitchClose;
So when one of those instances is pressed, I call the following method:
void armySwitchClose(object sender, EventArgs e)
The above method has a bunch of additional code in it which isn't required for here.
Now what I need to do is from another button, call this above function from every instance.
How can I do this?
Many Thanks
In constructor of each user control you can pass the same instance of some object who knows how to run this method
void armySwitchClose(object sender, EventArgs e)
Then, you call this method inside each event method, for ex:
Public Class UserControl(){
private MakeEvent makeEvent;
Public MyClass(MakeEvent makeEvent)
{
this.makeEvent = makeEvent;
Switch.armySwitchCloseButton.Click += armySwitchClose;
}
void armySwitchClose(object sender, EventArgs e)
{
makeEvent.armySwitchClose(sender,e);
}
}
Public Class MakeEvent() {
void armySwitchClose(object sender, EventArgs e)
{
//the real implementation
}
}
Hope this solve your problem.
Oh, I think I get your problem wrong... When you click in one button all the other events in others UC must be triggered, right? This problem can be solved with the Observer Pattern
Public Interface IObserver
{
void armySwitchClose(object sender, EventArgs e);
}
Public UserControl1: Observer
{
void armySwitchClose(object sender, EventArgs e)
{
//implementation UC1
}
}
Public UserControl2: Observer
{
void armySwitchClose(object sender, EventArgs e)
{
//implementation UC2
}
}
In button UC:
Public UserControlButton
{
private List<IObserver> observers;
public void addObserver(IObserver observer)
{
observers.Add(observer);
}
public void button_clickedEvent(object sender, EventArgs e)
{
foreach(IObserver observer in observers)
{
observer.armySwitchClose(object sender, EventArgs e);
}
}
}
In form with all buttons you call addObserver adding each user control.
In C# , Winform, I have created a form and bunch of UI controls on it. I have changed the name of the controls through Properties windows but the following automated generated code did not update automatically. However, the InitializeComponent code is automatically updated though. My problem is now that I don't remember which box or whihc label I renamed to certain name.. Two questions : How could I have done this more efficiently to begin with? Question 2) Is there anything I could do now to make it automatically change the corresponding names? I have heard of refactoring but I don't know if I could have used it here and how? I appreciate any help.
public partial class frmMyInterface : Form
{
public frmMyInterface()
{
InitializeComponent();
}
private void frmMyInterface_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Rename each these event handlers and then on the property window, reassign the events selecting from the dropdrown. Or delete these event handlers and double click on each event in the property window and this time it will update it for you
I am trying to figure out how to make it that when my timer ticks, it performs a bidder00_TextChanged, or something like that.
Is this even possible to do? and if it isn't, is there any other way to do it?
I tried to search Google for it but i didn't get any results, if you find anything that i missed please post it here.
I don't really have any code but here it is:
private void bidder00_TextChanged(object sender, EventArgs e)
{
if (bidder00.Text == addbidder1.Text)
{
bidBtn1.PerformClick();
}
}
That is my TextChanged Event
My timer doesn't have any code because it is going to perform the bidder00_TextChanged Event.
You could create a method Perform() and call it from within your event handlers :
private void timer1_Tick(object sender, EventArgs e)
{
Perform();
}
private void bidder00_TextChanged(object sender, EventArgs e)
{
Perform();
}
private void Perform()
{
if (bidder00.Text == addbidder1.Text)
{
bidBtn1.PerformClick();
}
}
I assume you have coupled your actual logic with your click event which is not a good idea. Separate the code out into a separate function and have both parts of the application call the same code e.g.
private void SubmitBid()
{
// code you want to execute
}
private void OnSubmitBid()
{
// confirm whether we can actually submit the bid
if (bidder00.Text == addbidder1.Text)
{
SubmitBid();
}
}
private void Timer1_OnTick(object sender, EventArgs e)
{
// trigger code from timer
OnSubmitBid();
}
private void bidder00_TextChanged(object sender, EventArgs e)
{
// trigger code from text change
OnSubmitBid();
}
private void btnBid_Click(object sender, EventArgs e)
{
// trigger code from button press
OnSubmitBid();
}
Notice all the UI controls trigger the same code. There is an extra call in there for the text control validation (i.e. OnSubmitBid()) - if this wasn't required then you would just call SubmitBid directly.
Here is the short sample code:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(object sender, EventArgs e); //can I call button1 event handler?
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
I wonder if it would be okay to code in the above way?
You can do that - although the code you provide can't be compiled. It should look like this:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
button1_Click(sender, e);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(txtbox1.Text);
}
But for best practice and code readability, you're probably better off doing this, especially as you are not making use of sender and e:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
ShowMessageBox();
}
private void button1_Click(object sender, EventArgs e)
{
ShowMessageBox();
}
private void ShowMessageBox()
{
MessageBox.Show(txtbox1.Text);
}
Yes you can do that; an event handler is just another method.
However it might be worth creating a new method that shows the message box, and having both Click event handlers call that:
private void txtbox1_DoubleClick(object sender, EventArgs e)
{
ShowTextboxMessage();
}
private void button1_Click(object sender, EventArgs e)
{
ShowTextboxMessage();
}
private void ShowTextboxMessage()
{
MessageBox.Show(txtbox1.Text);
}
An event handler is nothing more than a method, so you can call it like any other.