C# User Control (with sequences). Navigate within WinForms - c#

I'm looking for the best, most effecient way of implimenting user controls navigation based on events, below is the intended use.
I also don't know what is best, to have the next back button on the control or form.
Lastly, the next button may sometimes change and initially be validate on some controls and then if validated change to next if the validate is ok.
Ok the concept:
I have 1 primary user control with 3 checkboxes, behind each checkbox is a sequence of more user forms therefore, if all three are selected it should then load all three sequences one after another.
Regardless of how many are selected, all sequences end with the same final user control.
Basically I need to understand the best and most practicle way approach this (examples welcome).
There are lots of topics that cover winforms / user control navigation but I cannot seem to see a solution which pops out to me and fits my needs.
For compatibility with some systems, I have decided to use .net 3.5; should this make a difference in suggestions.
Feedback, links, code etc all welcome :)

I ran into a similar scenario some years ago. If memory serves, I approached it as follows:
Separate the navigation buttons into their own control that groups
the buttons together. Design this control to support docking.
In this control, provide distinct buttons for Back, Next, Finish and
Cancel. Provide separate properties on the control that determine
what the user can do: CanMoveBack, CanMoveForward, CanFinish, and
CanCancel. The container should be able to set these.
In the setter for the navigation properties, you want to be able to
adjust the visibility/disabled status of each of the navigation
buttons. Don't hot-swap text. That way, each button does one thing,
and only one thing. This keeps their event handlers nice and clean.
The user control itself should raise events: OnBackClicked,
OnNextClicked, OnFinishClicked, and OnCancelClicked. Your button
event handlers should raise these. The container should decide what
to do when they are clicked.

Related

Is there another way to browse user controls in C#

I am new to C# and I am using windows forms.
I don't know if the term "browse" is right to use in this case or not.
I have Form1 with 3 buttons ( buttonA , buttonB and buttonc) in it.
I linked buttonA with a user control which contains 20 buttons and each one of those 20 buttons is linked to a user control which contains 10 buttons each of which does an action which it is clicked, also same thing applies to buttonB and buttonC . Now I configured user controls visibility when button clicked . For example:
Click buttonA user control1 with 20 buttons show up, click one of those 20 buttons another user control show up with 10 buttons.
What I am trying to do is something like browsing windows folders but in this way I will end up having too many user controls and it is confusing and I feel this is not the correct way of doing it. Can anyone please help me if there is another way to do taht? Thank you
What I am trying to do is: I click on
Your scenario seems to be ideal to use TreeView control. This controls displays a hierarchy of items, in your case a hierarchy of products and subprodcuts. A user of your application will be able to expand/collapse nodes as he/she want just by clicking a node. It is much more intuitive and readable than pressing buttons.
For example you can easily achieve the following icons:
You can also associate icons with nodes, change their background or foreground etc.
I think ListView control may fit your needs. It can display a lot of items in different forms (for your task consider using View.LargeIcon or View.Tile for ListView.View property). It also supports groups that may be useful for building POS system.

Moving Controls from One Tab Page to Another

I currently am working on a WinForm project in which there are several different tabs. Within each tab there are various controls such as buttons, sub-tabs, text-boxes, ect...
I need to consolidate the overall application which involves taking certain controls from one tab and moving them to another. When I first tried doing so, I simply copy and pasted the controls. As you can imagine this didn't work due to the fact that I didn't move the properties with the controls, I really just created NEW ones on a different tab. Therefore when I complied the code, nothing worked because there was no code assigned to the new controls.
When I tried it again, this time I CUT and paste which also maintains the same properties as the old controls (specifically the reference name in the code), so as far as I can tell, the code should identify the controls by name, and apply the same actions. However, when I compile the code, the application successfully builds but the controls do not perform any actions.
At this point I am not sure what to do...
Use the Document Outline.
View... Other Windows... Document Outline.
Select the required component and drag it from one tab page to the other in the tree control. I did this and the actions are preserved in the new tab page.
Drag the item out of the tab control and onto the form itself. Change to the other tab. Then drag the item into that tab. It is essentially 2 drag moves, but since you do not ever cut, all code linking is maintained. If your tab control takes up the entire form, simply make it smaller while you do the preceding steps and then make it large again when you are done.
When you "cut" the controls, you sever the connections between the controls and their respective events. When you "paste" them again, they're not hooked up to the events anymore, so they don't appear to do anything.
The "event" methods should still be present in your code, but you'll have to manually go through and subscribe each event to each control again (via the Properties window).
Alternatively, revert those changes, then open the .Designer.cs file and look for something like this:
this.tabPage1.Controls.Add(this.dataGridView1);
Which (for example) places dataGridView1 inside tabPage1.
If you wanted to move the DataGridView to another TabPage, you could just change this.tabPage1 in the above code to this.tabPage2.
this.tabPage2.Controls.Add(this.dataGridView1);
Then you can flip back over to the designer view and move the control around to wherever you want it within the TabPage.
I just tested it. What is happening when you cut and paste your controls, you losing the wiring of the events. What you need to do after cut and paste is to go to control properties-events, find the event in question and on the right, select a method that you want to handle that event.
This will cut them from the first TabPage and paste them on the second, i think you can do this as often as you want. And with a small change you can make it a truly copy.
hope it helps
private void ControlsToTabPage(TabPage from, TabPage to)
{
Control[] ctrlArray = new Control[from.Controls.Count];
from.Controls.CopyTo(ctrlArray, 0);
to.Controls.AddRange(ctrlArray);
}

