Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi again all,

Seeking some more help.

I have a Label on my windows form that I would like to change when a procedure from a class is called.

C#
private void StartCmd_Click(object sender, EventArgs e)
{
     Library lib = new Library();
     lib.StopService();
         lib.DeletePrefs();
}


In the class I have the following under DeletePrefs

C#
public void DeletePrefs()
{
    try
    {
    fmMain F = new fmMain();
    String StrFile;
    StrFile = @"C:\Program Files\X\Workspace\prefs.txt";
              
     if (File.Exists(StrFile))
     {
          F.pFileStatus.ForeColor = System.Drawing.Color.Black;
          File.Delete(StrFile);
          F.pFileStatus.Text = "Deleted";
                    
     }
     catch (Exception ex)
            {
                string strMessage = ex.Message;
MessageBox.Show("An error occured deleting prefs file " + strMessage, "Delete                      File Process", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
}


When the code executes it works fine, However the label never updates.
Can someone please explain how to reference Windows Forms in C# Classes.

Thanks kindly
Posted
Updated 23-Jun-14 11:07am
v2
Comments
[no name] 23-Jun-14 17:24pm    
Well... just like you are doing, as unbelievable as that might seem. The actual problem that you are having is references. The line "fmMain F = new fmMain();" creates a brand new never seen before instance of your form, then you do stuff to it, your instance goes out of scope and the form is destroyed. You never see the changes. Probably the easiest thing for you to do is to pass an existing instance of your form to your library and then do the changes on that reference.
FMG Tech 23-Jun-14 17:55pm    
Hey again Wes, thanks for your speedy reply. So I know my questions are stupid a week into this.
Passing the form to the library is would like this?
fmMain F = fmMain();

instead of having new prior to fmMain();

Better way is to return information from your library to main program/form. Then update controls inside form. Doing this way you have loosely coupled architecture, and you can separate your layers. Libray doesn't need to know about your forms which and you can reuse your Library code. Try to use this simple approach:

C#
public bool DeletePrefs()
{
    try
    {
        bool result = false; // Default method result
        String StrFile = @"C:\Program Files\X\Workspace\prefs.txt";
        if (File.Exists(StrFile))
        {
            File.Delete(StrFile);
            result = true;
        }
    }
    catch (IOException ex)
    {
        // We are catching only IO Exceptions
    }
    catch (...) // If you are expecting other exceptions inside this method you should handle them here.
    {

    }
    finally
    {
        // Optional clean up
    }
    return result;
}


And calling your class should be:
C#
private void StartCmd_Click(object sender, EventArgs e)
{
    Library lib = new Library();
    lib.StopService();
    if (lib.DeletePrefs())
    {
        pFileStatus.ForeColor = System.Drawing.Color.Black;
        pFileStatus.Text = "Deleted";
    }
    else
    {
        MessageBox.Show("Error while deleting prefs file");
    }
}



If you need to return additional error informations you can create your custom exceptions or rethrow standard exceptions to inform your library consumer about it.

More informations you can find here:
http://msdn.microsoft.com/en-us/library/vstudio/ms229042%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/vstudio/ms229014%28v=vs.100%29.aspx[^]

I hope you find this useful :)
 
Share this answer
 
v2
First or all, you created a new instance of the fmMain. You're modifying a copy of the form, not the one that you see on screen.

Secondly, your library should NOT be concerned with changing any label or other control. The code in the fmMain class is responsible for that. If you continue with what you're doing, your Library class is completely dependent on the existence of fmMain. You will not be able to use the class in any other form or even any other project because of that.

What you're library should be doing is exposing the data it's trying to send to the form through events or other delegates. Then it's up to the form to subscribe to those events and decide what data to show and how to show it.
 
Share this answer
 
v2
Comments
FMG Tech 23-Jun-14 17:38pm    
Understood. I guess my question would be now change to if Im calling the DeletePrefs on the button click should I code under it to see if file exists and if not deliver the label as deleted?

I appreciate all your help. Trying to read this C# book and browse MSDN site to make all this stuff work correctly.

-The Noob
Your form class should implement an interface named IHost that should have at least one method named RefreshAdterDelete() and then the logic class will communicate with the form by using this interface, so your logic class should have a property or a param of its constructor of type IHost and after delete it has to invoke the method of the interface.
 
Share this answer
 
What you are doing is create a new form object in the delete procedure that will now update the existing form
you can find the solution in multiple ways
but i will suggest two ways
way 1:
Create a Delete event in the library class and access it from the form

way 2
pass the form object as a reference Argument to the delete procedure using the param object you can access the form existing form
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900