How to make a Dynamic FlowControlPanel visible in C# Winforms - c#

I'm trying to create a dynamic flowcontrolPanel and make it visible after adding controls to it. However the problem now is, the controls are added successfully but the panel is not visible on the screen. The below piece of code is a sample which I have tried form my side:
List<object> lstChild = lstFromChild;
//creating an instance for the flowlayoutpanel
FlowLayoutPanel objflowParent = new FlowLayoutPanel();
objflowParent.Name = "flowLayoutParent";
objflowParent.Location = new Point(380,155);
objflowParent.Size = new Size(800, 800);
objflowParent.BackColor = Color.DarkCyan;
objflowParent.BorderStyle = BorderStyle.Fixed3D;
objflowParent.Dock = DockStyle.Fill;
objflowParent.AutoSizeMode = AutoSizeMode.GrowAndShrink;
objflowParent.Size = new Size(300, 30);
objflowParent.SuspendLayout();
//Adding Controls to the flowlayoutpanel
foreach (Control item in lstChild)
{
objflowParent.Controls.Add(item);
}
objflowParent.ResumeLayout(false);
//Tried to hide a datagrid available in the screen to check if it is hiding the panel
dgMainGrid.Hide();
this.Show();
Note: lstChild is the list carried form another form with controls loaded in it.

Related

C# add buttons to flow control without stealing focus

In my application I dynamically create buttons and add them to a flow control that is later cleared. I have this on a timer to refresh every X seconds to clear then add buttons. This is all being done on the main form.
The problem is when I have a child form launched the main form will steal focus every time the controls are added to the flow control.
Here is the code I have that dynamically clears and adds the controls on the main form.
I call this before adding the controls
flw_users.Controls.Clear();
This is what I call to dynamically create/add the buttons to the flow control.
private void DisplayNewMobileUser(string MobileUserName)
{
// Set Button properties
Button button = new Button();
button.Text = MobileUserName;
button.Size = new System.Drawing.Size(171, 28);
button.Name = MobileUserName;
button.BackColor = System.Drawing.Color.White;
button.FlatAppearance.BorderColor = System.Drawing.Color.White;
button.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Bold);
button.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
button.ForeColor = System.Drawing.Color.Black;
button.Margin = new System.Windows.Forms.Padding(0, 1, 0, 1);
button.TextAlign = System.Drawing.ContentAlignment.TopLeft;
button.Click += new EventHandler(MobileUserName_OnClick);
flw_users.Controls.Add(button);
}
Is there a way to add buttons to a flow control with out it always stealing focus ?
Thanks to LarsTech I researched how to properly dispose each control added to flw_users. The main issues was fixed by changing the OnClick event to change focus to a label, which in turn didn't cause the main form to gain topmost every time the controls were cleared and re added. So everytime I clicked a button that button still had focus while the new form appeared.
Thanks everyone !
Here is the code I used to properly clear the controls
private void ClearUsers()
{
List<Control> ctrls = new List<Control>();
foreach (Control c in flw_users.Controls)
{
ctrls.Add(c);
}
flw_users.Controls.Clear();
foreach (Control c in ctrls)
{
c.Dispose();
}
}

Dynamic label parent not working

I'm creating a chat program and for the chatbox i want there to be a whatsapp kind of style message layout. My way of doing it (probably not the best) is by creating a picturebox dynamically with my blue background picture in it and then adding a label , making the picturebox the parent and then overlay the message over the picture box. I'm using this bit of code :
private void CreateChatBox(int height, string message)
{
PictureBox pb = new PictureBox();
Label pbl = new Label();
pb.Name = height.ToString();
pbl.Text = message;
pbl.Name = height.ToString();
pb.Image = LocalChat.Properties.Resources.ChatBox_Test;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
// set picturebox possitions and margins
pb.Left = 15;
pb.Top = 100;
pb.Width = 250;
pb.Height = 75;
tabPage.Controls.Add(pb);
//set label positions and margins
pbl.Parent = pb;
pbl.AutoSize = true;
pbl.Width = 200;
pbl.BackColor = Color.Transparent;
pbl.Location = new Point(1, 1);
// Add button click event Handler and add buttons and lables to the panel
tabPage.Controls.Add(pbl);
}
my problem is that i'm making the picturebox the parent of the label, yet the label will just sit on the very top left of the form and not inside the picturebox as i want. Am i not getting what parent should be doing?
or how do i get my label to be confined inside the picturebox?
You must remove your line of code
tabPage.Controls.Add(pbl);
at the end of the file
because after you set the parent of the label to the PictureBox you're setting its parent to the TabPage, again
When you use anyControl.Controls.Add(otherControl) you're setting the otherControl.Parent to anyControl
But i'll say that if you want that kind of control settings you should instead of dynamically creating and setting its control you could create an usercontrol and inside the usercontrol you would put the logic to the display of the message, and you could dinamically create the userControl and put it anywhere you would like

Add tabcontrol to dynamically created tab control in winforms

