CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: C# Form Help

  1. #1
    Join Date
    Jul 2012
    Posts
    2

    C# Form Help

    Hello,

    I am trying to create a Windows Form App that when a button is clicked, it pulls up a different "form". I want the new "Form" to be in the same area and not its own pop up. What I have so far is below.
    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;
    using System.IO;
    using System.Net;
    
    namespace Exposure_Photography_V2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public void MainForm()
            {
                int ButtonSize = 175;
                int SidesBorder = 150;
                int ButtonFontSize = 20;
    
                this.Size = new Size(900, 600);
                this.Show();
    
                Height = this.Size.Height;
                Width = this.Size.Width;
                BackColor = Color.Black;
                Text = "Exposure Photography";
    
                Button PubPhoto = new Button();
                Button PriPhoto = new Button();
                Button UpdateSite = new Button();
    
                int ButtonVerticalLoc1 = ((Width - 2 * SidesBorder) / 3) - ButtonSize / 4;
                int ButtonVerticalLoc2 = ((Width - 2 * SidesBorder) / 3) * 2 - ButtonSize / 4;
                int ButtonVerticalLoc3 = (Width - 2 * SidesBorder) - ButtonSize / 4;
                int ButtonHeightLoc = Height / 2 - 100 - ButtonSize / 8;
    
                PubPhoto.Parent = this;
                PubPhoto.Size = new Size(ButtonSize, ButtonSize);
                PubPhoto.Location = new Point(ButtonVerticalLoc1, ButtonHeightLoc);
                PubPhoto.Text = "Add a Public Photoshoot";
                PubPhoto.Font = new Font(Font.FontFamily, ButtonFontSize);
                PubPhoto.BackColor = Color.White;
                PubPhoto.Click += new EventHandler(PubPhoto_Click);
                PubPhoto.Show();
    
                PriPhoto.Parent = this;
                PriPhoto.Size = new Size(ButtonSize, ButtonSize);
                PriPhoto.Location = new Point(ButtonVerticalLoc2, ButtonHeightLoc);
                PriPhoto.Text = "Add a Private Photoshoot";
                PriPhoto.Font = new Font(Font.FontFamily, ButtonFontSize);
                PriPhoto.BackColor = Color.White;
                PriPhoto.Click += new EventHandler(PriPhoto_Click);
                PriPhoto.Show();
    
                UpdateSite.Parent = this;
                UpdateSite.Size = new Size(ButtonSize, ButtonSize);
                UpdateSite.Location = new Point(ButtonVerticalLoc3, ButtonHeightLoc);
                UpdateSite.Text = "Update the Website";
                UpdateSite.Font = new Font(Font.FontFamily, ButtonFontSize);
                UpdateSite.BackColor = Color.White;
                UpdateSite.Click += new EventHandler(UpdateSite_Click);
                UpdateSite.Show();
            }
    
            void PubPhoto_Click(object sender, EventArgs e)
            {
                //Will hide the starting buttons and show the things needed for PubPhoto
            }
    
            void PriPhoto_Click(object sender, EventArgs e)
            {
                //Same as Pub but slightly different code
            }
    
            void UpdateSite_Click(object sender, EventArgs e)
            {
                //FTP Code but clears the buttons also
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                MainForm();
            }
        }
    }
    If you have any resources that will help me or any advice, it will be much appreciated.

  2. #2
    Join Date
    Jan 2010
    Posts
    1,133

    Re: C# Form Help

    You can (1) hide some elements and show some other, but that's ugly/tricky if the UI is complicated; another approach is to (2) organize various "screens" into Panels - put a bunch of child controls on a panel, and then show/hide the panels themselves (the logic is still located in the main form class); for self-contained screens you can (3) derive custom UI elements from UserControl, or even the Panel class - this keeps the logic of each "screen" in it's own class, but note that you need to make sure that the design takes the inter-screen communication requirements into consideration.

    Essentially, this is a state-machine: think of different screens as of different states - you need to figure out state transitions (Where can I get from this screen?), conditions for the transitions (What causes the change?), and you need to figure out a way to represent this in code. You also need to decide which class will be responsible for this logic. For example, in case of (1) & (2), you would probably put the code in the Form1 class; in case of (3), you can put it in Form1, or in the screen-classes.

    You can also do something like this:
    Code:
            private Panel _currentScreen = _startScreen;   // _startScreen is some existing panel
            public Panel CurrentScreen
            {
                get { return _currentScreen; }
    
                set
                {
                    Controls.Remove(_currentScreen);
                    
                    // Alternatively: keep them all, just hide them
                    // _currentScreen.Visible = false;      
    
                    if (!Controls.Contains(value))
                    {
                        Controls.Add(value);                    
                    }
                    
                    _currentScreen = value;
                    _currentScreen.Visible = true;
                }
            }
    This is a simple property that let's you show/hide (or add/remove) panels by assigning the one that's supposed to be currently visible.

    In more advanced scenarios, such as the MVP architectural pattern or its variants, the presentation logic (what's visible when, under what conditions, how things appear and why, etc.) is located in a separate class, not in the UI class (form, view).

    If you just need several separate screens, which can be accessed randomly, at any time, you can use the TabControl (the Appearance property let's you display tabs as tabs or buttons).

  3. #3
    Join Date
    Jul 2012
    Posts
    2

    Re: C# Form Help

    Thank you for the information. I found a suitable fix for now but I am going to re work it with the panel class eventually. My solution was to hide and show which form I want in the same location.

  4. #4
    Join Date
    Mar 2012
    Posts
    6

    Re: C# Form Help

    Hi Rwinnett, I will direct you to a resource. How to Create a C# Windows Forms Application on MSDN. Please see: http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx. Both the text and picture examples will help you.
    Best Regards

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

Featured