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

Asp.net WebAPI XML deserialize?
What are differences in purposes and contents between ECMA-335 and ISO/IEC 23271:2012 as CLI standards?
convert long binary data in database access to image using c#
touch control to move cube (in an array that generates them) left and right
DataGrid displaying content value of 'System.Data.DataRowView'
Selenium does not execute click command
Check whether array is in another array, but in order and elements can repeat [duplicate]
Cannot get ClickOnce app to deploy if I pass arguments
Exception thrown: 'System.ApplicationException' in renderdocui.exe
Dynamic drawing multiple shapes - WinForm [duplicate]
OData $expand Contains Incorrect Context Link
Using JSON.NET to read a dynamic property name
Animating a Control using a sine wave
Find which key a user is pressing without form focus
Apply permissions on WPF UI controls
Ignore SOAP Header In Response

Categories

HOME
google-cloud-pubsub
redux
microcontroller
nltk
pandoc
overrides
download
procmon
pip
wowza
dynamics-crm-2011
mbed
lambda-calculus
opendaylight
game-engine
wallet
aws-devicefarm
xforms
sonarlint
opengraph
functional-dependencies
struts
android-xml
jira-zephyr
akavache
android-cursorloader
gulp-jshint
typeorm
request-uri
osgi-bundle
vtd-xml
language-detection
kubectl
android-scrollview
imageprocessor
fastadapter
apache-falcon
machine-code
stdin
cowboy
keychain
file-descriptor
vpython
cakephp-3.2
spring-ide
infovis
text-formatting
flask-socketio
noise
playframework-2.3
photoshop-script
typewriter
timex
acceleo
ownership
stripes
mongodb-3.3
joystick
bytearray
kik
pyshark
paypal-subscriptions
nonblocking
overlapping
kramdown
ios8-share-extension
php-gd
identifying
joomla3.3
skview
maven-archetype
teamstudio-unplugged
go-gin
gnip
nsnotificationcenter
indic
time-frequency
behance-api
twitter-finagle
django-tests
codio
imaplib
thucydides
google-voice
cab
ojb
nsnumber
select2-rails
microsoft-speech-platform
vline
setcookie
moonscript
webpage-screenshot
xcode4.6.3
databus
dancer
scriptmanager
pex-and-moles
youtube.net-api
sessiontracking
norton
viewdata
mdd
stackless

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