Tab control - wait until control become enabled

I have multiple controls on one form,and when i select some value from combo box(for example 1) next control became enabled, else next control stay disabled.
Problem is that if i just press 1 and tab, after that next control became enabled, but program jump over it just like control are still disabled, and tab control selecting next control.
I need to find way how to tab check is control become enabled and go on this control,and if control are still disabled that go on next enabled control.
Thanx
You created a mousetrap for the user, very hard to escape from. Technically you can handle the keyboard navigation by trapping the Tab key before it can be used to navigate but the user still has an unsolvable problem when he wants to use the mouse to change the focus. He has nothing decent to click on.
You'll need to re-think your UI design. One possible solution is to change the ComboBox's DropDownStyle to DropDownList. Which ought to be pretty appropriate if you use its selected item to enable other controls, there should only be a limited set of valid selections. If that's not what you want then you need to do something drastic. Not necessarily limited to hiding controls instead of disabling them.
This is probably caused by the event of the combo-box you using to control your flow.
The "Changed"/"Value changed" events in most languages fire up after the control has lost focus.
You forgot to add a tag for the UI technology you are using.
If you are using WinForms, then you can try to execute the SelectNextControl method on your control that the user just edited. This will find the 'next' control for you, and activate it.
Lets assume it's winforms (playing with disabled/enabled like this in wpf is.. against mvvm rules).
Firstly, ensure what tab order/index of your controls is ok. To test, if they are all enabled, then pressing Tab should go through them in the right order. This can be seen easily
Next thing is to choose one of many possible solutions, to make 1,Tab to work:
disable Tab key navigation at all;
make controls to pass control (focus) to specific control (making tab order irrelevant);
use SelectNextControl (work best for custom controls, which when will support that tab-flow schema);
prevent focus changing, do all logic, change focus (theoretically).

Communication between User Controls

I have a TabControl with two tabs. One tab has a list of stores and the other has a list of employees. On the store tab I have a button that displays all employees of the store; to do that, I want to switch to the other tab and invoke a showEmployeesFromStore(store_id store) method from that tab's User Control. How would I do that?
You've got the wrong mental model. Just because the user control isn't visible on the TabControl doesn't mean that the code is invisible as well. Just call the control's method in your code, it needs to be public of course. Then change the tab control's SelectedIndex property to switch the active tab page.
The button should not be part of the 1st user control. Actually it is better not to use a button but to just trigger an event when the user selects another store.
I would expose an event on the store user control for SelectedStoreChanged or something to that effect. Pass back the newly selected store_id in the event delegate.
Subscribe to that event with your form. When the event fires, it is the form's job to decide with to do with that information. In this case, have it pull out the store_id from the store UserControl's SelectedStoreChanged event and pass it in to EmployeeUserControl.showEmployeesFromStore(store_id store)
Keep your controls ignorant of each other. Let the owner of the controls decide how to react to whatever events are raised by the controls. You'll sleep better with dreams of increased usability, better separation of subject areas, and more fewer working weekends due to untangling odd control flow... ;o)
Just realized I missed a detail. The button you're talking about should be on the form itself and not any of the user controls, assuming you don't want it to just update in real time using eventing described above. On button click, the form should go check StoreUserControl.SelectedStoreID() and pass the result to EmployeeUserControl.showEmployeesFromStore()

What would be the best course of action for display different form in a single Winform?

Here's a screenshot of my application:
Basically, depending on what option is selected I'd like to show another 'content' which can be a buttons, or forms or whatever.
What would be the best choice for this? Using MDI? I'm really new to this type of thing.
This scenario lends itself well to tab pages, as you'd find on a TabControl.
However, since you already have a mechanism for switching between the content, you might prefer to create a series of Panels whose Dock property is set to DockStyle.Fill. When the user clicks the appropriate heading, you simply need to show the appropriate panel and call BringToFront() on it. This is essentially what the tab control does internally, anyway.
Don't forget to use SuspendLayout() and ResumeLayout() appropriately to reduce flicker, which can be a huge problem in WinForms applications, especially when there are lots of controls.
You can position a TabControl where the buttons are not visible and control it from your buttons.

Categories

Resources