Programmatically added controls not showing when added to a tab page - c#

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);
}

Related

C# getting form reference in an eventhandler

My problem is not complicated but i've been looking up for quite some time now and can't seem to find the correct way to phrase it so if this is a duplicate I am sorry.
I have a Form created on a button click, within this Form is a table layout which contains a set of Controls (a label, a combo box and a button). What I want to do is to store the current index selected in the combo box when the button is pressed. However, I can't access the combo box when I'm in the button click event handling block.
This is the creation of my Form, nothing extraordinary.
Form settingsWindow = new Form();
settingsWindow.Text = "Connexion settings";
settingsWindow.MaximumSize = new System.Drawing.Size(200, 200);
TableLayoutPanel layout = new TableLayoutPanel();
layout.Dock = DockStyle.Fill;
Label label = new Label();
label.Text = "Choose an available COM port";
Button bt_OK = new Button();
bt_OK.Text = "valider";
bt_OK.Anchor = AnchorStyles.Bottom;
bt_OK.Name = "bt_OK";
bt_OK.Click += Bt_OK_Click;
ComboBox cb_portList = new ComboBox();
SerialComm serial = new SerialComm(); //Filling the combo box with a list of
List<string> portList = new List<string>(); //available COM ports
portList = serial.getAllPorts();
foreach(string portName in portList)
{
cb_portList.Items.Add(portName);
}
layout.Controls.Add(label);
layout.Controls.Add(cb_portList);
layout.Controls.Add(bt_OK);
What I'd like to be able to do is the following, imagine I made a class to store the data I want :
private void Bt_OK_Click(object sender, EventArgs e)
{
datacontainer.selectItem = cb_portList.SelectedItem;
settingsWindow.close();
}
Am I not able to do so because my Form is already created within an eventhandler ? Or is it just because they're in separate blocks. If so is there a way the new Form and its controls available from outside the block ?

How to delete (remove) space between textboxes after remove some controls?

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;
}
}

Getting Control details loaded in Panel dynamically in WinForms

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...
}
}

Trying to add a image to a group box, however, it doesnt show up

Solved: Using design toolbox.
I'm using the following method to add a image
private void cobertura1vertente_Load(object sender, EventArgs e)
{
PictureBox pb1 = new PictureBox();
pb1.Image = Image.FromFile("C:/cobertura1vertente.png");
pb1.Location = new Point(3, 3);
pb1.Size = new Size(250, 250);
//doubt right below
groupBox81.Controls.Add(pb1);
}
I usually would add a image to a entire form, but now i want to add it to a specific groupbox, who lies inside a tab.
If the code is correct, my guess is that the location that i've set is relative to the whole form, and not to the groupBox, why would that happen?

Making text popup in a C# forms application on mouseover [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
create custom tooltip C#
Does anyone know of a way to make a box 'popup' when the user cursors over a certain item?
For example, I want to have a PictureBox on a C# forms application and when the user cursors over it, a box of text will pop up.
I'm aware of ToolTip however I was thinking of something more customisable; in my mind I'm thinking of the kind of popup boxes you see in World of WarCraft when you cursor over an item in your inventory (obviously it doesn't have to be THAT flashy, but at least one where the text colour, background colour, text etc. are all modifiable).
You can use a ToolStripControlHost to host a control (for instance a panel) and add the content you want. Then you add that control to a ToolStripDropDown using the Items collection, and use the Show(Control,Point) method to show the control.
Thought I'd add an example
public class Form1 {
public Form1() {
ToolStripDropDown customToolTip = new ToolStripDropDown();
customToolTip.Items.Add(new CustomPopupControl("Hello", "world"));
MouseMove += (o, e) => {
Point location = e.Location;
location.Offset(0, 16);
customToolTip.Show(this, location);
};
}
class CustomPopupControl : ToolStripControlHost {
public CustomPopupControl(string title, string message)
: base(new Panel()) {
Label titleLabel = new Label();
titleLabel.BackColor = SystemColors.Control;
titleLabel.Text = title;
titleLabel.Dock = DockStyle.Top;
Label messageLabel = new Label();
messageLabel.BackColor = SystemColors.ControlLightLight;
messageLabel.Text = message;
messageLabel.Dock = DockStyle.Fill;
Control.MinimumSize = new Size(90, 64);
Control.Controls.Add(messageLabel);
Control.Controls.Add(titleLabel);
}
}
}
I mean if it a button or an image button you can add something like MouseHover action and then show your message
private void button1_MouseHover(object sender, System.EventArgs e)
{
MessageBox.Show("yourmessage");
}
you need to customize the tooltip. refer to
http://www.codeproject.com/Articles/98967/A-ToolTip-with-Title-Multiline-Contents-and-Image
There are some other articles there, but this one works fine for me.
You may need to add code for your requirement.

Categories

Resources