-1

I'm using WinForm to do my little CRM.

I got MainWindow form and on this form 3 panels ( 1 Top/ 1Left/ the other part is filled up by 7 Control User )

All the Control User are on top of each other, and when i click on some button( attached to the left panel) the called CU is brought to the front.

 public void BtnContact_Click(object sender, EventArgs e)
    {
        contactControl1.Visible = true;
        contactControl1.BringToFront();
        panelBar.Height = BtnContact.Height;
        panelBar.Top = BtnContact.Top;
        employe1.Visible = false;
        comptaControl1.Visible = false;
        histoControl1.Visible = false;
        alerteControl1.Visible = false;
        voyageursControl1.Visible = false;
        parametresControl1.Visible = false;
    }

I don't want all the CU to load at the start of the App, but i want them to be launch when i click on the button on the left. And let say if i opened one and now opening a new one it close the one who was opened.

If i have no choice to open everything ( which i doubt ) how can i choose the one i want to open first or second etc ??

Thank you

Alex
  • 1
  • 3
  • Using buttons to show\hide controls does not look intuitive. Please check how the same can be done using the third-party library like [DockPanel suite](http://dockpanelsuite.com) for example. – oleksa Jan 14 '20 at 08:37
  • Every button brings a differents CU on the front. So it would be the same. Thank you – Alex Jan 14 '20 at 08:54
  • 1
    `UserControl` has `Load` event. You can handle it in the form and load data at that point. It will be raised the first time that you show the `UserControl`. – Reza Aghaei Jan 14 '20 at 09:33

4 Answers4

0

First you need to make instance of your UC, something like this:

public partial class YourUC : UserControl
{        
    public static YourUC _instance;
    public static YourUC Instance
    {
        get
        {
            if (_instance == null)
                _instance = new YourUC();
            return _instance;
        }
    }
}

Then you need to call and show UC when button is clicked

    private void btn_Click(object sender, EventArgs e)
    {

        if (!pnlControls.Controls.Contains(YourUC.Instance))
        {
            pnlControls.Controls.Add(YourUC.Instance);
            YourUC.Instance.Dock = DockStyle.Fill;
            YourUC.Instance.BringToFront();                
        }
        else
        {
            YourUC.Instance.BringToFront();                
        }
    }
plahic
  • 1
  • 1
  • This code will just bring the UC on the front right ? Not unloding it. Cause i already managed to show hide them – Alex Jan 14 '20 at 08:57
  • than you only need to dispose.. Dispose() method releases all resources used by control YourUC.Instance.Dispose(); – plahic Jan 14 '20 at 09:15
0

You it sounds like you want to have a "pages" navigation, I'm not use to work with WinForm, but perhaps this post could help you

What is the most efficient way to create a Win form with multiple pages?

It seems that you have to manage what Control you want to display manually.

Note that passing Controls from Visible to Hidden doesn't unload them

  • That's why i'm looking for the unload them, and then reload them later while clicking to the right button – Alex Jan 14 '20 at 08:56
  • Sorry I messed up the link I've posted Here it is https://stackoverflow.com/questions/22386004/what-is-the-most-efficient-way-to-create-a-win-form-with-multiple-pages – Antoine Besnard Jan 14 '20 at 09:04
0

I don't want all the CU to load at the start of the App

Use properties tab and make visible=false or use formload event to prevent all cu loading at the start.

private void Form1_Load(object sender, System.EventArgs e)
{
   //use code to make anything visible or invisible  
}
UTSHO
  • 113
  • 2
  • 8
  • "use formload event to prevent all cu loading at the start." How ? this is my question. Thank you – Alex Jan 14 '20 at 09:01
  • Just double click on your winform.. [Click here for more info](https://youtu.be/XK0xXQtyt_U) – UTSHO Jan 14 '20 at 13:30
0

you can put all controls that should be shown or hidden on the panel. This is called the other part is filled up by 7 Control User in the question, so just use Panel to store all those controls.

Then you can introduce new field on your form to store current control index that is shown and to write button click that will check the index and activate next control

int currentIndex = -1;

public void BtnContact_Click(object sender, EventArgs e)
{
  currentIndex++;
  if (currentIndex >= panelFor7Control.Controls.Count)
    currentIndex = 0;
  foreach (Control c in panelFor7Control.Controls)
    c.Visible = false;
  Control control2show = panelFor7Control.Controls[currentIndex] ;
  control2show.Visible = true;
}

Button BtnContact has to be pressed to activate next control on the panel.

PS I suppose that you are trying to load data for the visible control only. You can use Control.VisibleChanged to check is data loaded and to load if control is visible and data was not loaded.

oleksa
  • 3,688
  • 1
  • 29
  • 54