I've a tabcontrol on my page with 2 tabs. Now I want to create another tabcontrol dynamically and want to add the existing tab control to dynamically created tab control tabs.
Is is possible? I'm not able to add this.
Here is my code:
TabControl tbdynamic = new TabControl();
TabPage tbpaagedynamic = new TabPage();
tbpaagedynamic.Controls.Add(statictabcontrol);
tbdynamic.TabPages.Add(tbpaagedynamic);
Any idea?
Yes, it is posiible.
Add dynamic tab to Form :
this.Controls.Add(tbdynamic);
example
TabControl tbdynamic = new TabControl();
tbdynamic.Height = 200;
tbdynamic.Width = 200;
TabPage mPage = new TabPage();
mPage.Text = "Test Page";
tbdynamic.TabPages.Add(mPage);
mPage.Controls.Add(statictabcontrol);
statictabcontrol.Top = 0;
statictabcontrol.Left = 0;
this.Controls.Add(tbdynamic);
Just add the bringToFrontMethod() at the end of adding it to your Window.
tbdynamic.BringToFRont();

add a Tab to Tab control with its content

I am working on an ERP project. it is a button on treeView box and when it is clicking on a button in treeView it must create a Tab with its content (content which is defined-designed before).
I can add a tab programically but how can I design its content?
Adding this to your click event of your treeview should do what you are after:
var contentControl = new ContentControl (); //This is what we will put all your content in
contentControl.Dock = DockStyle.Fill;
var page = new TabPage("Tab Text"); //the title of your new tab
page.Controls.Add(contentControl); //add the content to the tab
TabControl1.TabPages.Add(page); //add the tab to the tabControl
To your project, add a new UserControl called ContentControl (or whatever you need, just using this in my example), and fill it with all the contents you want to appear in your tab.
You have few solutions, the simplest one is to create TabPage, create desired Controls, set up their properties (i.e. Size, Location, Text etc.), add them to the TabPage and then add TabPage to the TabControl.
TabPage tp = new TabPage();
//create controls and set their properties
Button btn1 = new Button();
btn1.Location = new Point(10,10);
btn1.Size = new System.Drawing.Size(30,15);
//add control to the TabPage
tp.Controls.Add(btn1);
//add TabPage to the TabControl
tabControl1.TabPages.Add(tp);
the second solution is to override TabPage in your class, for instance CustomTabPage where you will set up controls in the constructor of the class. Then, when you want to add new TabPage, create your CustomTabPage instance and add it to the TabControl.
public class CustomTabPage : TabPage
{
public CustomTabPage()
{
//create your Controls and setup their properties
Button btn1 = new Button();
btn1.Location = new Point(20, 20);
btn1.Size = new System.Drawing.Size(40, 20);
//add controls to the CustomTabPage
this.Controls.Add(btn1);
}
}
//Create CustomTabPage
CustomTabPage ctp = new CustomTabPage();
tabControl1.TabPages.Add(ctp);
the third solution (the best but the most complicated) is to create your desired UserControl with everything you want on it (you can use Designer help), then create an instance of your UserControl, Create a TabPage and add UserControl on the TabPage. Then add TabPage to the TabControl.
public partial class CustomControlForTabPage : UserControl
{
public CustomControlForTabPage()
{
InitializeComponent();
}
}
//Create CustomControl
TabPage tp = new TabPage();
CustomControlForTabPage ccftp = new CustomControlForTabPage();
//set properties you like for your custom control
tp.Controls.Add(ccftp);
tabControl1.TabPages.Add(ctp);
Add a new user control to the project then use the designer to do controls/layout, then when you click all you do is add a new instance of the user control to the tab - probably docked to fill the tab unless your form's size is fixed.

How do I put a panel of buttons into flow layout panel?

I have a panel of labels, buttons and image that I wish to put into a flow layout panel.
As seen in some tutorial, I understand that it is possible to auto align new and additional buttons into a flow layout panel.
what I would like to ask is that is it possible to put a panel WITHIN a flow layout panel and call multiple instances of the same panel to appear within the flow layout panel.
My panel code would be
this.panelNotification.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panelNotification.Controls.Add(this.button1);
this.panelNotification.Controls.Add(this.lblImage);
this.panelNotification.Controls.Add(this.lblName);
this.panelNotification.Controls.Add(this.lblLinkName);
this.panelNotification.Controls.Add(this.lblLinkLocation);
this.panelNotification.Controls.Add(this.lblLocation);
this.panelNotification.Location = new System.Drawing.Point(3, 3);
this.panelNotification.Name = "panelNotification";
this.panelNotification.Size = new System.Drawing.Size(506, 100);
this.panelNotification.TabIndex = 17;
So is it possible to include the whole panel into a flow layout panel? if yes, how do i do it. thank you.
Yes, you can put a Panel into a FlowLayoutoutPanel.
No, you can't put a control several times into a FlowLayoutoutPanel (in fact you can, but it is only displayed once).
But what you could do is writing some kind of Factory-Method that creates a new Panel with new Buttons/Labels/other Controls etc. every time you call it, and add these new instances to your FlowLayoutpanel. Something like this:
public class Form1
{
private Panel CreateNotificationPanel()
{
var p = new Panel { BackColor = Color.Red };
p.Controls.Add(new Button { Text = "Test" });
return p;
}
private void Form1_Load(System.Object sender, System.EventArgs e)
{
var flp = new FlowLayoutPanel { Dock = DockStyle.Fill };
flp.Controls.Add(CreateNotificationPanel());
flp.Controls.Add(CreateNotificationPanel());
flp.Controls.Add(CreateNotificationPanel());
this.Controls.Add(flp);
}
public Form1() { Load += Form1_Load; }
}
Another (and problably better) approach would be to create a UserControl that contains your Buttons/Labels/etc. instead of using a panel and adding all controls manually. Just create with the Designer and add new instances of the UserControl to the FlowLayoutPanel.

Categories