c# create a user control on button press, won't show - c#

counters.Add(new Counter());
foreach (Counter con in counters)
{
con.Show();
con.Top = this.Top;
con.Left = this.Left;
}
counter is a very basic UserControl I made with 3 buttons and textbox. I'm trying create a draggable Counter, I can see the counter on the list (counters) but I can't see it anywhere on screen.
I was wondering if new Counter() is enough to create the UserControl on screen.
(i mean to create this "counter" object dynamically)

Control needs to have a parent that hosts the control. The parent control can be either a form or another container control. When you want to add your control to the hosting control, add the instance into the Controls collection of the hosting control like this:
var counter = new Counter();
var form = new Form();
form.Controls.Add(counter);
form.Show();

I think user Controls need to be in some sort of container, for example a flowLayoutPanel or your instance of the Form class itself to visually exist.
You need to call CONTAINERINSTANCE.Controls.Add(con).

Related

create dynamic tabs and use control of tabs in C#.net

I created one emulator for one device, so each time the user clicks on the button I am creating one new form, and each form represents new devices.
now I am looking for another option by which inside my main form I can create multiple tabs (instead of multiple forms) which has controls like button, dropdown, text box, grid view.
and the user can navigate between tabs, create a new tab dynamically, use controls inside of the tab.
any suggestion ??
Just use the TabControl on my 'main form'. and added tab pages at runtime.
tabControl1.TabPages.Add("New Tab");
placed an entire form inside a tab control ...this code will create a new instance of a form and place it in the last tab of a tab control:
frmDevice dev = new frmDevice();
dev.TopLevel = false;
dev.FormBorderStyle = FormBorderStyle.None;
dev.Parent = tabControl1.TabPages[tabControl1.TabCount - 1];
dev.Dock = DockStyle.Fill;
dev.Show();
Reference :https://www.codeproject.com/Answers/5246227/Create-dynamic-tabs-and-use-control-of-tabs-in-csh#answer1

How to access runtime controls from another form? (C#)

I have a form (Form1) and a button on it. When I press that button, I create runtime panels stored in an array of panels, declared like that:
Panel[] Panouri_variabile = new Panel[20];
If I press the button, a panel is created. If I press the button again, another panel is created underneath the previous panel and so on.
Each panel has a textbox inside it. Obviously, the textboxes are stored in an array of textboxes, declared like that:
TextBox[] Nume_variabila = new TextBox[20];
The user writes something in each textbox of each panel.
Now, I want to access the data written by the user in those textboxes, from another form, like that:
Form1 form = new Form1();
form.Panouri_variabile[i].Nume_variabila[i].Text
That could be easily done if the panels and the texboxes are created at design-time, by simply setting the Modifier property of all controls to public.
The problem is that they are created at run-time, so I can't change the Modifier property.
After a lot of searches I found the following posible solution:
Panel new_Panel = Panouri_variabile[i];
And then declare the following property at the same level as the event - handlers are (class-level I think)
public Panel new_Panel { get; private set; }
I noticed that I can see the new_Panel from another form, so I can access it like that:
Form1 form = new Form();
form.new_Panel
but the problem that it is not indexable! I have an array of panels (and an array of texboxes) so I should access them using an index, as I specified above!
Is there a way of accessing those texboxes from another form? Or should I create them design-time?

How do I have similar multiple panels of GUI to appear on my Windows Form Application

I have a c# windows form application that has a similar GUI functionality as MSN. It works in the way such that only a notification window appears if there is notification which in this case I have put several buttons and other stuffs in a single panel. (is this the right way to do so?)
How do I code it such that I can use a arrayList to add similar panels to the list and use a for loop to call it out. Example would be calling 2 or 3 similar panels through the use of arraylist(?) and for them to appear below one another. (Maybe like how MSN notifications window comes up one above another.)
the code for the panel is
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.lblImage);
this.panel1.Controls.Add(this.lblName);
this.panel1.Controls.Add(this.lblLinkName);
this.panel1.Controls.Add(this.lblLinkLocation);
this.panel1.Controls.Add(this.lblLocation);
this.panel1.Location = new System.Drawing.Point(13, 134);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(506, 100);
this.panel1.TabIndex = 17;
do I have to code the for loop in the designer file or the coding file? as after I have tried to add for loop in the designer code file, the designer view sort of unable to display my UI.
I'm guessing this is what you're looking for
for(int i = 0; i < panels.length; i++){
AddPanel(panels[i], i);
}
AddPanel(System.Drawing.Point point, int tabIndex){
Panel panel = new Panel();
this.Add(panel);
panel.Controls.Add(new Button());
panel.Controls.Add(new Label("Image"));
panel.Controls.Add(new Label("Name"));
panel.Controls.Add(new Label("linkName"));
panel.Controls.Add(new Label("linkLocation"));
panel.Controls.Add(new Label("location"));
panel.Location = point;
panel.Name = "panel" + i.ToString();
panel.Size = new System.Drawing.Size(506, 100);
panel.TabIndex = tabIndex;
}
You'll need to populate the panels array with a the points you'd like your panels to be added at.
I would create a Custom Control that has the interface you want for each item. Have the Control expose properties, methods, and events that allow you to access the child controls in a constant manner.
You can create multiple instances of the custom control and add them to a List and attach event handlers to them, etc...
If you use a flow layout panel as the parent and add each instance of the custom control to it, it will automatically handle the layout for you without you having to manually position them. If the flow layout is inside a container with autoscroll set, you will have a nice scrolling list of whatever kind of Items you can dream up.
adding example
With a custom control called ListItem.
ListItem item = new ListItem();
someFlowPanel.controls.add(item);
You should probably set item's width to the width of the flow panel you add it to, and set it to anchor left and right.

