Displaying group items in a panel - c#

I have a problem displaying items inside a panel, that is attached to dataflow layout, the panel has label, textbox, button, checkbox and image, when I run the program only the image disaplys even though I have set the postion of each control in the panel, and the panel is looped from the database inside the dataflow layout control, also, when i try to change the size of the panel nothing displays.
I just want someone to point to the right direction , any help is appreciated
thank you
what I have done do far:
foreach ("looped according to the database")
{
Panel pan = new Panel();
PictureBox img = new PictureBox();
Button btn = new Button();
Label house = new Label();
TextBox Street = new TextBox();
CheckBox check = new CheckBox();
pan.Location = new Point();
pan.Size = new System.Drawing.Size();
MemoryStream ms = new MemoryStream();
img .Image = Image.FromStream(ms);
img .Tag = Product.ID;
img .SizeMode = PictureBoxSizeMode.StretchImage;
img .Location = new System.Drawing.Point();
img .Size = new System.Drawing.Size();
img .TabIndex = 0;
house .Text = Product.Name;
house .Location = new System.Drawing.Point();
house .Size = new System.Drawing.Size();
house .AutoSize = true;
house .TabIndex = 1;
Street.Location = new System.Drawing.Point();
Street.Size = new System.Drawing.Size();
Street.TabIndex = 2;
btn.Text = "info";
btn.Size = new System.Drawing.Size();
btn.Location = new System.Drawing.Point();
btn.ForeColor = Color.Green;
btn.TabIndex = 2;
pan.Controls.Add(img);
pan.Controls.Add(check);
pan.Controls.Add(House);
pan .Controls.Add(Street);
pan.Controls.Add(btn);
this.ProductsFlowPanel.Controls.Add(pan);
}
}
I am trying to achieve something similar to this;
image

A couple of things I noticed,
The panel object you created at the start is reassigned on line 10 and 11:
Panel pan = new Panel();
...
pan = new Point(); //probably remove this
pan = new System.Drawing.Size(); //and this
In addition, the size and position of all your controls is just empty points and sizes, eg:
img .Location = new System.Drawing.Point();
img .Size = new System.Drawing.Size();
It should probably be something more like this:
img .Location = new System.Drawing.Point(10,10);
img .Size = new System.Drawing.Size(50,50);

Okay, there are some things in your code which are definitely wrong. I don't know if that's causing the problem, but this is the direction you asked for.
The syntax: something x = new something(); is used to create an object of something
The prerequisite for this is that the something() class should be reachable.
Now let's break this down something x; <-- this declares something as x
x = new something() <-- this initiates x to the something object.
What you're doing in your code is:
Panel pan = new Panel(); //pan is now an object of `Panel()` class.
pan = new Point(); //pan is now an object of `Point()` class.
pan = new System.Drawing.Size(); //pan is now an object of `System.Drawing.Size()` class.
Now, in C#, the last declaration is accepted. So basically, pan isn't an object of the Panel class anymore. Which means your logic for panel won't work.
Another thing I noticed is that the size and position of all your controls is just empty points and sizes.
It should be something like:
img .Location = new System.Drawing.Point(50,50);
img .Size = new System.Drawing.Size(100,100);
Hope this helps :)

Related

How do I dynamically layer objects ontop of eachother in Windows Forms?

I'm using a FlowLayoutPanel to dynamically display an array of PictureBoxes and Labels. They're going in the order: PictureBox-Label-PictureBox-Label and so on. I need those labels to appear above each picture so they'll match, basically layring them ontop of picturebox margins. I tried using Controls.SetChildIndex(temp, 2); but it seem to just swap the postion of the picturebox. I also tried using temp.BringToFront(); but then all the pictureboxes being displayed at the top of the panel and all the labels are below (I need each label to match each picturebox above them). Here's the code:
public void RunMeta()
{
Label mostPickedLabel = new Label();
mostPickedLabel.Text = "Most picked heroes";
flowLayoutPanel1.Controls.Add(mostPickedLabel);
mostPickedLabel.Margin = new Padding(15, 0, 1000, 0);
mostPickedLabel.Font = new Font("Lucida Sans Unicode", 15);
mostPickedLabel.ForeColor = Color.DarkCyan;
mostPickedLabel.Size = new Size(200, 30);
foreach (var mostPickedHero in FetchDataFromDota2Site.MostUsedHeroesAndImages)
{
PictureBox temp = new PictureBox();
temp.ImageLocation = mostPickedHero.ImageSource;
temp.SizeMode = PictureBoxSizeMode.StretchImage;
temp.Left = temp.Width * flowLayoutPanel1.Controls.Count;
temp.Margin = new Padding(15, 30, 15, 30);
flowLayoutPanel1.Controls.Add(temp);
flowLayoutPanel1.AutoScroll = true;
Label heroName = new Label();
heroName.Text = mostPickedHero.MostPickedHeroName;
heroName.Font = new Font("Lucida Sans Unicode", 8);
heroName.ForeColor = Color.White;
flowLayoutPanel1.Controls.Add(heroName);
}
}

