c#


How to make a Form load before opening another?


I am creating a C# application that opens multiple forms at once. This one currently works by opening the forms all first, THEN loading their content. How do I force the form to open, load its content, then open the next?
The repeat action is what triggers multiple forms opening at once.
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ShowForms();
Application.Run();
}
static void ShowForms()
{
Random random = new Random();
int formCount = 0;
RepeatAction(5, () =>
{
formCount++;
int x = random.Next(0, 1000);
int y = random.Next(0, 1000);
Form1 form = new Form1
{
StartPosition = FormStartPosition.Manual,
ShowInTaskbar = false,
Location = new Point(x, y)
};
form.FormClosed += (sender, e) =>
{
if (--formCount > 0)
{
return;
}
Application.ExitThread();
};
form.Show();
});
}
form.Show(); causes to show the form. So either make RepeatAction to a sequential loop or keep an array of forms and then iterate through the array and call the form.show() function.
Instead of a simple loop, try creating a callback method that you give to each form when it's instantiated, that calls back to your main logic from the end of its Load event (or whatever handles form population). The callback will then keep track of how many forms have been created and terminate the "loop".
It might look something like:
static volatile int formCount = 0;
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ShowForms(5);
Application.Run();
}
static void ShowForms(int formsLeft)
{
if(formsLeft == 0) return;
Random random = new Random();
int x = random.Next(0, 1000);
int y = random.Next(0, 1000);
Form1 form = new Form1
{
StartPosition = FormStartPosition.Manual,
ShowInTaskbar = false,
Location = new Point(x, y),
LoadCallback = ()=>ShowForms(formsLeft - 1);
};
form.FormClosed += (sender, e) =>
{
if (--formCount > 0)
{
return;
}
Application.ExitThread();
};
formCount++;
form.Show();
}
You would then need to add a LoadCallback property to Form1, of type Action, or a custom named delegate that takes no parameters and returns void. You then simply call LoadCallback() at the end of whatever method populates your form, and it comes back to this code with one fewer form to create, until there are none.

Related Links

How do you get CSLA 3.02 BusinessListBase ListChanged event to identify the child object whose OnPropertyChanged triggered the event?
Design a combo box with disabled combo-box items [closed]
Reading a String as an int pointer
Web browser print from service running on windows 2012
How to publish Umbraco from Visual Studio without getting the install wizard
Should I use array or arraylist/list<>
How to access google analytics data without username/password
Changing signature appearance after signing pdf file with iTextSharp
C# Set ListView item selected after inserting a new Item in winform
c# treeview to xml error [duplicate]
Use OverlayIdentifiers icons in namespace extension
Data isn't being updated in Entity Framework
What is best practice for reading all bytes from tcp socket in windows phone?
Create Thread that wait until var change
WPF list view's grid view area mouse capture
Applying bitwise or operator to decimal in c#

Categories

HOME
spring-boot
drupal
gulp
http
kendo-ui
alasql
service
ldap
snappy
windows-server-2008
skypedeveloper
azure-database-mysql
livecharts
watson-iot
x509certificate
singleton
opengl-es
vsixmanifest
sahi
ndepend
point-of-sale
captcha
eclipse-orion
loader
beacon
accordion
hl7-fhir
sonarlint
inline-assembly
functional-dependencies
ppp
android-wear-2.0
drombler-fx
csproj
pypy
xlib
jboss-arquillian
intermec
wordpress-rest-api
slim-lang
availability
microstrategy
traitsui
fastadapter
javax
repeat
border-layout
hippocms
multi-user
matrix-multiplication
redhat-brms
nsdateformatter
gem-fury
usernotifications
icu
autodesk-data-management
piranha-cms
anonymous-function
yank
text-formatting
opos
webmatrix
codesniffer
cardview
timefield
qtruby
salesforce-service-cloud
taverna
personality-insights
exchange-server-2007
properties-file
p4python
tokudb
pdcurses
stereotype
transparentproxy
gmaps4jsf
autopep8
beagleboard
alpha-beta-pruning
cordova-3
aiff
frisby.js
mind-manager
famous-engine
redmine-plugins
selenium-grid2
triplestore
telerik-appbuilder
hotlinking
yandex-api
opencobol
elixir-framework
cvi
pthreads-win32
xcode6.1-gm-seed
usersettings
msbuild-task
ncml
screensharing
orchestration
github-archive
nsscrollview
mcpd
method-call
azure-appfabric
user-preferences
html-help-workshop
unauthorized

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App