How to match default TabPage styles with programaticlaly created tab pages? - c#

When inserting tabpages programmatically, the TabPages that are created do not have the same type of color-scheme as one would expect when using the designer. As you can see from the screenshot image below, the "Active" tab's background color has remained unchanged. How would I programmatically add TabPages to a tab-control so it would behave as if I had added them using the designer?
I have the following code that programmatically inserts tab-pages onto a tab control I have created using WinForms (it is in a foreach loop).
TabPage tPage = new TabPage();
tPage.Text = item.DisplayName + " Options";
FlowLayoutPanel flowPanel = new FlowLayoutPanel();
Label lblLocationField = new Label();
lblLocationField.Text = "Insert into location field:";
CheckBox chkLocationField = new CheckBox();
chkLocationField.Name = "locationField";
flowPanel.Controls.Add(lblLocationField);
flowPanel.Controls.Add(chkLocationField);
tPage.Controls.Add(flowPanel);
this.tabControlConfiguration.Controls.Add(tPage);

It turns out, a new instance of TabPage sets the UseVisualStyleBackColor property to false by default, whereas the designer sets it to "true."
Therefore, by simply adding the one line below to my code, I was able to get it to render properly!
tPage.UseVisualStyleBackColor = true;

Related

How to append a link label to an existing label?

I have a label inside a panel to display simple text. Now I have a link label that is added dynamically that displays some more information. How can I show this link label right next to the label text at run time? For example, the lable displays
A record is added.
I need to show with a link label "View Additional Details" next to the label text.
A record is added. View Additional Details
I have the code as below but it overlaps the existing label text. Thanks for any help!
LinkLabel details = new LinkLabel();
details.Text = "View Additional Details";
LinkLabel.Link link = new LinkLabel.Link();
link.LinkData = infoDetails;
details.Links.Add(link);
details.LinkClicked += new LinkLabelLinkClickedEventHandler(details_LinkClicked);
//Adding the link label control to the existing label control
lblInfo.Visible = true;
lblInfo.AutoSize = true;
lblInfo.Controls.Add(details);
Why are you trying to add a LinkLabel to a label? Add the LinkLabel to the same form as the label, and set the location of the LinkLabel appropriately.
In the example below, I'm assuming the code is being called from the form's class (or to a panel if you are using one). If not, replace this with your form instance. I'm setting the Y location as the same as lblInfo so the LinkLabel appears next to it. Adjust lblInfo.Margin.Right and details.Margin.Left as desired.
details.Margin.Left = 5;
details.Location = new Point(
lblInfo.Location.X + lblInfo.Width + lblInfo.Margin.Right + details.Margin.Left,
lblInfo.Location.Y
);
this.Controls.Add(details);
Update: changed padding to use Margin (thanks Anthony).

Align dynamically added controls horizontally and vertically within a control in c# winforms