BackgroundImage layout to Bottom

I have a task, that can be easily completed with html/css, but I cant figure out how to do it with C# WindowsForms.
I need to create container with dinamic height and with 3 types of BackgroundImage's. To do so I created panel1 and set BackgroundImageLayout of "body.jpg" to tile.
Then I put panel2 and set BackgroundImageLayout of "top.jpg" to none.
So I need to create only one container with "bottom.jpg" but here is the problem.
Image is tall and it must have layout to bottom, but I cant set BackgroundImage of panel3 to Bottom and I cant dock PictureBox to bottom, because in that case it will prevent to set content overlay.
P.S. Sorry if you cant understand some of my explanations - my native language is Russian.
Here is what constuctor created for me:
//
// Charsheet_bg
//
this.Charsheet_bg.AutoSize = true;
this.Charsheet_bg.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Charsheet_bg.BackgroundImage")));
this.Charsheet_bg.Controls.Add(this.Charsheet_top);
this.Charsheet_bg.Dock = System.Windows.Forms.DockStyle.Left;
this.Charsheet_bg.Location = new System.Drawing.Point(0, 0);
this.Charsheet_bg.Margin = new System.Windows.Forms.Padding(0);
this.Charsheet_bg.MinimumSize = new System.Drawing.Size(285, 0);
this.Charsheet_bg.Name = "Charsheet_bg";
this.Charsheet_bg.Size = new System.Drawing.Size(285, 488);
this.Charsheet_bg.TabIndex = 30;
//
// Charsheet_top
//
this.Charsheet_top.BackColor = System.Drawing.Color.Transparent;
this.Charsheet_top.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("Charsheet_top.BackgroundImage")));
this.Charsheet_top.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.Charsheet_top.Dock = System.Windows.Forms.DockStyle.Fill;
this.Charsheet_top.Location = new System.Drawing.Point(0, 0);
this.Charsheet_top.MinimumSize = new System.Drawing.Size(285, 200);
this.Charsheet_top.Name = "Charsheet_top";
this.Charsheet_top.Size = new System.Drawing.Size(285, 488);
this.Charsheet_top.TabIndex = 31;
And screenshot will be in 5 mins.
Screenshot with tooltips

Overlapping PicuresBox with transparent background

I want make two or more overlapping PictureBox with transparent background but if I do this I see only one Image:
I create my PictureBoxes like this:
PictureBox pb1 = new PictureBox();
pb1.Size = new Size(32, 32);
pb1.Location = new Point(0,0);
pb1.Image = Image.FromFile("../Graphics/Grounds/ground.png");
pb1.Visible = true;
PictureBox pb2 = new PictureBox();
pb2.Size = new Size(32, 32);
pb2.Location = new Point(0,0);
pb2.Image = Image.FromFile("../Graphics/Grounds/human.png");
pb2.Visible = true;
Later I add those two PictureBoxes to my Panel:
panel1.Controls.Add(pb1);
panel1.Controls.Add(pb2);
So why does it only shows one PictureBox?
Try change panel1.Controls.Add(pb2); to pb1.Controls.Add(pb2);
Make sure you set the correct location as pb1 be the container
If your human.png has transparent background:
pb2.BackColor = Color.Transparent;

C# Add Controls To Panel In a Loop

