C# How to group different Form Controls together? - c#

I am using Windows Forms.
I do only have one Frame. In this Frame I organized some Controls together. Where the Controls in one Row belong together (they form an Object). And overall I have five of those Rows, with the same Controls.
Eg.: Name , Surname, Age, Income, etc (in one Row)
There are Dropdownlists, Checkboxes, NumicUpDown, etc
I would now need to know an way to "bind" them together. The same counts for the Events. Because at the moment I would make for every different Control an Event and then distuingish them by an Number at the end of the Controls Name, which would be rather annoying. And then generate from that the Object.
What way do I have to not make dozens of Events and indicate that everything in one Row belongs together?

For grouping them together on the Form (not Frame) - put them all on one Panel.
To use the same even handler for all of them - simply subscribe to the same one. (Using the designer - select the events, and choose from the drop down. For doing this from code - just do YourControl2.EventName += YourControl1.EventName.) Note that this is not possible for every type of Control and every event. It's only possible if the events have the same signature.

You can easily create a UserControl. Right-click the project or a folder in the project and select UserControl. You can then add controls to it, in the designer, just as for Forms.
Once you have compiled the project, your UserControl will appear at the top of the toolbox and you can drag and drop it onto a form.
Like this would would create a user control representing one row, and add one of them for each row to your form. You can also add additional properties, methods and events to your user control class. They will automatically appear in the properties window.
But in your case, a DataRepeater Control for Windows Forms would be appropriate. It is part of the Visual Basic Power Packs, but just works as well in C# projects.

take all your controlls in one Panel. So You can with group more RadioButtons and they can know one for anderer. Then you can between them scwiching so you becomming what you whant.

Related

C# WinForms - How to create a Properties Page like Visual Studio Project Properties

I'm trying to create a form that operates like the Visual Studio Properties page, with a list of categories (like Application, Build, Build Events etc) in a column on the left and corresponding information on the right. I'm planning to use either panels or a tabcontrol (with the tab header hidden) for the right hand side.
However, I'm unsure how best to create the column of categories on the left. Is there a standard control that provides this functionality?
Otherwise, I considered using a panel containing Buttons, or individual Panels, or a ListBox, but I don't think these would give the same look. Also, I don't want to write code if a suitable control already exists.
Ideally I would like to be able to easily disable all the categories, for example while editing a record on one page.
Having nested categories might be nice, but is not essential.
If the information on the right is not related to one another as you switch categories on the left, to create a good separation between categories I'd suggest you do the following:
Create a user control for each individual category
Split your form in two, the bar on the left and a panel on the right
The bar on the left, for the categories, can be a list of radio buttons, or links, or whatever you like. I'd suggest a TreeView since it easily support sub-categories.
As the user click on a category (by attaching a method to the corresponding even on the control used for the categories) you can remove the control from the panel, if any, and reset the control that corresponds to the selected category.

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);
}

Best way to display groups of data in one form?

I am looking to create a windows form. This form is to display groups of data separately and I was aiming to do so in the same form window rather than have multiple windows open.
For example each group of data is defined by a Job#. A user will want to review X different Job#'s at a time (Range would usually be 1-5, but could be more). I would like to have 4 dataGridViews for each Job# plus various identifying and summed data in text boxes. Initial Example Concept
I was looking into using TabPages/Tab Controls. My initial idea was to have a user click the different tabs to view the data for those jobs that they have pre-selected. However these tabs don't seem to behave like classes from what I can see. Is there perhaps a better way to go about this or some way to have the tabs act like classes? So that each tab has a Job TextBox, 4 Different DataGridViews, etc. So that I can easily create and display any number of jobs?
For example each Tab would have 4 dataGridViews, maybe 8 Text Boxes, Standardized Labels and a Standardized layout.
So would using tabs be a good idea? Using some other WinForm control?
There are at least 2 solutions here:
Create a custom Panel holding all the controls you want (TextBox, DataGridView, ...), design it so that it looks best to you. Then add each of that Panel to each of TabPage of your TabControl.
Create a new custom TabPage and add the custom TabPage to your TabControl.TabPages instead of the standard TabPage.
I think the second approach can be done if you can initialize everything using code (not by drag-n-drop) because to get the drag-n-drop supported, you may need to write a custom TabControl. So the first approach is feasible and OK. You can replace the Panel by a Form, set the Form.TopLevel = false, you can add that form to any container control. Using Form, you can benefit the easiness of drag-n-drop to design and organize your controls.
Here is a little of code which may help you figure out:
public class TabPageClient : Form {
public TabPageClient(){
InitializeComponent();
Dock = DockStyle.Fill;
TopLevel = false;
FormBorderStyle = FormBorderStyle.None;
}
//.... other code
//I think this is important
//You should define some Properties and methods to control the data flowing into and out from your TabPageClient.
//You can also support some method to add/remove the controls dynamically.
}
Take the drag-n-drop requirement into account, I think using UserControl may be better:
public class TabPageClient : UserControl {
public TabPageClient(){
InitializeComponent();
Dock = DockStyle.Fill;
}
}
For UserControl, you should place the class in a separate *.cs file, compile your project and then you can see it in the ToolBox window (at the very top). Just drag and drop it like as other controls.
Personally, I prefer grid-detail views. Something we make heavy use of in our software. The form has a SplitContainer in vertical alignment.
In the top panel, you add a list of some kind (ListBox, ListView, DataGridView...any control into which you can load a list and then react to selection).
In the bottom panel, you have yet more options. Simplistically, you could use a TableLayoutPanel and then setup rows/columns to provide whatever arrangement of embedded controls you like, such as your array of grids to display data. I suppose in keeping with your model, you'd have a single ColumnStyle and four (4) RowStyles. Then in each row, add a grid.
This way, you keep it all in one place...one "screen", if you like (no tabs to flip through). You react to the selection in the list to decide what data to display in the grids. You then just need a bit of code (a class I would hope) that interfaces between the class(es) that provide the data, and the form controls that display it (the grids).
An additional benefit here is that with the list presentation, you can have a lot more than five (5) jobs in play at any one time. In fact, with scrolling, as many as you like (not limited to the number of tabs before the display goes to pot).
Also, you would be re-using the grids. Simply refreshing the displayed data for the list item selected. You could even pre-load and cache it all if freshness is not an issue.
Also consider binding directly to data source if that's an option.

