Add UI element dynamically on Button click - c#

I made a simple Form with a TableLayoutPanel and a button below it.
Is it possible to create another TableLayoutPanel below the one I already have on each button click?
And how can i fill it with data?
I searched for many hours but didn't find anything.
Here is a screenshot of my form, if it helps you:
I want the other tables to appear on the lower half of the split.
Thanks in advance, Jan

Of course you can. You may want to consider adding the TableLayoutPanels to a FlowLayoutPanel so that they will wrap and scroll automatically.
Just create the control, setup the columns and rows, then add it to some container:
TableLayoutPanel tlp = new TableLayoutPanel();
// setup "tlp" with your desired properties
tlp.XXX = YYY
// ...
flowLayoutPanel1.Controls.Add(tlp);

Related

How make an area on win form scrollable?

Items like below have to be accommodated on a specific area on WinForm.
"Order 1 ------------ Time Left: 00:20:00"
"Order 2 ------------ Time Left: 01:30:20"
We should be able to perform the following action on the each order:
Each item will occupy not more than one line. As the area specified
for it on the win form is limited, as more items comes in, I want to
make the area scrollable.
According to the time left the background color of the line should also change.
There is a DONE button next to it. So, if the item is selected and DONE is pressed, the item is moved off the list.
My question is what C# Form control can be used for it. I was thinking of labels, but how to make the area scrollable if there are many of them. If not labels, what else is suggested?
As suggested in comments, you can use grid, but in case it does not suits your requirements, this is like something what you can do -
Create a custom user control which will have a label control for your order detail and a Done button. Something like this (sure you will design it better!). The label will be empty initially and you will pass then from outside, using a public property for example, while creating the control.
Define an Event on this control, and raise this event when the Done button is clicked. This is required for your main form to know that when to remove this user control.
Add a FlowLayoutPanel to your main form. This will be the container for your user controls. Make sure to set the following so that the controls are created as desired
this.flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
this.flowLayoutPanel1.WrapContents = false;
this.flowLayoutPanel1.AutoScroll = true;
Now you can start adding your custom control to this FlowLayoutPanel, either by loop or the way you like. The control will added in linear way, one in each line, and you will also get scroll it it exceeds the given space.
Make sure to define event handler for the control, so that you know when to remove the control. And of course you can set other properties like back groung color etc. That's not going to be any problem.

RradioButton GroupBox in DataGridView?

At run time I populate the values in DataGrid. dropdownlists,numeric,bool,text.
Is it possible to have a group of radio buttons ?
How can to do it ? how to add a groupbox so that one of them is selected at a time?
if I don't use a DataGridView, what is another suitable View ?
PS. not using xaml,wpf. classic windows forms.
Datagridview will not be suitable for this kind of combination, therefore, it's best if you use the flow layout control and that should help with organising the lay out.
I think you can't put a groupbox in a datagridview component, it's not made for that I think.
You have to create your own DataGridViewCell with painting logic and handling click events. Long time ago I did something like that, maybe it will be useful for you:
http://pastebin.com/nh4PfPPS
I paint check box and 2 radio buttons and save their positions to handle clicks.
I did not create column for it, because I needed to mix cell types, so just in place where I was adding rows and needed that cell I did:
row.Cells[0] = new DataGridViewControlCell();
I would not say it is the best way, but hey worked for me. Good luck :)

how to dock a tabcontrol

I am beginner in c#. I have some problems with arranging the controls on a winforms. In my project, I took one tabControl and gave the property dock --> fill. After implementing all the designs, I remembered that my form need panelHeader as well as panelFooter. when I add them and given property dock -->top and dock --> bottom respectively, these panels are coming on top of the tabControl. I tried to solve this by removing dock --> fill to tabControl and setting it again. but no use. How to solve this problem? (i dont want to delete tabControl and redesign because it took me 4 hours for design).
If this is a waste question then please only comment so that i can delete this question later.
Please Help
Thanks in Advance.
EDIT:
Well i handled it by using cut and paste properties.. but what if there are more controls to take care of (if so then it will make the form messy).
Try right click on the control and click "Bring to Front". I remember trying this long before I am not sure that this answer is correct.
Temporarily set Dock = None for Tabcontrol......... add a TableLayoutPanel with 3 rows in your form.
1.In First row u can add "panelHeader" and set Dock for it
2.Select TabControl and move it to tablelayout second row set the Dock = Fill
3.In last row add your "panelFooter"
You have to write in your Designer.cs page this code
this.yuortabcontrol.Dock = System.Windows.Forms.DockStyle.Fill;
And instead .Fill you write as per blow image in tabcontrol

adding controls to tableLayoutPanel

I was wondering if it was possible to add more than one item into single tableLayoutPanel?
Currently, I can only insert ONE item, it won't accept anything else. I would like to have for example a richtextBox with label and button inside it. Is is possible? Thanks! I'm not asking for code, I just need to know if it's possible to manually drag and drop these items into single cell in tableLayoutPanel.
It appears you are only allowed one control per cell, if that is what you mean. You can always add a container control such as a panel with it's Dock property set to Fill you can then add your additional controls to it. Or add another tablePanelLayout Control to the Cell and set the row / columns how you need and then add your controls to that. Or as LarTech mentioned in the comments a UserControl would work also.
You Can also add a Panel Control and then add all other stuff to the Panal
Add a container such as a GroupBox or Panel, and set it to fill all available space in the table cell, and that could override the TableLayoutPanel's "one per box" rule so you should be able to add as many controls as you wish

How do I create a banner panel in C#?

I want to create my own custom control that is basically a TableLayoutPanel with 3 rows and 1 column. The top and bottom rows will contain labels (banners) and the middle row is where I will add other controls. The problem is that when I try to build other forms/controls from this control, the designer doesn't recognize the middle panel. How do I get it to? If I drag a textbox to the middle and set Dock=Fill, it will cover the entrie form/control. Also, is there any way to get the designer to reject dragging of controls to the top and bottom (banner) rows? I've tried the steps in the following link but haven't had any luck (http://support.microsoft.com/?scid=kb%3Ben-us%3B813450&x=21&y=15).
I figured it out. The trick was to create my own designer that inherits from ParentControlDesigner and overrides the Initialize method and calls EnableDesignMode for the inner content panel. On top of this, I needed to set the Designer attribute of my user control to this new designer. The details are shown here.
One problem, though. I can drag controls to the content panel I created and everything looks fine. But, once I recompile, the controls disappear. They are still there, I just think they're getting drawn before the banner panel. I will create a separate thread for this problem.

Categories