Setting a controls properties from another form

  • Thread starter Thread starter surfrat_
  • Start date Start date
S

surfrat_

Hi,

I have a project that loads a splash screen while initialization takes
place. On the splash form is a label that I want to update with
messages on what is happening with the initialization. Before the main
form is loaded I show the splash screen and then try to update the
splash label to indicate the status. I cannot figure out the correct
syntax.

Here is a code snippet. My last line generates a
"Error 1 'WeatherStation.formSplash.labelStartupStatus' is inaccessible
due to its protection level". How do I fix this?

Thanks

SurfRat.

namespace WeatherStation
{

public partial class formWeatherStation : Form
{

// WeatherStation Constructor
/// <summary>
/// WeatherStation Constructor.
/// </summary>
public formWeatherStation()
{
InitializeComponent();
InitializeWeatherStation();
}

// InitializeWeatherstation
/// <summary>
/// Checks Weatherlink.Dll and com port access.
/// </summary>
public void InitializeWeatherStation()
{
// Instantiate a Splash screen and show it.
formSplash SplashScreen = new formSplash();
SplashScreen.Show();
// Force a refresh so text shows correctly.
SplashScreen.Refresh();

// Try to access Weatherlink.dll and catch any errors.
formSplash.labelStartupStatus.Text = "Hello";
 
Hi,
There are a couple of ways you could do what is required.
1> Write a method inside the splashscreen form which takes a string as
an argument. Inside that method you could set the Label.Text property
2> Alternatively you could use an event. Pass the string to be
displayed as a eventarg and set the the Label.Text property inside the
eventhandler.
hope this helps...
 
Hi,

'WeatherStation.formSplash.labelStartupStatus' is inaccessible due to its
protection level" means that labelStartupStatus isn't visible to
formWeatherStation. Protection level is the private, protected, public in
front labelStartupStatus. Try setting it to public. Instead of accessing
the control of another form directly you should go through a property or
method

public String StartupStatus
{
get
{
return labelStartupStatus.Text;
}
set
{
labelStartupStatus.Text = value;
}
}

or

public SetStartupStatus(string status)
{
labelStartupStatus.Text = status;
}

You may find the splash screen not updating as you want it to, in which
case Application.DoEvents(), threading, invoking, delegates and events are
keywords to look up on.

The article below demonstrates how you can use threading to create a
splash screen.

[A Pretty Good Splash Screen in C#]
http://www.codeproject.com/csharp/PrettyGoodSplashScreen.asp
 
Hi,

Thanks all for your suggestions and help. How do I declare a label as
public?

Thanks

SurfRat.
 
Do global search in your project for

private Label labelStartupStatus

change "private" to "public", that'll do it.

Thank you
 
Hi,

I got it sorted. Thanks to all for help. I set labelStartupStatus to
public as suggested which worked. I also tried the class properties
method with the following:

public string StartupStatusText
{
set
{
labelStartupStatus.Text = value;
}
}

This worked and is the method I settled on. Now working on getting my
splash screen to refresh properly :)

Thanks

SurfRat.
 
Back
Top