Where should a new form be initialized?

glasswizzard

Well-known member
Joined
Nov 22, 2019
Messages
126
Programming Experience
Beginner
So I want a second form initialized and ready to show using "Form2 frmForm2 = new Form2();". Where is the best place to put this line?

Thanks
 
Instantiating a form like you are doing above, only creates an instance. It does not show it.
 
Where does the documentation suggest you put this line?

A better question. What is that line used for?

If you have form1 and form2. If form1 does not let you access form2's objects, what do you think you need to do?

Surely if you know how to instantiate an object, you know how and where to use it.
 
Generally speaking, you would create a form immediately before displaying it. Assuming that there's no reason to do otherwise in this case, you would put that line of code immediately before the line that will display it, e.g. in the Click event handler of a Button if you want to display the form when the user clicks the Button. If you want to do something other than that in this case, that suggests that you think you have a reason to. Can you explain what that reason is? It may well be that what you're trying to do is misguided. The form must be created on the UI thread so, if it takes time to perform the creation, you're still going to hold up the UI regardless of when you do it. If the issue is retrieving data or the like then that is something that you can split off and do on a secondary thread. Without some context, it's impossible to say what might be the best solution without guessing.
 
Generally speaking, you would create a form immediately before displaying it.
Do you not think this topic self-evidently answers itself rhetorically; and the person posting knows how to instantiate, and in which case; likely knows where to use the instantiated object too. The topic is eliciting the obvious from the perspective of the OP in my opinion. I feel trolled even answering it. ?
 
Do you not think this topic self-evidently answers itself rhetorically; and the person posting knows how to instantiate, and in which case; likely knows where to use the instantiated object too. The topic is eliciting the obvious from the perspective of the OP in my opinion. I feel trolled even answering it. ?
It sounds to me like the OP wants to create a form at some point and then display it at some later point. If that is the case then it is likely misguided. I'd like to establish whether that is what they want to do and why they think it's a good idea.
 
Hi, sorry for not being clear. The reason I want Form2 to be instantiated upon the start of the program is because there is a control (a listbox) on form2 that will be receiving data and being populated from a control on form1 ( a textbox), when Form2 is eventually displayed I want all the information from the listbox in Form2 to be visible, otherwise (I believe) the listbox will only become active and receiving data from Form1 when Form2 is shown. I want Form2 to be fully active and functioning in the background until it the user wants to display it.

So it's just so the data can be gathered in the background until it's shown, so where would be best/appropriate place for it to be instantiated?

And for anyone thinking I was trolling, I'm sorry I didn't word my question appropriately.
 
What you should be doing is adding the data from Form1 to a list of some sort and then populating the ListBox from that when Form2 is created and displayed.

And when Form2 is shown, I keep using the list to populate the listbox? The listbox will need to keep having things added to it constantly even after Form2 is shown. If a list is the thing to use, then a list it is. Thanks.
 
Since form2 doesn't need to be showing straight away. John is suggesting that you add a list<T> to form1 or a storage class instead. Populate the list<T>, and when you are ready to use form2 you can instantiate it, and then transfer all of the the values in your list<T> to the form2 listbox.

Note if the user does not need to interact with the list box on form2 you should simply just use a list<T> instead.
 
It's hard to know the best course of action without knowing a bit more about the specifics of the situation. One option might be to use a BindingList<T> in Form1 that you then pass to Form2 and bind to the ListBox. You can then continue to add items to the list in the first form and they will be automatically displayed in the second form.
 
OK I'll give some more specifics, Form1 has a TextBox, Form2 (the history form) has a ListBox, the TextBox will send it's text string when it is updated (after one second of inactivity) and the ListBox will be updated with the text as a new item, ie the listbox is just a history of what has been typed into the text box. The history form will be shown by pressing a button, so the user may not even ever want to see the history, but I need the history to always be kept up to date, whether the user cares about it or not. If the user does show the history form I want them to be able to double click on one of the listbox items and have that sent to the textbox.

Hopefully there's enough information there, I can't think of anything else that would be helpful but let me know if there is.
 
In that case, do as I suggested. Have the first form keep its own history and then pass it to the second form if and when it is opened. As I said, if you use a BindingList<T> then you can just continue to add items in the first form and any bound control(s) will update automatically. I'd suggest adding a constructor to the second form that takes the BindingList<T> as an argument and binds it to the ListBox. You should unbind in the FormClosed event handler.
 
Back
Top Bottom