I have a panel in WinForms which loads panels at run time during a method call.
My code looks like:
//Adding a child panel
Panel p = new Panel();
//Adding controls to panel
Label lbl5 = new Label();
lbl5.Location = new Point(105, 3);
lbl5.Text = note.noteName;
Label lbl6 = new Label();
lbl6.Location = new Point(105, 43);
lbl6.Text = note.noteName;
p.Controls.Add(lbl5);
p.Controls.Add(lbl6);
//Adding child panel to main panel
Panel1.Controls.Add(p);
In this way whenever the method is called a new child panel will be added to main panel.
Can I Click a particular panel which is displayed in main panel ?
I want to get the value of the controls present in selected panel and show it somewhere.
I would appreciate any help on this.
Name your panel....
var pPanel = new Panel();
pPanel.Name = "pPanel";
// or write it this way....using object initializer
var pPanel = new Panel
{
Name = "pPanel"
};
Then loop through the controls in you master panel for the control you are looking for...
foreach(Control ctrl in mainPanel)
{
if (ctrl.Name.Contains("pPanel")) .... then do something etc...;
}
You can also search for other controls in your panels the same way ...
Subscribe to a event like so:
Panel p = new Panel();
p.Click += panel_click;
And then create the event:
private void panel_click(object sender, EventArgs e)
{
Panel childPanel = sender as Panel;
foreach(Control c in childPanel.Controls)
{
//Do something with you values...
}
}
Related
The first line is able to clear the panel but doesnt seem to show the usercontrol in the panel in the parent form.
panel1.Parent.Controls.Clear();
if (panel1.Parent == null)
return;
messagessent uc = new messagessent();
uc.Dock = DockStyle.Fill;
panel1.Parent.Controls.Add(uc);
Why you use panel1.Parent? In this way, you remove all controls not from the panel itself, but from the parent control on which this panel is located.
This action removes the panel itself. And, accordingly, the panel does not have a parent now. The Parent property becomes null.
I suppose you need to write like this:
panel1.Controls.Clear();
messagessent uc = new messagessent();
uc.Dock = DockStyle.Fill;
panel1.Controls.Add(uc);
I have a windows form which include some textbox and labels.In my program I set all of them unvisible and when I press button it makes all of the labels and textbox visible with the code below and it works perfect.
List<Label> lbls = this.Controls.OfType<Label>().ToList();
foreach (var lbl in lbls)
{
if (lbl.Name.StartsWith("label"))
{
lbl.Visible = true;
}
}
List<TextBox> txts = this.Controls.OfType<TextBox>().ToList();
foreach (var txt in txts)
{
if (txt.Name.StartsWith("textBox"))
{
txt.Visible = true;
}
}
But when I put all of my labels and textboxes into groupbox.My code doesn't work.How can I do this?
Note: My groupbox is also unvisible and when I press button.
groupBox1.visible =true;
This code works and groupbox panel seems, but the code of labels and textboxes doesn't work.
Because you are working with the immediate child of Form here
List<Label> lbls = this.Controls.OfType<Label>().ToList();
Notice this that means your current form. so when you have controls outside in form it works,
But when you put them inside group box it won't be the immediate child anymore.
so use
List<Label> lbls = groupBox1.Controls.OfType<Label>().ToList();
This will give you access to immediate children of the group box.
You're better off creating a recursive method of your own. Try implementing something like this:
private void MakeControlsInvisible(Control container, params Type[] controlTypes)
{
foreach (Control control in container.Controls)
{
if (controlTypes.Contains(control.GetType()))
{
control.Visible = false;
}
if (control.Controls.Count > 0)
{
MakeControlsInvisible(control, controlTypes);
}
}
}
And then using it on whatever container you wish:
MakeControlsInvisible(this, typeof(Label), typeof(TextBox)); // Will make all labels and textboxes inside the entire form invisible.
MakeControlsInvisible(groupBox1, typeof(Label), typeof(TextBox));// Will make all labels and textboxes inside groupBox1 invisible.
I'm new working with C# and I'm asking on here because I didn't find a solution searching in google and other questions on SO, I will explain what my example application does:
When I run it it display a form with a textbox by default, this textbox always will be shown, after type some text and press enter it will generate a new textbox and a new button (all the controls even the default textbox are inside a panel), and the new textboxes have the same functionality as the default textbox, when I click on the button generated next to its textbox it removes the button itself and the textbox but after that if I remove some random textboxes it leaves a space between these controls, how can reorganize this content to dont let space between them?
As you can see in the image, can you tell me how can fix this or give me an advice to achieve this? thank you, by the way this is the method I use to generate the buttons and textboxes
private void GenerarTextBox()
{
panelContenedor.VerticalScroll.Value = panelContenedor.VerticalScroll.Minimum;
TextBox tb = new TextBox();
tb.Text = "Prueba " + id;
tb.Name = "txtBox" + id;
tb.KeyDown += new KeyEventHandler(TextBox_Keydown);
Button bt = new Button();
bt.Cursor = Cursors.Hand;
bt.Text = "X";
bt.Name = "btnPrueba" + id;
bt.Click += new EventHandler(ClickBotones);
Point p = new Point(20, 30 * id);
Point pb = new Point(130, 30 * id);
tb.Location = p;
bt.Location = pb;
panelContenedor.Controls.Add(tb);
panelContenedor.Controls.Add(bt);
tb.Focus();
id++;
}
And this to remove the textboxes and the buttons
private void ClickBotones(object sender, EventArgs e)
{
Button bt = sender as Button;
string nombreBoton = bt.Name;
string idBoton = nombreBoton.Substring(9);
string nombreTextBox = "txtBox" + idBoton;
foreach (Control item in panelContenedor.Controls.OfType<Control>())
{
if (item.Name == nombreTextBox)
{
panelContenedor.Controls.Remove(item);
panelContenedor.Controls.Remove(bt);
}
}
}
You could place your dynamic controls on a FlowLayoutPanel. Either directly or grouped together in a Panel or UserControl.
Set the FlowDirection property of the FlowLayoutPanel to TopDown. The FlowLayoutPanel will then arrange your controls automatically. You can also set the WrapContents property to False and AutoScroll to true to make the scroll bar appear.
Alternatively you can use FlowDirection = LeftToRight, place the text box and the button directly on the FlowLayoutPanel and let the child controls wrap (WrapContents = True). In the child controls, a new property FlowBreak appears. It can be set to True for the last control to appear in a row and let the next one wrap independently of the width of the FlowLayoutPanel.
You can also play with the Margin property of the child controls to control their layout in the FlowLayoutPanel as the Location property becomes useless.
The FlowLayoutPanel (as well as the Panel) is available in the Toolbox in the section "Containers".
When you delete the controls, you need to do a recalc of the positions. So when you have added them in sequence, you can go with:
bool repos = false;
Point p;
foreach (Control item in panelContenedor.Controls.OfType<Control>())
{
if (repos)
{
Point tmp = item.Location;
item.Location = p;
p = tmp;
}
if (item.Name == nombreTextBox)
{
panelContenedor.Controls.Remove(item);
panelContenedor.Controls.Remove(bt);
repos = true;
p = item.Location;
}
}
Up till now I have been just displaying my programmatically created controls to this.controls but now I want to add in tabbing functionality for large sets of data. I added in a tab control and in the code I have the programmatic controls added to the tabpage but I cannot get the controls to display... help what do I need to do get the controls to display
right now what i have is
private void Form1_Load(object sender, EventArgs e)
{
panel = new Panel();
panel.Location = position;
panel.BorderStyle = BorderStyle.Fixed3D;
panel.Width = 240;
panel.Height = 210;
company = new Label();
company.Location = new Point(panel.Location.X + 10, panel.Location.Y + 10);
company.Text = tempServer.Value.companyName;
company.Font = new Font(company.Font.FontFamily, 12, FontStyle.Bold);
tabs.TabPages["1"].Controls.Add(company);
this.Controls.Add(tabs);
this.Controls.SetChildIndex(tabs, this.Controls.Count);
}
Edit(to help clarify)
I have an application which reads from a database for each tuple in the data base my WinForm application creates a new panel which is then populated with various information with dynamically created labels. the position is then offset and the next panel is created. I was informed that i need to now have my application support tabs. each tab will only show so many panels. my problem accured when i tried to add these dynamically created panels to the tab control instead of this.control. when i did so the panels and their information was no longer being drawn and I cant figure out how to make the panels display
You didn't set Text for your label so you didn't see any thing, try this:
company = new Label(){Text = "some text here"};
The whole code:
private void Form1_Load(object sender, EventArgs e) {
company = new Label{Text = "some text here"};
tabs.TabPages["1"].Controls.Add(company);
this.Controls.Add(tabs);
}
I have a Panel named panel1. panel1 has a "mosuseHover" eventhandler .panel1 also has some controls like pictureBox , label etc.
When i move mouse on panel1 , the event fire correctly , but when the mouse courser goes on panel1 controls , like pictureBox , the event not work .
how can i make event to be invoke when mouse courser is on child controls.
I should note that i dont want create eventhandler for each child contol.
Best Regards
You can add an IMessageFilter to implement your own global MouseHover for your Panel like this:
//This code uses some reflection so you must add using System.Reflection
public partial class Form1 : Form, IMessageFilter
{
public Form1(){
InitializeComponent();
Application.AddMessageFilter(this);
}
public bool PreFilterMessage(ref Message m) {
Control c = Control.FromHandle(m.HWnd)
if (HasParent(c,panel1)){
if (m.Msg == 0x2a1){//WM_MOUSEHOVER = 0x2a1
//Fire the MouseHover event via Reflection
typeof(Panel).GetMethod("OnMouseHover", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(panel1, new object[] {EventArgs.Empty});
}
}
return false;
}
//This method is used to check if a child control has some control as parent
private bool HasParent(Control child, Control parent) {
if (child == null) return false;
Control p = child.Parent;
while (p != null) {
if (p == parent) return true;
p = p.Parent;
}
return false;
}
}
NOTE: The code above is implemented to work for nested controls of any level in your Panel. If your panel contains only child controls stopping at level 1. You can change the code a bit by using c = Control.FromChildHandle(m.Hwnd) and check the control's parent by c==panel1 without having to use the HasParent method to check for child-ancestor relationship.
You may create an eventhandler on the children and simply call the panels handler with the same arguments.
Also have a look at this thread
I countered the same problem. I solved it by creating a MouseEnter event and in it I declared Control ctl = sender as Control; Then I called ctl's Parent, with, Control panel = ctl.Parent; Now do whatever you want, as in panel.BackColor = Color.Red;
Like this:
private void label_MouseEnter(object sender, System.EventArgs e)
{
Control ctl = sender as Control; // gets the label control
Control panel = ctl.Parent; // gets the label's parent control, (Panel in this case)
if (ctl != null)
{
ctl.Font = new Font(ctl.Font.Name, 11, FontStyle.Underline); // change the font of the Label and style...
panel.BackColor = Color.Blue; // change the panel's backColor
ctl.Cursor = Cursors.Hand;
// do more with the panel & label
}
}
But don't forget to iterate through all the controls and to get the Panel and whatever that's inside the Panel.
Like this:
public YourApp()
{
InitializeComponent();
foreach (Control objCtrl in this.Controls) // get all the controls in the form
{
if (objCtrl is Panel) // get the Panel
{
objCtrl.MouseEnter += new EventHandler(panel_MouseEnter); // do something to the panel on MouseEnter
objCtrl.MouseLeave += new EventHandler(panel_MouseLeave);
foreach (Control ctl in objCtrl.Controls) // get all the controls inside the Panel
{
if (ctl is Label) // get the label inside the panel
{
ctl.MouseEnter += new System.EventHandler(label_MouseEnter);
ctl.MouseLeave += new EventHandler(label_MouseLeave);
ctl.Click += new EventHandler(label_Click);
}
if (ctl is PictureBox) // get the pictureBox inside the panel
{
ctl.MouseEnter += new EventHandler(label_MouseEnter);
}
}
}
}
}
You can figure out the rest.
Hope this helps.