How to apply multiple events on a single button click - c#

So i am using Windows Forms App on visual studio c#. I am developing a game. For that i have given an "INSTRUCTIONS" Form. In that form i want to give the instructions. The game has 4 modes so for each mode i want to give separate instructions. I have used groupBox for providing instructions for each mode. Which means that in the groupBox i have added textBox for instruction. Now for 4 modes i am using 4 groupBox.
Now the question is that how to open groupBox2 then groupBox3 then groupBox4 by clicking on same "NEXT" Button?? Like when the form loads the groupBox1 loads which is for first mode. Now when the user clicks on NEXT button, the groupBox1 gets invisible and the groupBox2 becomes visible which is for second mode. Then again when user clicks on same Next button the groupBox2 gets invisible and groupBox 3 becomes visible.
In the same way for "PREVIOUS" button which will make groupBox3 invisible and groupBox2 visible.

There are a couple reasonable approaches to this.
One would be to keep a variable int step (or similar) in the class data, and inside the button's unified click handler do switch (step).
Another would be to have separate click handlers, and in the first click handler, do NextButton.Click -= Step1NextClicked; NextButton.Click += Step2NextClicked; and similarly in the others.
A variation on the second approach would be for the button click handler to not have any logic of its own, but just call an Action which points to a separate handler function at each stage. Then you can update the Action in a single assignment when changing stages, instead of having to remove the old handler and add the new one.
Yet another approach would be to swap not just the groupbox, but a borderless panel holding both the groupbox and the button. Then you actually have multiple Next buttons that display one at a time in the exact same screen location.
Personally I would use the first option, since it's very efficient. If I had more than a handful of different behaviors, I would probably use the Action approach.

Related

Why is the tab page body not updating with a .NET tab control?

I am having a strange problem with the .NET TabControl in C# (Visual Studio 2010). Start a Windows Forms Application. Add a tab control and a button. Add two different labels to the two tab pages so you can differentiate them. The purpose of the button is just to act as a next button; subscribe to the its Click event with the code:
tabControl1.SelectTab(1);
Let's assume the user entered something wrong on the first tab, so when they try to go to the second tab we want to send them back, so subscribe to the tab control's SelectedIndexChanged event with the code:
if(tabControl1.SelectedIndex == 1)
{
tabControl1.SelectTab(0);
}
Now run the program and click the button. You will notice that as judged by the highlighted tab at the top, the first tab page is the one that appears to be selected, as you'd expect. However, as judged by the tab page that actually appears in the body of the tab control, it's still the second tab page that shows up! Calls to various controls' Focus(), Update(), and Refresh() functions don't seem to help. What is going on here?
I repro. This is a generic problem with event handlers, you can confuse the stuffing out the native Windows control by jerking the floor mat like that. TreeView is another control that's very prone to this kind of trouble.
There's an elegant and general solution for a problem like this, you can use Control.BeginInvoke() to delay the command. It will execute later after the native control is done with the event generation and all side-effects have been completed. Which solves this problem as well, like this:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
if (tabControl1.SelectedIndex == 1) {
this.BeginInvoke(new Action(() => tabControl1.SelectTab(0)));
}
}

Load Multiple UpdatePanel Controls With JavaScript Button Clicks

I have 3 update panels on a page. Each of them is set to conditionally update. In each update panel I have a single button control that when clicked will populate the panel with data. Each panel is independent of one another.
Everything is working fine when you manually click each one of the 3 buttons in their respective panel. The issue I am having comes up when I use JavaScript to click the buttons. I have success when executing a single JavaScript call but when I try to click all 3 buttons with JavaScript, things behave sporadically. Depending on where I place the JavaScript calls in my code, different panels work and the others do not.
For example:
__doPostBack('btnBindTeamTicketList', '');
__doPostBack('btnBindPriorityList', '');
__doPostBack('btnSearchTemplate', '');
will show the panel that holds the 'btnSearchTemplate' button, but not the others. Different results occur when I change the sequence around.
Is there a way to click my 3 independent buttons (each in its respective panel) using JavaScript so that each panel will load as if I were clicking on them individually? I am guessing this has to do with the AJAX somehow conflicting with each other or a timing problem. Anyone point me in the right direction?
I am using ASP.NET 3.5/C#
I think you should do one postback. Put a hidden update panel in your page and put a button inside this new update panel. This button should call events
btnBindTeamTicketList_Click(null, null)
btnBindPriorityList_Click(null, null)
btnSearchTemplate_Click(null, null)
and should update the other update panels like upTeamTicketList.Update(). Now try clicking this new button.