How to implement search for form objects?

I use c# and c++ builder, and have big application with many menus, buttons, check boxses, and also with many forms. When trying to use one of functionality of those objects I use standard user method by clicking on button, clicking on menus and choose some submenu options, etc...
But now I want to use faster method to use functionality of objects, want to create search for objects. That means if I type name of some button and press enter, that those event become same as i click on same button. I don't want to clicking on buttons, and menus anymore cause it take me too much time to find it.
So I create new form which appear on shortcut and have inside listbox that will be using for search all objects, but I don't know what code to put inside that listbox show me all object names?
How to connect listbox with all objects which is inside project?
In C#, every Control has Controls property that returns a collection of child controls. You can iterate through all the "children" of a form and add their names to your listbox. In the opposite direction, when you've got a control name, you can find it using Controls.Find() method.
For CBuilder case, refer to the following thread:
BCB : how to iterate over controls on a form?

WinForms TabOrder tool: Broken or just confusing?

I have a form with a bunch of panels, and some panels inside groupboxes. When using the TabOrder tool in Vs2005, the controls outside of containers are given integers (0), the controls inside panels are given decimals (72.0), and the controls within panels within groupboxes are given three-part values (73.73.0). Unfortunately the resulting tab order has nothing to do with the order I clicked my controls.
Does this tool simply not support nested containers? Am I doing something wrong? Perhaps holding Shift- or Ctrl- when I click (I've tried these with no success)?
Am I going to be forced to manually type in three-part tab orders for all my controls? That would be a bummer.
The tab order tool is not designed for you to enter values manually; it is designed for you to click on controls in the order that you'd like them to progress as the user tabs.
The numbers are not decimals; they represent the tab order of the control within its parent container. For example, if you have a Form with a Panel named panel1 and a Button inside of it named button1, then button1 would display a number like:
X.Y
X is the tab order of panel1
Y is the tab order of button1 within panel1.
I will acknowledge that the designer isn't as intuitive (or transparent) as it probably should be, but it does work.
I had the same problem with textboxes and buttons within group box in VS2010. TabOrder tool was just useless: Tab orders were broken no matter how I re-ordered the tab stops. In order to make the correct tab order I had to re-order of how controls are added to the group box in form designer initialization code:
this.groupBox2.Controls.Add(this.startTimeTextBox);
this.groupBox2.Controls.Add(this.endTimeTextBox);
this.groupBox2.Controls.Add(this.exitButton);
This way tab order would be startTimeTextBox -> endTimeTextBox -> exitButton and so on.
I think I figured out the way to do it in the designer: the trick is apparently that you have to click the panels/groupboxes as well in order to assign the different parts of the full ordering; in this way, it seems that a bredth-first clicking method needs to be used as opposed to clicking the child controls themselves.
Kinda sad, since it forces you to know the full structure of the whole form instead of just what the user sees.
I had this same problem and discovered this tool: http://archive.msdn.microsoft.com/cdstabindex
I had to change the manifest to make it work with VS2010 though. Also, I've modified the source code for myself to make the UI a little better, but even as it is, I would recommend having a look at this tool.
Remove Group-boxes from Controls and try again this works for me :)

Categories

Resources