User Control c# Add Dynamically - c#

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.

Related

Displaying user control in parent form panel

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

How do I display a PictureBox on a PictureBox in C#?

I want to display a PictureBox ontop of a Picturebox. I have a "mother" Picturebox and a button next to it. Everytime you click the Button a new PictureBox should be displayed on the "mother". I have created the PictureBox like this:
PictureBox newPictureBox = new PictureBox();
newPictureBox.Location = new Point(x:30,y:30);
newPictureBox.BackColor = Color.Red;
newPictureBox.Visible = true;
newPictureBox.Height = 200;
newPictureBox.Width = 200;
Now I have no idea how to display it to the user. I tried to use .Show() and
Call the GetChildIndex and SetChildIndex methods of the parent's Controls collection.
I also tried that. Either I don't know how to call it or it just doesn't work. Been searching for a solution for way too long. Does anybody have an idea of how to display that PictureBox on top of that pictureBox?
So turns out, I forgot to add the picture Box to the "mother" Picturebox. I added 1 line:
motherPictureBox.Controls.Add(newPictureBox);
So now my Code looks like this:
PictureBox newPictureBox = new PictureBox();
newPictureBox.Location = new Point(x:30,y:30);
newPictureBox.BackColor = Color.Red;
newPictureBox.Visible = true;
newPictureBox.Height = 200;
newPictureBox.Width = 200;
motherPictureBox.Controls.Add(newPictureBox);
Didn't need to bring it forward, I just forgot to add it...
you need to add new picturebox to form's controls
PictureBox NewPictureBox = new PictureBox();
NewPictureBox.BackColor = Color.Red;
NewPictureBox.Location = MotherPictureBox.Location;
NewPictureBox.Size = MotherPictureBox.Size;
this.Controls.Add(NewPictureBox);
NewPictureBox.BringToFront();

How to reproduce a forum/twitter-like UI on Winform

As a classic forum, threads and replys are displayed on a page, with dark and light and dark and light backcolor.
I am trying to write a client of a forum on windows using winform. It is
At first, I have tried this way:
Add a big panel to the form, let's call it the PARENT PANEL.
Add small panels to the big panel like this:
panel1.Visible = false;
for (int i=0; i<5;i++)
{
Panel parent = new Panel();
parent.Height = 800;
Random ra = new Random();
TextBox p = new TextBox();
p.Text = "fehsuifq";
p.Multiline = true;
p.WordWrap = true;
p.Dock = DockStyle.Fill;
parent.BackColor = Color.FromArgb(ra.Next(0, 254), ra.Next(0, 254), ra.Next(0, 254));
p.BorderStyle = BorderStyle.None;
p.ReadOnly = true;
p.TabStop = false;
p.BackColor = this.BackColor;
parent.Controls.Add(p);
parent.Dock = DockStyle.Top;
panel1.Controls.Add(parent);
}
panel1.Visible = true;
Every panel(tenicially, a control) displays a thread's text and images and others details(like authors or avator).
Images are not shown until it is clicked.
when the image is clicked, it is loaded and the controls's height will change as result.
The PARENT PANEL will contains hundreds of these controls since there will be so many threads. It is and have to be scrollable, obviously.
But if I put a textbox in the control, the scroll wheel no longer work on the PARENT PANEL.If I use a label, it is not selectable.
I think this way can't be more stupid, completely.
So I am looking for a better to do this job, to display hundreds or even thousands threads/replys on winform, which is:
the height is dynamic, because the images inside will not load until it is clicked.
The text inside is selectable (I edited this just to disambiguate)
the PARENT PANEL can response to the mouse's wheel, just like twitter, forums.
So that I can use my scroll wheel to browse all the replys at one time. The loading is a background work.
Look at the picture, when the text is selected, the whole panel is still response to the wheel(just like normal webPage). This is a uwp app and I am not sure if winform can do this.

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

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;

How to programmatically add PictureBox control to the tab page control

How can I programmatically add a PictureBox control to a TabPage control and fit to its size?
PictureBox pb = new PictureBox();
pb.Dock = DockStyle.Fill;
tabPage.Controls.Add(pb);
The code would be look like:
var pb = new PictureBox();
pb.Image = Bitmap.FromFile(#"\\filepath");
pb.Dock = DockStyle.Fill;
yourTabPage.Controls.Add(pb);
But if your sole purpose for the tab page is displaying an image, you better use the 'BackgroundImage' of the tabpage itself and use its BackgroundImageLayout to maintain image size, this saves a bit of hassel compared to adding a PictureBox

Categories

Resources