Remove original event behaviour of WinForm Control

I would like to remove the original event behavior of controls within a form (similar to design mode).
So, when the user clicks on the button, i only want to capture that event. I do not want the original button event to be fired. Is this somehow possible?
I am looking for a generic solution. So it should work with any form and any control within the form.
Reason: I wrote a form validation rules designer. It uses reflection to enumerate all form-types in the entry assembly. The user can then select a form type, the designer creates that form, enumerates the controls, and embedds the form in the designer panel.
clicking on a control, opens a formular designer panel, and the user can now create a formular for that control and saves the formular to a DB.
When the form is then opened in the normal "runtime" mode, it loads its validation formulars.
Events are not in fact disabled in the Winforms designer. The designer executes the constructor of the form through Reflection, everything in the InitializeComponent() method executes, including the event subscriptions. Wherever this might cause a problem, the controls check the DesignMode property (prevents a Timer from starting for example) or by custom designers. The form is displayed underneath a transparent layered window on top of which the selection rectangle and drag handles are painted. Which prevents issues with mouse clicks and keyboard focus.
You probably ought to look at this magazine article to get this working for you.
From what I understand from your question, I guess, you can still use the "DesignMode" property for this as well. In your event handling routine, you may want to bypass execution by checking on this property:
if (this.DesignMode) return;
as the first statement in your event handling block of code.

C# - TabPage Color event

C# certainly isn't my strong suit so I appreciate all the generous folk sharing their knowledge. I'm working with a Windows Form and I've read up on events and have found some excellent help on how to modify a TabControl so I can have an OnDraw event that will add some coloring to the tabs.
The color of each tab is based upon the state of a connection variable:
Current (green)
Lost (red)
Stale (yellow)
The OnDraw event works excellent for updating the color of each tab, but that only occurs when the user selects a different tab to view.
What I would like to happen is for the color of each tab to be updated whenever the connection state changes. For example, let's say Tab#1 is colored green, but then the connection state changes to stale so now the tab needs to be colored yellow but it won't get colored like that until the user clicks on a different tab and the OnDraw event is triggered.
So I'm trying to figure out how to do that. When the OnDraw event is triggered normally (by the user clicking on a different tab) a "DrawItemEventArgs" parameter is passed into the even handler. That variable is already populated with the pertinent data needed to figure out which tab was clicked on, the boundaries of that tab and etc. So I am unsure where it came from or how I can programmatically re-create such a call to re-color the tabs whenever the connection variable changes.
Please let me know if I need to clarify anything!
Thank you.
You can call Invalidate() on the control to force a repaint.
If you have an event fired when your connection state changes you could do an
InvalidateVisual()
on all of your tabs from within that event.
If you want to have a constant refresh going, then you probably need to create System.Timers.Timer object.
Once you create a Timer and set the timer tick value to whatever interval you need (in milliseconds) it will fire the OnTimerTick event at regular intervals. From this event you can trigger a call to your OnDraw method through the Invalidate() method. Invalidate tells the system that your screen needs to be refreshed and it will call OnDraw and OnPaint at the next available opportunity.

Windows Forms: Multiple default buttons?

In a simple Guess-The-Number game I'm making, the user inputs a couple of numbers and hits a button. A second panel becomes enabled and the original one is disabled. The user now enters a number and hits another button, multiple times. Since the two panels will never be enabled at the same time, I'd like for both buttons to be "default", i.e., they will be pushed if Enter is pressed. Is this possible? Apparently, I can only set one of these per window.
A window in Windows by definition has only one default button. This is a functionality intended for dialog windows where you would never disable the default button.
What you can do instead is to switch the default button when you disable one panel.
Another option would be to throw out default buttons altogether and use KeyPreview on the form and handle the enter key yourself, sending it to the appropriate button that is currently active.
Nope, no way to accomplish that that I know of. You'll have to switch the default button when one button is pressed to the next button.
You can only have one default button per window. However, this can be changed at runtime, so when you activate a specific panel, you can make it's button the default one at that point.

Categories

Resources