New to C# and learning as i go. I've tried Googling, but havent found what im looking for (still looking).
I have a simple C# App that so far is working fine. The next part i want to figure out is how to invoke a series of tasks/methods.
Say i have 4 individual tasks (Tasks 1-4). Each one of them does something different...
Button 1 - Shows a Messagebox
Button 2 - Changes a label text color
Button 3 - Writes something to console.
etc
etc.
Each one of these works fine when you press its associated button. But now lets say i have a Checkbox next to each of these that you can check/uncheck. Plus a button that says "run selected".
My question is how do i program that button to run ALL the tasks in sequential order AND only run the tasks that are selected?
Checkbox 1 - OFF
Checkbox 2 - ON
Checkbox 3 - OFF
Checkbox 4 - ON
So when i press the "Run Selected" Button - Only Tasks 2 and 4 run.
Again, im new, and my lingo may not be correct - but hopefully you get the idea.
Thanks,
Mike
First of all, I would isolate each task outside the click handlers of the buttons, into its own methods:
private void Task1()
{
MessageBox.Show("Task 1");
}
private void Task2()
{
MessageBox.Show("Task 2");
}
private void Task3()
{
MessageBox.Show("Task 3");
}
private void Task4()
{
MessageBox.Show("Task 4");
}
I just used simple message boxes for the sake of simplicity, but any code will work actually.
Then make each click handler just call each method:
private void button1_Click(object sender, EventArgs e)
{
this.Task1();
}
private void button2_Click(object sender, EventArgs e)
{
this.Task2();
}
private void button3_Click(object sender, EventArgs e)
{
this.Task3();
}
private void button4_Click(object sender, EventArgs e)
{
this.Task4();
}
Now the important part, when the "do all checked" button is clicked, you check each checkbox state and launch the appropriate method in sequence:
private void button5_Click(object sender, EventArgs e)
{
if (this.checkBox1.Checked) this.Task1();
if (this.checkBox2.Checked) this.Task2();
if (this.checkBox3.Checked) this.Task3();
if (this.checkBox4.Checked) this.Task4();
}
Related
If you ever remove focus from any professional application like Chrome/FireFox/Visual Studio, and then reclick a button/menu item, it will actually click it as if you never lost focus.
How can I apply the same concept in C# WinForm? I tried many things like
private void form1_MouseClick(object sender, MouseEventArgs e)
{
BringToFront();
Activate();
}
Activate/focus/select/etc... nothing worked to react the same way, it always takes 3-4 clicks to actually click on a menu!
I thought about making a click event for every single control, but that seemed rather redundant.
Check this for example (Yellow Clicks)
You are right about Menues taking an extra click to get focus.
Which is extra annoying since the menue get highlighted anyway but doesn't react to the 1st click..
You can avoid that by coding the MouseEnter event:
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
// either
menuStrip1.Focus();
// or
this.Focus();
}
The downside of this is, that it is stealing focus from other applications, which is not something a well-behaved application should do..
So I think it is better to wait for a definitive user action; code the MouseDown event in a similar way..:
private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
{
menuStrip1.Focus();
}
Or use the event that was made for the occasion:
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
menuStrip1.Focus();
}
I can't confirm a similar problem with Buttons or any other controls, though.
I have find trick to solve your problem. it work for me 100%
See this code:
dynamic elem1;
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
elem1 = sender;
}
private void menuStrip1_MouseLeave(object sender, EventArgs e)
{
elem1 = null;
}
private void Form1_Activated(object sender, EventArgs e)
{
if(elem1 != null){
elem1.PerformClick();
if (elem1.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem") elem1.ShowDropDown();
}
elem1 = null;
}
Here what happend.
When mouse enter button/menu item elem1 = this button/menu, and when mouse leave it set back to null.
so when form Activated we can call elem1.PerformClick() to click the button/menu item.
Hi im practically new to C# i usually program with c++ so please bear with me. I am coding an application and i have 10 buttons, 5 of which which are practically doing the same thing. Currently I have 5 event handlers doing the same thing. How can I change this to a single even handler with if statements. Also my problem is that even though the methods of each button are the same i have some small differences from one to another as described below:
button 1 copared with button 5
button 2 copmpared with button 6
button 3 compared with button 7 and so on
How can i tackle this small difference in each case?
Thankyou so much
button.SomeEvent += SomeHandler
void SomeHandler(object sender, EventArgs e)
{
Button b = (Button)sender; //get the specific button that was pressed
...
}
Use the += operator to add a method to an event and simply add the same method.
You could set them all to the same handler, get the button that called it, check the id in an if statement and do different actions -- OR -- you could use the much more common refactor that looks like this:
void Button1Action(object sender, EventArgs e)
{
// do stuff for button 1 here
SharedCode();
}
void Button2Action(object sender, EventArgs e)
{
// do stuff for button 2 here
SharedCode();
}
void Button3Action(object sender, EventArgs e)
{
// do stuff for button 3 here
SharedCode();
}
void Button4Action(object sender, EventArgs e)
{
// do stuff for button 4 here
SharedCode();
}
void SharedCode()
{
// do stuff for all buttons
}
I have a tabbed form with a StatusStrip at the bottom, which includes a StatusLabel. I want to use this status label for various actions ("1 record updated" etc). It is simple enough to create specific events to set the label's text property.
But how best to reset the status to blank? The user could perform any number of other operations where the status is no longer meaningful (going to another tab, clicking other buttons etc.).
It is not feasible to create all the possible events to reset the status message. Is there a way to incorporate some type of timer so that the message fades out after several seconds? Has anyone else found a good solution for this?
Is it truly important to clear the status though? There are plenty of products which will keep their status label unchanged until the next status event occurs. Visual Studio is a good example of this. It may be worth simplifying your scenario and taking this approach.
If you do want to clear the status after an event I think the most maintainable way to do this is with a Timer. Essentially clear after a few seconds when the status is set
Timer m_timer;
void SetStatus(string text) {
m_statusLabel.Text = text;
m_timer.Reset();
}
void OnTimerTick(object sender, EventArgs e) {
m_statusLabel.Text = "";
m_timer.Stop();
}
Yes a timer would work for this to clear it. Here is an example of one I've knocked together.
public partial class Form1 : Form
{
private System.Timers.Timer _systemTimer = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
_systemTimer = new System.Timers.Timer(500);
_systemTimer.Elapsed += _systemTimer_Elapsed;
}
void _systemTimer_Elapsed(object sender, ElapsedEventArgs e)
{
toolStripStatusLabel1.Text = string.Empty;
_systemTimer.Stop(); // stop it if you don't want it repeating
}
private void button1_Click(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "random text just as an example";
}
private void button2_Click(object sender, EventArgs e)
{
_systemTimer.Start();
}
}
Assume button1 is your action to update the status, and button2 is just a random way to start the timer (this can be however you want to start it, I've only used another button click as an example). After the set amount of time passes the status label will be cleared.
I am currently using a simple button to open a webpage.
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca");
}
What I want to do is get it to open 3 pages at once with the one click and I am having a hard time getting it to work. I have tried multiple Process.start lines
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca");
System.Diagnostics.Process.Start("http://www.gmail.com");
System.Diagnostics.Process.Start("http://www.stackoverflow.com");
}
and even adding multiple pages into the handler.
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca","http://www.gmail.com","http://www.s tackoverflow.com")
}
It will only open the last page in the list in both cases. Any ideas?
If IE is open, your code works fine and opens each link in a new tab, if not, I was able to make it work by making the app wait for 1 sec before calling the second page to open:
void ReportingClick(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://www.google.ca");
System.Threading.Thread.Sleep(1000);
System.Diagnostics.Process.Start("http://www.gmail.com");
System.Threading.Thread.Sleep(1000);
System.Diagnostics.Process.Start("http://www.stackoverflow.com");
}
I have a strange bug, please, let me know if you have any clues about the reason.
I have a Timer (System.Windows.Forms.Timer) on my main form, which fires some updates, which also eventually update the main form UI. Then I have an editor, which is opened from the main form using the ShowDialog() method. On this editor I have a PropertyGrid (System.Windows.Forms.PropertyGrid).
I am unable to reproduce it everytime, but pretty often, when I use dropdowns on that property grid in editor it gets stuck, that is OK/Cancel buttons don't close the form, property grid becomes not usable, Close button in the form header doesn't work.
There are no exceptions in the background, and if I break the process I see that the app is doing some calculations related to the updates I mentioned in the beginning.
What can you recommend? Any ideas are welcome.
What's happening is that the thread timer's Tick method doesn't execute on a different thread, so it's locking everything else until it's done. I made a test winforms app that had a timer and 2 buttons on it whose events did this:
private void timer1_Tick(object sender, EventArgs e)
{
Thread.Sleep(6000);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
frmShow show = new frmShow();
show.ShowDialog(); // frmShow just has some controls on it to fiddle with
}
and indeed it blocked as you described. The following solved it:
private void timer1_Tick(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(DoStuff);
}
private void DoStuff(object something)
{
Thread.Sleep(6000);
}