-2

When I start my program, date should be printed on label named dan.Text automatically but it prints only when I make mouse click input on GUI. How do I fix that ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VremenskaPrognoza
{
    public partial class vremenskaPrognoza : Form
    {
        public vremenskaPrognoza()
        {
            InitializeComponent();
        }


        private void vremenskaPrognoza_Load(object sender, EventArgs e)
        {

        }

        private void ime_grada_Click(object sender, EventArgs e)
        {

        }

        private void dan_Click(object sender, EventArgs e)
        {


            Console.WriteLine(dan.Text = DateTime.Now.ToString("DDDD/MM/YYYY"));


        }
    }
}
Trevor
  • 7,777
  • 6
  • 31
  • 50
Renato
  • 1
  • 4

4 Answers4

1

Put the dan.Text assignment in the form Load event:

private void vremenskaPrognoza_Load(object sender, EventArgs e)
{
  dan.Text = DateTime.Now.ToString("DDDD/MM/YYYY");
}

Or in the form Shown event:

private void vremenskaPrognoza_Shown(object sender, EventArgs e)
{
  dan.Text = DateTime.Now.ToString("DDDD/MM/YYYY");
}

Here is the chain of calls:

  1. Constructor : prefered place to instantiate objects and initialize UI instances.
  2. Load : prefered place to create and initialize non designer UI objects or complete them like populating combobox or dataset.
  3. Activated : prefered place to do things each time the form is activated and get focus.
  4. Shown : prefered place to do things after the form is showned like open another form or show a info box.

The difference between using constructor, load or shown is mainly to have a clean code design and a smooth UI behavior.

There is no real difference between putting code in the constructor or in the load, but if an exception occurs in the constructor it is more serious than in the load.

https://learn.microsoft.com/en-us/dotnet/framework/winforms/order-of-events-in-windows-forms

  • Not sure why you were voted down. Have an up cause this is exactly correct. – N_tro_P Oct 28 '19 at 16:41
  • I haven't voted down, I'm new user so I can't give upvotes or downvotes. – Renato Oct 28 '19 at 16:52
  • @Çöđěxěŕ To have clean code, to separate it, to have small methods and have a distinction between things you do and stages. And for example, you have an app that performs a web update check at startup: the user will prefer having the notify box under the main app form instead of an alone box on the desktop (unless the app must starts hidden in the tray icon)... to know for sure where she/he is. And yes, if an exception occurs in a constructor it is always more serious than outside because the instance is not created and that may cause more problems than in the load, if you don't catch it. –  Oct 28 '19 at 17:11
  • @Çöđěxěŕ The word constructor means what it means: it constructs the instance. You may prefer putting vars creation here and others initializations like populating a combobox in the load event. So modifying the Text property of a control or the selecteditem of a combobox is better done in the load or in the shown events (taking in considartion that the user see things done in the shown). But in fact It is only good practive advices and you can do all these things where you want... –  Oct 28 '19 at 17:19
0

Put the code to be executed into the Form.Shown Event.

It is very important to put it in the right event. Putting it in a event that is too early, can cause the code to run before the elements it tries to access have been added/initialized or cause similar sequence issues. And it is the last event of a Form opening for the first time.

Once you do use the same code in 2 places, it is usually beneficial to have a subfunction for this. Some like "update_dan()" perhaps? You can then call that function in the click and/or shown event handler.

Christopher
  • 9,634
  • 2
  • 17
  • 31
0

When I start my program, date should be printed on label named dan.Text automatically but it prints only when I make mouse click input on GUI

This is because you only put code in the private void dan_Click(object sender, EventArgs e) event, it will only ever work in this event.

Firstly, in your code, you have wrapped an assignment in Console.WriteLine and it's not like you think it would. What's happening is the assignment does happen, but then the Console.WriteLine happens which is the result of dan.Text.

Your button text would look like:

 DDDD/10/YYYY

Not what I think you'd want; it doesn't even print out a DateTime. I think wrapping the whole thing is a type-o to be honest, just remove it. So it would look like this now:

 da.Text = DateTime.Now.ToString("DDDD/MM/YYYY");

If you want to assign something to the dan.Text property at load time, there are more than a few event's to do this as already mentioned.

One recommendation that hasn't been mentioned is overriding the OnLoad event. This would look like:

 protected override void OnLoad( EventArgs e)
 {
    base.OnLoad(e);
    // do something here?
 }

Using the event is only appropriate when another class would be interested in the event. Which is what events are for. Another class being interested in the Load event is very rare, only really useful to do window arrangement stuff.

Still, the Load event works well with the designer and programmers are very comfortable with it. It isn't horribly wrong, you'd only get in trouble when you start inheriting the form and code doesn't run in the right order.

Most code that now gets put in the Load event really belongs in the constructor.

On a final note about the DateTime.Now.ToString("DDDD/MM/YYYY"), it's not even a valid format. To see what ones are, please see there and or here.

References: Form_Load() 'event' or Override OnLoad()

Trevor
  • 7,777
  • 6
  • 31
  • 50
-1

We rarely refer to a console applicaiton as a GUI, but sure I will bite.

Move your Console.WriteLine out of the "CLICK" event and into the load event.

N_tro_P
  • 673
  • 5
  • 15