Dynamic Naming of C# Form Elements - c#

My app needs to be able to dynamically create new form elements and work with them. Right now I have a panel with buttons and labels in it. I need to be able to make a duplicate of this and show it in my app and then work with it.
For example, I have panel1. Inside are label1, button1, and button2.
Label 1 just counts up by seconds.
When you click button1, label1 starts counting up. When you click button2, the timer stops.
My problem is that I need to be able to duplicate panel1 many times and still have the new buttons correspond to the correct labels.
On button_click
private void button1_Click(object sender, EventArgs e)
{
Button theSender = (Button)sender;
Panel parentPanel = (Panel)theSender.Parent;
}
From here, I can't target any of the child control . I'm used to targeting and handles in jQuery, so I don't even know the correct C# terminology for how to explain myself.

If understand your problem correctly, I recommend you to make a Usercontrol with a Panel and fill it with your Label, Button and whatever. Write the events for your buttons in the usercontrol. Then introduce this usercontrol in your form and it should work. You can introduce any number of usercontrols in your form and each button will behave/work for the label in that usercontrol only.
As you mentioned you are new in winforms and you are not sure what I am saying, let me know and I will help if I get enough time.
Hope it helps.

Children of a control can be accessed using Control.Contrtols collection, e.g. to access button on a form:
Button btn = this.Controls["button1"];
But that is only true if button1 is placed directly on your form and button1.Name property is set to "button1" (designer does that automattically, if you are creating your controls dynamically, you have to take care of naming your controls yourself.)
You can also enumerate child controls of any control, e.g. child controls of panel1:
foreach(Control c in panel1.Controls)
{
// do something, e.g.
if(c is Label){//do sth...}
if(c.Name.Equals("label1") && c is Label)
{
Label l = c as Label;
}
}
and as #rapsalands said, UserControl may be an answer for you.

I would create a user control (UserControl) for this.
Check this article for more explanation about the difference between Control and UserControl.
Controls and UserControls are easy to duplicate and the full functionality is there.

You can create new UI Controls in code as you would any other object: Button b = new Button();
Then you can add them to the form using form.Controls.Add(b). You'll need to position and size the controls as well (there are properties available for doing this) and hook up your event handlers using b.Clicked += form.button_click;.
To see an example of this, you can try having a look at the designer.cs file that is generated in Visual Studio (don't make changes to it, just have a look). It will look quite complex at first but might go some way to helping demystify Windows Forms, and you will be able to find all of the properties you need to set in there.
Whenever you update something in the designer, Visual Studio generates new code and puts it in the designer.cs file. The entire form is set up in the InitializeComponent() method, which is called from the constructor of your form. You should be able to copy some of that code and with a couple of modifications use it for creating your own dynamic UI elements.
As rapsalands says, it sounds like a user control would be useful in this situation, as it will help encapsulate the functionality you're after. However that may take a bit of time to get your head round and you may find it simpler for now to do everything in your form without creating a new control.

So you are a beginner and need some time to understand Usercontrol as I mentioned in my previous answer. Use a for loop in the Constructor or Load event of your form to dynamically generate controls.
Panel panel;
Label label;
Button button1;
Button button2;
for(int i = 0; i > count; i++)
{
panel = new Panel();
button1 = new Button();
button2 = new Button();
label = new Label();
panel.Controls.Add(button1);
panel.Controls.Add(button2);
panel.Controls.Add(label);
Controls.Add(panel);
button1.Click += Event1;
button2.Click += Event2;
}
private void Event1()
{
label.Text = "Button 1 Clicked."
}
private void Event2()
{
label.Text = "Button 2 Clicked."
}
This way certainly you can create as many controls you want and will also serve your purpose. Use some variables to locate the panel controls appropriately. Set any properties you wish to add in the for loop for the controls.
This is just an alternative for my previous answer. I still recommend the previous answer given by me. This code is dummy and not tested.
Hope it helps.

Related

C# WPF Create function that creats new inputs

I am using C# Wpf and i would like to create a function that once you click on a button A new Textbox will appear is this possible and I want to know if it possible to increase the form hight when a new Textbox is created
The question isnt exactly specific but yes you can make new UI elements in the code-behind. In WPF you would put your button on the window, then add the "Click" event to it. Then in the click event you could code something like. . .
private void Button_Click(object sender, RoutedEventArgs e)
{
Button newButton = new Button(); //Create a new button control and assign it to newButton
newButton.Width = 35; //Access fields like this (You can access any of the ones you access from the Xaml user interface)
newButton.Height = 20;
newButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
newButton.VerticalAlignment = System.Windows.VerticalAlignment.Top;
newButton.Content = "click me!";
grMain.Children.Add(newButton); //Add it to the grid
}
grMain is the name of my grid, make sure you name it and call it by the name. You can also call other elements, such as a stack panel or whatever you need. So for your case just make it a textbox instead of a button
Oops didnt see the second part of your question! To resize the window you need to first add a name to it in the XAML (Like you would name a grid or button - just name the window something). And just as we called grMain in my example call that and change the height/width properties.

how to access a user define controls iner controls in parent form

I'm new to windows forms programming so my question may sound little strange.
I have created a user define control (countdown timer) now I'm creating n no of it dynamically in a form by Click of a button (Add new timer) its working well and good.
Here is the Creation Code
private void Addnew_Click(object sender, EventArgs e)
{
UserControl1.userControl11 = new UserControl1();
flowLayoutPanel1.Controls.Add(userControl11);
}
My user control has a Reset button that reset all the content inside the user define control.
it is also working, but What I want Allow user to reset all the Created timers using the “Reset All” button on the form.
Okay one way to do this.
Create a List<UserControl1> private member on your form called say _myUserControls
In your Addnew Handler add it to the list.
If you have a remove button, don't forget to remove from _myUserControls as well.
Add a Reset method to your UserControl1, that does what it needs to do.
Then in your Reset all button click handler
foreach(UserControl1 ctrl in _myUserControls)
{
ctrl.Reset();
}
Jobs a good 'un
The answer I referred you to in comments, would be a way of finding all instances of your UserControl1 class, so you wouldn't need an internal list.

Usercontrol removal

My problem is the following. I have a form which when it loads up invokes a user control onto it. On the user control I have buttons. With the buttons I would like to make the user control disappear with all the buttons and picture boxes on it and make a new user control appear. The appear part is all right but I can't make the user control disappear with all the objects on it.
Can you help me with this one?
So the dispose method closes the usercontrol well, but then I can't make the new user control appear. Code is like this:
akusztikusUserControl auc = new akusztikusUserControl();
public menuUserControl()
{
InitializeComponent();
}
private void akusztikus_btn_Click(object sender, EventArgs e)
{
this.Dispose();
this.Controls.Add(auc);
}
}
Have you considered using Panel or a TableLayoutPanel control ? In both cases you can remove controls through the .controls.remove option
You can then just add the user control to the panel / tablelayoutpanel whenever you need to use it again.

How to use same textbox with other tabs on the tabcontrol?

Sorry for the simple question and also sorry if there is an answer on the site and I couldn't find.
I want to use same textbox in every tab that I use on my form. How can I do that?
Add this to the TabControl's SelectedIndexChanged event handler.
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox1);
}
Adding a control to one tab page's Controls collection automatically removes it from the others.
Note: I have tested it by adding two lables on the form above the tab control and added these two lines to the method shown above:
label1.Text = tabControl1.TabPages[0].Controls.Count.ToString();
label2.Text = tabControl1.TabPages[1].Controls.Count.ToString();
Put the TextBox in the parent control of the TabControl. It can hover over all the rest. You might need to rework the focus traversal though.