Creating a custom TabPage control in C#

I'm building a small tabbed c# Form and I'd like each tab page to have some common features, notably, an OK button and an error message and to have a space for the specific form fields.
Has anyone else done something similar and how did you approach it?
This is easy to do without extending either TabControl/TabPage.
Define one UserControl, and put the common elements on it you want on every TabPage.
On the Form: go ahead and design the TabPage specific controls you want for each TabPage : make sure they are not going to visually overlap with the common controls once the UserControl has been added.
In the Form Load Event of your main Form do something like this :
// form scoped variable to hold a referece to the current UserControl
private UserControl1 currentUserControl;
private void Form1_Load(object sender, EventArgs e)
{
foreach(TabPage theTabPage in tabControl1.TabPages)
{
currentUserControl = new UserControl1();
theTabPage.Margin = new Padding(0);
theTabPage.Padding = new Padding(0);
theTabPage.Controls.Add(currentUserControl);
currentUserControl.Location = new Point(0,0);
currentUserControl.Dock = DockStyle.Fill;
currentUserControl.SendToBack();
}
}
Even though the 'SendToBack isn't really required here it is "insurance" that your UserControl with the 'Okay button and TextBox for an error message are placed behind the individual controls you have assigned to each TabPage.
Several ideas:
Keep the common controls outside the tabpanel;
Extend the TabPage/TabControl
Create a base UserControl with the common buttons and make usercontrols that inherit from it. Then place one inherited usercontrol per TabPage.

C# making a tab, adding controls to the tab, and then having multiple tabs of that type

TabPage newpage = new TabPage();
Tabs.TabPages.Add(newpage);
newpage.Controls.Add(this.tableLayoutPanel41);
newpage.Location = new System.Drawing.Point(4, 26);
newpage.Name = "AddMaintAgreement" + offset;
newpage.Size = new System.Drawing.Size(736, 523);
newpage.TabIndex = 10;
newpage.Text = "Add Maintenance Agreement";
newpage.UseVisualStyleBackColor = true;
offset++;
Basically thats what i have at the moment, I added the offset in there because i thought it might affect my problem.
Basically, this code here works okay for adding one "addmaintagreement" tab. After that only the latest tab has any controls on it!
Basically I'm stumped. Any help would be appreciated. Thanks.
I think short example should stay here:
TextBox tmpLog = new TextBox(); // create new control of textbox type
tmpLog.Text = "some text here";
TabPage tb = new TabPage("my brand new tab"); //create tab
tabControl.TabPages.Add(tb); // add tab to existed TabControl
tb.Controls.Add(tmpLog); // add textBox to new tab
tabControl.SelectedTab = tb; // activate tab
Derive from TabPage and add the controls you want in that derived class. Then use your derived class instead of TabPage.
Controls can only be parented to one control, but it looks like you are trying to parent your tableLayoutPanel41 in every TabPage instance. You need to create new copies of the controls for each instance of the tab. There are various ways to fix this.
Programmatically create your tab page and its contents multiple times.
Have the contents of your TabPage implemented as a user control that you dock fill on a tab page. Then recreate one of those for each page duplicate your require.
Create a class derived from TabPage that implements your tab page and create new instances of that for each use.

Categories

Resources