Results 1 to 5 of 5

Thread: Simple C# Question

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Simple C# Question

    Why doesn't this work? the panel does not show. It really should, it does if i add the panel from the Designer..

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace iPhone_Clone
    {
        public partial class HomeScreen : Form
        {
            public HomeScreen()
            {
                InitializeComponent();
            }
            Panel Panel = new Panel();
            PictureBox PicBox = new PictureBox();
    
            private void HomeScreen_Load(object sender, EventArgs e)
            {
                Panel.BackColor = Color.Green;
                Panel.Width = 50;
                Panel.Height = 100;
                Panel.Top = 50;
                Panel.Left = 50;
    
                PicBox.BackColor = Color.Red;
    
                Panel.Controls.Add(PicBox);
    
                Panel.Visible = true;
    
            }
    
            
    
        }
    }

  2. #2
    Lively Member
    Join Date
    Aug 2007
    Posts
    66

    Re: Simple C# Question

    You need to add the panel to the control collection of the form like you did with the picturebox in the panel.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: Simple C# Question

    Thanks!

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: Simple C# Question

    This doesn't work..
    Code:
     
    This.controls.add(Panel) ;

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Simple C# Question

    It'll work if you do it properly so you haven't done it properly. Show us what your code actually looks like now.

    Also, it's really a very bad idea to use 'Panel' as the name for a variable of type Panel. It's like you're trying to make your code confusing. At least follow the almost universal convention of starting the names of variables with a lower-case letter, but also try to provide a descriptive name that indicates something's purpose and distinguishes it from its type.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width