C#, fire event of container

I have a MyButton class that inherits from Button. On this class I have placed several other controls (Labels, Progessbar). The problem with this is that the controls on the Button make it impossible to fire the Button.Click or Button.MouseHover event. How can I achieve it that the controls on the Button are only displayed but are "event transparent": A click/hover on the label and progessbar is the same as if I clicked/hover directly on the Button (including sender and everything). Something like "inheriting the events from the parent".
class MyButton : Button
{
Label foo = new Label();
ProgressBar bar = new ProgessBar();
}
You should derive from UserControl then have the button as a child control, and bubble up the button child's on click event.
This link is probably more than what you need, but it's a good starting point.
UPDATE
As pointed out, you may not be using ASP.NET. So here is another post that talks about different custom user controls, specifically what you're after is a Composite Control. This is for Windows Forms.
Write Click event handlers for the label and PB, have them call PerformClick().
Making controls transparent to the mouse is possible but is ugly to do. You'd have to override WndProc() to catch WM_NCHITTEST and return HTTRANSPARENT. The all-around better solution is to not use controls. A Label is just TextRenderer.DrawText() in the button's Paint event. ProgressBar isn't hard either, e.Graphics.FillRectangle().
Having the child controls be real controls in front of the button (either in a class inheriting from Button or from UserControl) may make it hard to get button-specific events working properly, as you have found. (Edit: Although it's hard, it's not impossible -- see Hans Passant's answer)
As a workaround, instead of using child controls, you could custom-paint them onto the button surface, since you don't need most of the functionality of the controls (events, focusing, etc.), just their display.
You can do the additional painting in the OnPaint method of your class. Something like:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawString("My fake label", Font,
SystemBrushes.ControlText, new Point(10, 10))
// draw progressbar
}
To update the display, you would need to trigger a repaint of the Button using Invalidate().
Take a look at Custom Bitmap Button Using C# on CodeProject for a more complete example.
This answer was here a moment ago, but got deleted:
Can you inherit from UserControl instead? This will allow you to place other controls on the control surface, including your button and subscribe to their events.
If you're using WPF, I guess what you're looking for would be something called Bubbled Events. It's a feature in WPF by which events are bubbled from a control to its parent (in your case, it would be from your ProgressBar and Label to your button). I found this article on the matter which I think would be of help to you.
Hope this helps :)

Categories

Resources