I have this program that dynamically adds pictureboxes referring to the number of president in the database. How do i put them inside the groupbox and align the pictureboxes inside the groupbox? And the groupbox should stretch if the pictureboxes are many.
I have this codes now :
private void Form1_Load(object sender, EventArgs e)
{
conn.Open();
try
{
cmd = new SqlCommand("SELECT COUNT(Position) FROM TableVote WHERE Position='" + "President" + "'", conn);
Int32 PresCount = (Int32)cmd.ExecuteScalar();
TxtPresCount.Text = PresCount.ToString();
for (int i = 0; i < PresCount; ++i)
{
GroupBox PresGB = new GroupBox();
{
PresGB.Size = new Size(491, 152);
PresGB.Location = new Point(12, 12);
PresGB.Text = "President";
this.Controls.Add(PresGB);
PresGB.SendToBack();
PictureBox PresPB = new PictureBox();
PresPB.Location = new Point(80 + (150 * i) + 20, 50);
PresPB.Size = new Size(75, 75);
PresPB.BorderStyle = BorderStyle.Fixed3D;
PresPB.ImageLocation = "imgPath";
this.Controls.Add(PresPB);
PresPB.BringToFront();
};
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
I would want the pictureboxes to be inside the groupbox and align it inside.
Maybe the FlowLayoutPanel control already does what you are trying to do. Just create your picture boxes and add them to a FlowLayoutPanel instead of a GroupBox.
FlowLayoutPanel automatically arranges controls in rows and/or columns depending on the value of its FlowDirection property. Set myFlowLayoutPanel.FlowDirection = FlowDirection.TopDown to get a vertical arranged list.
If you don't want multiple rows or columns set the WrapContents property to false. You can also set the AutoScroll property to true to automatically get scrollbars if the controls don't fit.
If you prefer to have the border of a GroupBox you can still put the FlowLayoutPanel into a GroupBox.
To adjust the space between the picture boxes you can use the Margin property.
This gives you a lot of control over the layout and you don't need to calculate the control positions. Also, if the size of the FlowLayoutPanel changes everything is rearranged automatically.
UPDATE:
I have a few comments on your code:
The curly braces make this look like the syntax of an object initializer - but it isn't.
GroupBox PresGB = new GroupBox(); // this line ends with a semicolon
{
// therefore this is just a block of code not related to new GroupBox()
};
You should remove the curly braces.
The creation of the group box is inside the loop. I doubt that you want a new group box for each picture box. This is the reason why you only see a single picture. Each new group box hides all the previous ones.
You add the picture boxes to the form instead of the group box.
You use "cryptic" names. PresGB and PresPB are very likely to be swapped accidentally. Abbreviations are usually a bad choice for names.
You don't need to call SendToBack or BringToFront since you don't want the controls to overlap anyway.
I don't think GroupBox is a good choice. Of course you can make it bigger if the number of pictures increases but you are limited by the screen and you don't get scollbars if the picture boxes don't fit. Use a FlowLayoutPanel. It has all the "magic" that you are looking for.
Replace your for loop with this piece of code:
var panel = new FlowLayoutPanel();
panel.SuspendLayout(); // don't calculate the layout before all picture boxes are added
panel.Size = new Size(491, 152);
panel.Location = new Point(12, 12);
panel.BorderStyle = BorderStyle.Fixed3D;
panel.FlowDirection = FlowDirection.LeftToRight;
panel.AutoScroll = true; // automatically add scrollbars if needed
panel.WrapContents = false; // all picture boxes in a single row
this.Controls.Add(panel);
for (int i = 0; i < PresCount; ++i)
{
var pictureBox = new PictureBox();
// the location is calculated by the FlowLayoutPanel
pictureBox.Size = new Size(75, 75);
pictureBox.BorderStyle = BorderStyle.FixedSingle;
pictureBox.ImageLocation = "imgPath";
panel.Controls.Add(pictureBox);
}
panel.ResumeLayout();
You can always drop a control on your form, do what you want to do then look at the designer generated code to see how the designer does it (in the "Designer.cs" file). Behind the scenes it is loading all controls and setting all properties via code.
That being said.
Keep in mind that once you put your picturebox inside the groupbox all location coordinate are in relation to the groupbox. So "0,0" is the upper-left corner of the groupbox, not the form.
To anchor your picturebox use the following code (this is just a straight copy-paste from my designer generated code, so you can clean it up a bit):
this.PresPB.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
To dock your picture box (so it fills the entire containing control):
this.PresPB.Dock = System.Windows.Forms.DockStyle.Fill;
You also need to change this line:
this.Controls.Add(PresPB);
to this:
PresGB.Controls.Add(PresPB);

Is it possible to dynamically create checkbox and specify itsgroupbox/grid and location in C# only?

I am trying to create a large number of checkboxes and felt it would not be efficient if I created them in the xaml.
Is it possible to dynamically create checkboxes and specify the group box/grid and its location in C# only? I know we can dynamically create the check boxes but I am not too sure how to manage its location via c#.
I am relatively new to WPF, thanks!
#user2584960 "Once I created them, how can I reference them? I am stuck... I gave the checkbox a name before adding to children but cannot reference them... "
I can only imaging you're trying to do:
Checkbox c = panel.Children.Add(new Checkbox());
You're not returning anything when you add to the panel, so you cant "Reference" a checkbox class.
As #Alex G was saying, his answer is showing how you add to the content window if you need to set a refrence just create a new class then add it to the window:
StackPanel panel = new StackPanel();
this.Content = panel;
CheckBox c = new CheckBox();
c.IsChecked = true;
//you Could create a loop to loop all of the list checkboxes to add them.
panel.Children.Add(c);
most likely you have a Page that has Content property
you should be able to do following
StackPanel panel = new StackPanel();
this.Content = panel;
panel.Children.Add(new CheckBox());
panel.Children.Add(new CheckBox());
panel.Children.Add(new CheckBox());
I guess you can reference them as following
CheckBox cb = new CheckBox();
panel.Children.Add(cb);

User Control c# Add Dynamically

I create a user control dynamically in my code
UserControl myobject = new UserControl();
myObject contains a button etc.
when I add this control to my picturebox
picturebox.Controls.Add(myobject);
my picturebox's backgorund image is dissappeared.
Why?
note: the button can be seen however. I want picturebox to be seen also
Set transparent basckground color of your user control. This will make picturebox visible:
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
picturebox.Controls.Add(myobject);
BTW I believe you have different name of user control. And yes, as #samjudson stated, PictureBox should not be used this way: try to use Panel with background image instead (approach will stay same - use transparent color to see parent control):
panel.BackgroundImage = // your image
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
panel.Controls.Add(myobject);
Try this:
UserControl myobject = new UserControl();
Button but = new Button();
but.BackColor = Color.Gray
pic.BackColor = Color.Green;
myobject.Controls.Add(but);
pic.Visible = true;
pic.Controls.Add(myobject);
The PictureBox control is not meant to be used as a container. Try adding a parent Panel or similar and the adding the PictureBox and your custom control to the Panel control.

How to add a control to a added tab C#

I am creating a simple application that keeps track of coins. I have a button that creates a new tab in a tabcontrol container. I am wanting to add some textboxes to the newly added tabs. The below code adds a textbox to my main tab called "Control". I have tried playing around with that field, but it always adds it to the main page called control. How would I do this? I have the following code:
string name = txtName.Text;
//validate information
try { }
catch { }
//create new tab
string title = name;
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
//Add text boxes
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(250, 75);
tb.Name = "TextBoxName";
tb.Size = new System.Drawing.Size(184, 20);
Control.Controls.Add(tb);
//put data inside of textboxes
tb.Text = txtCoin.Text;
myTabPage.Controls.Add(tb);
You need to add the text box to the controls collection of the new tab:
//Control.Controls.Add(tb); //Replace this line
myTabPage.Controls.Add(tb);
change
Control.Controls.Add(tb)
to
tabControl1.TabPages.Last().Controls.Add(tb)
tabControl1.TabPages.OfType<TabPage>().Last().Controls.Add(tb)

Categories

Resources