I wish to add a button for every line in a file to a panel.
My code so far is:
StreamReader menu = new StreamReader("menu.prefs");
int repetition = 0;
while(!menu.EndOfStream)
{
Button dynamicbutton = new Button();
dynamicbutton.Click += new System.EventHandler(menuItem_Click);
dynamicbutton.Text = menu.ReadLine();
dynamicbutton.Visible = true;
dynamicbutton.Location = new Point(4+repetition*307, 4);
dynamicbutton.Height = 44;
dynamicbutton.Width = 203;
dynamicbutton.BackColor = Color.FromArgb(40,40,40);
dynamicbutton.ForeColor = Color.White;
dynamicbutton.Font = new Font("Lucida Console", 16);
dynamicbutton.Show();
menuPanel.Controls.Add(dynamicbutton);
repetition++;
MessageBox.Show(dynamicbutton.Location.ToString());
}
menu.Close();
The problem is that only the first control gets created.
The code looks fine but there could be a following situations.
1.You might have only one entry in the file, so you are experiencing only One Button added to the panel.
2.Your panel width is smaller than the sum of all the dynamic buttons width.
I suspect no 2 is the main reason that is causing problem.
So, I recommend that you use FlowLayoutPanel. To add a dynamic content as it automatically layout all the child controls.
Each time it is generating the same name for dynamic controls. That's the reason why it is showing only the last one. It simply overwrites the previous control each time.
int x = 4;
int y = 4;
foreach(PhysicianData pd in listPhysicians)
{
x = 4;
y = panPhysicians.Controls.Count * 30;
RadioButton rb = new RadioButton();
rb.CheckedChanged += new System.EventHandler(rbPhysician_CheckedChanged);
rb.Text = pd.name;
rb.Visible = true;
rb.Location = new Point(x, y);
rb.Height = 40;
rb.Width = 200;
rb.BackColor = SystemColors.Control;
rb.ForeColor = Color.Black;
rb.Font = new Font("Microsoft Sans Serif", 10);
rb.Show();
rb.Name = "rb" + panPhysicians.Controls.Count;
panPhysicians.Controls.Add(rb);
}
Try this code
StreamReader menu = new StreamReader("menu.prefs");
var str = menu.ReadToEnd();
var items = str.Split(new string[] {"\r\n" } , StringSplitOptions.RemoveEmptyEntries);
foreach (var item in items)
{
Button dynamicbutton = new Button();
dynamicbutton.Click += new System.EventHandler(menuItem_Click);
dynamicbutton.Text = item;
dynamicbutton.Visible = true;
dynamicbutton.Location = new Point(4+repetition*307, 4);
dynamicbutton.Height = 44;
dynamicbutton.Width = 203;
dynamicbutton.BackColor = Color.FromArgb(40,40,40);
dynamicbutton.ForeColor = Color.White;
dynamicbutton.Font = new Font("Lucida Console", 16);
dynamicbutton.Show();
menuPanel.Controls.Add(dynamicbutton);
repetition++;
}
The problem with Panel and similar controls other than the FlowLayoutPanel is when you create a control and a second one, the second is created at the same position if you are not changing it's location dynamically or setting it according to the other already added controls. Your control is there, it's in the back of the first control.
A flowLayoutPanel is better as it will add the controls next to each other as you add them while compromising more finer control at their positioning.
I also have similar problems with panels. For what you are doing it could be useful to just add strings to a listbox rather than using labels and a panel. That should be simpler.

Show image above SMFPlayer issue

An image (Image class) is placed above a SMFPlayer (both elements are created in code-behind). Z-index of the image is the Z-Index of SMFPlayer + 1. The image is resized (adjusting the width) according to the playing progress of SMFPlayer.
videoPlayer = new SMFPlayer();
videoPlayer.Width = 1920;
videoPlayer.Height = 1080;
videoPlayer.Margin = new Thickness(1920, 0, 0, 0);
PlaylistItem item = new PlaylistItem();
Random r = new Random();
item.MediaSource = new Uri("video.wmv");
item.DeliveryMethod = DeliveryMethods.ProgressiveDownload;
videoPlayer.Playlist.Add(item);
videoPlayer.AutoPlay = true;
videoPlayer.AutoLoad = true;
videoPlayer.IsControlStripVisible = false;
videoPlayer.PlaylistVisibility = FeatureVisibility.Disabled;
videoPlayer.MediaEnded += new EventHandler(player_MediaEnded);
LayoutRoot.Children.Add(videoPlayer);
bar_yellow3 = new Image();
bar_yellow3.Source = new BitmapImage(new Uri("/SMF_ProgressiveDownload1;component/assets/bar_y.png", UriKind.Relative));
bar_yellow3.Width = 775;
bar_yellow3.Height = 34;
bar_yellow3.Margin = new Thickness(2948,1034,0,0);
bar_yellow3.Stretch = Stretch.Fill;
bar_yellow3.VerticalAlignment = VerticalAlignment.Top;
bar_yellow3.HorizontalAlignment = HorizontalAlignment.Left;
LayoutRoot.Children.Add(bar_yellow3);
However, when the playing progress is less than 20%, the image blinks randomly. When the SMFPlayer is set to be invisible ( Visibility.Collapsed ) , the image is normal.
I have tried to call the update function of the Image, which is: bar_yellow3.UpdateLayout(); but the method does not solve the blinking issue.
Any solution?
Try use effects (Shazzam will help you) instead using Z order.

Categories