Domanda

I'm new to the c# programming language, and I seem to have gotten by pretty swell up until this.

How can I start my program without having one of my panels automatically run its paint method, I want for my panel to stay blank and wait for the users input, but instead its paint method keeps getting called on on start up.

Currently I have found that simply not adding the event until after the button is clicked works, but somehow I feel that isn't the "proper" way to go about this.

È stato utile?

Soluzione

Hard to say without seeing some of your existing code, but here's a few possibilities.

  1. Create some class-level boolean variable called didUserEnterInput and set it to "false" initially. When the user enters input, set it to "true". In your paint event, just check the variable.

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        if (didUserEnterInput)
        {
            // generate pattern
        }
    }
    
  2. You could just hide the panel initially. Set Visible = false (WinForms) or Visibility = Collapsed (WPF). When the user enters input wherever, change the visibility to display the panel.

  3. If this is WinForms, you could just copy the code out of the .designer.cs file and into your code-behind, in a separate method named CreatePanel() or something. Then call that when you need it at runtime to actually display the panel.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top