Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. VS2008 and Updating controls ? [modified]

VS2008 and Updating controls ? [modified]

Scheduled Pinned Locked Moved C#
helpquestionannouncement
13 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mike Bluett
    wrote on last edited by
    #1

    PROBLEM: The problem I am having is, given the layout of the Microsoft template for developing a Windows App, I need to know what is the best way of implementing a method to make modifications to a control on my main form (Form1). NECESSARY BACKGROUND: A basic Windows program comes up with a Form1.cs and a Program.cs (I have renamed Program.cs to FileOrganizer.cs). In FileOrganizer.cs there is a Main method. Within this Main method is "Application.Run(new Form1());". What I want to do (within another method of the FileOrganizer class) is to update a control programmatically. The fact that Form1 is "run" the way it is (i.e., using "Application.Run"), makes it so there is no reference to an object. Because there is no way to reference the Form1 object I don't know of a way to modify one of the controls on Form1. If I create a global reference to Form1 and then create a "new" instance of Form1 (within Main) I receive an error ("Error 4 An object reference is required for the non-static field, method, or property 'FileOrganizer.FileOrganizer.mainForm'"). My implementation is as follows: namespace FileOrganizer { public class FileOrganizer { private Form1 mainForm; /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); mainForm = new Form1(); Application.Run(mainForm); I understand that this error occurs because the Main method is static although I don't understand why. One way of making this work is by doing the following: public class FileOrganizer { private Form1 mainForm; /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FileOrganizer fg = new FileOrganizer(); } public FileOrganizer() { mainForm = new Form1(); mainForm.Visible = true;But I am not sure if this is the proper way to do what I need to do (i.e., provide a way to modify a control on Form1 in another method of the FileOrganizer class).

    N D M 3 Replies Last reply
    0
    • M Mike Bluett

      PROBLEM: The problem I am having is, given the layout of the Microsoft template for developing a Windows App, I need to know what is the best way of implementing a method to make modifications to a control on my main form (Form1). NECESSARY BACKGROUND: A basic Windows program comes up with a Form1.cs and a Program.cs (I have renamed Program.cs to FileOrganizer.cs). In FileOrganizer.cs there is a Main method. Within this Main method is "Application.Run(new Form1());". What I want to do (within another method of the FileOrganizer class) is to update a control programmatically. The fact that Form1 is "run" the way it is (i.e., using "Application.Run"), makes it so there is no reference to an object. Because there is no way to reference the Form1 object I don't know of a way to modify one of the controls on Form1. If I create a global reference to Form1 and then create a "new" instance of Form1 (within Main) I receive an error ("Error 4 An object reference is required for the non-static field, method, or property 'FileOrganizer.FileOrganizer.mainForm'"). My implementation is as follows: namespace FileOrganizer { public class FileOrganizer { private Form1 mainForm; /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); mainForm = new Form1(); Application.Run(mainForm); I understand that this error occurs because the Main method is static although I don't understand why. One way of making this work is by doing the following: public class FileOrganizer { private Form1 mainForm; /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FileOrganizer fg = new FileOrganizer(); } public FileOrganizer() { mainForm = new Form1(); mainForm.Visible = true;But I am not sure if this is the proper way to do what I need to do (i.e., provide a way to modify a control on Form1 in another method of the FileOrganizer class).

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      Mike Bluett wrote:

      update a control programmatically.

      What do you mean? What are trying to accomplish? Typically you would create a method in the form class to call and manipulate the control from there.


      only two letters away from being an asset

      M 1 Reply Last reply
      0
      • M Mike Bluett

        PROBLEM: The problem I am having is, given the layout of the Microsoft template for developing a Windows App, I need to know what is the best way of implementing a method to make modifications to a control on my main form (Form1). NECESSARY BACKGROUND: A basic Windows program comes up with a Form1.cs and a Program.cs (I have renamed Program.cs to FileOrganizer.cs). In FileOrganizer.cs there is a Main method. Within this Main method is "Application.Run(new Form1());". What I want to do (within another method of the FileOrganizer class) is to update a control programmatically. The fact that Form1 is "run" the way it is (i.e., using "Application.Run"), makes it so there is no reference to an object. Because there is no way to reference the Form1 object I don't know of a way to modify one of the controls on Form1. If I create a global reference to Form1 and then create a "new" instance of Form1 (within Main) I receive an error ("Error 4 An object reference is required for the non-static field, method, or property 'FileOrganizer.FileOrganizer.mainForm'"). My implementation is as follows: namespace FileOrganizer { public class FileOrganizer { private Form1 mainForm; /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); mainForm = new Form1(); Application.Run(mainForm); I understand that this error occurs because the Main method is static although I don't understand why. One way of making this work is by doing the following: public class FileOrganizer { private Form1 mainForm; /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FileOrganizer fg = new FileOrganizer(); } public FileOrganizer() { mainForm = new Form1(); mainForm.Visible = true;But I am not sure if this is the proper way to do what I need to do (i.e., provide a way to modify a control on Form1 in another method of the FileOrganizer class).

        D Offline
        D Offline
        Dragonfly_Lee
        wrote on last edited by
        #3

        Do not get it. Can you clarify the question again and tell us what you were trying to do in a sentence?

        LuckyBoy

        M 1 Reply Last reply
        0
        • D Dragonfly_Lee

          Do not get it. Can you clarify the question again and tell us what you were trying to do in a sentence?

          LuckyBoy

          M Offline
          M Offline
          Mike Bluett
          wrote on last edited by
          #4

          This sentence was copied from my previous post: "What I want to do (within another method of the FileOrganizer class) is to update a control programmatically." For example, I want to add some nodes to a TreeView control. So, this is what I want to do; however, you will not be able to respond with an appropriate answer unless you read through my previous post entirely.

          D 1 Reply Last reply
          0
          • M Mike Bluett

            This sentence was copied from my previous post: "What I want to do (within another method of the FileOrganizer class) is to update a control programmatically." For example, I want to add some nodes to a TreeView control. So, this is what I want to do; however, you will not be able to respond with an appropriate answer unless you read through my previous post entirely.

            D Offline
            D Offline
            Dragonfly_Lee
            wrote on last edited by
            #5

            Mike Bluett wrote:

            For example, I want to add some nodes to a TreeView control.

            If you want to do this, why donot you put the update control method in the mainform constructor? Maybe I misunderstood you question. However, this is not common to initialize the control in this way for windows form app.

            LuckyBoy

            M 1 Reply Last reply
            0
            • D Dragonfly_Lee

              Mike Bluett wrote:

              For example, I want to add some nodes to a TreeView control.

              If you want to do this, why donot you put the update control method in the mainform constructor? Maybe I misunderstood you question. However, this is not common to initialize the control in this way for windows form app.

              LuckyBoy

              M Offline
              M Offline
              Mike Bluett
              wrote on last edited by
              #6

              Maybe I wasn't clear enough. This is not about initialization. This is about modifying controls after initialization. Controls should be able to be modified anywhere within the program and at any time while the program is running.

              N 1 Reply Last reply
              0
              • N Not Active

                Mike Bluett wrote:

                update a control programmatically.

                What do you mean? What are trying to accomplish? Typically you would create a method in the form class to call and manipulate the control from there.


                only two letters away from being an asset

                M Offline
                M Offline
                Mike Bluett
                wrote on last edited by
                #7

                I have updated my original posting in order to try and make it more clear. Hope this helps.

                N 1 Reply Last reply
                0
                • M Mike Bluett

                  Maybe I wasn't clear enough. This is not about initialization. This is about modifying controls after initialization. Controls should be able to be modified anywhere within the program and at any time while the program is running.

                  N Offline
                  N Offline
                  Not Active
                  wrote on last edited by
                  #8

                  Yes, however, you cannot update a control from a thread other than the main UI thread. A well documented fact of Windows UI work. There are plenty of examples, here and elsewhere, about adding nodes to a treeview, I suggest you do a little research.


                  only two letters away from being an asset

                  1 Reply Last reply
                  0
                  • M Mike Bluett

                    I have updated my original posting in order to try and make it more clear. Hope this helps.

                    N Offline
                    N Offline
                    Not Active
                    wrote on last edited by
                    #9

                    As I said before, the typically you would expose a method on the form to update and manipulate the control. This is an advanced topic that seems to be above your level of skill, I suggest you do more research and learn more about forms and controls before you continue.


                    only two letters away from being an asset

                    M 1 Reply Last reply
                    0
                    • N Not Active

                      As I said before, the typically you would expose a method on the form to update and manipulate the control. This is an advanced topic that seems to be above your level of skill, I suggest you do more research and learn more about forms and controls before you continue.


                      only two letters away from being an asset

                      M Offline
                      M Offline
                      Mike Bluett
                      wrote on last edited by
                      #10

                      It should not be an advanced topic!! This is something ANY programmer would want to do with almost ANY program. Besides before posting here I have done extensive reading in all kinds of C# books and resources and cannot find an answer to my problem. Basically, I am trying to understand why Microsoft would create such a complicated approach to doing what should be incredibly simple. What I want to do is simply use a GLOBAL reference to Form1 so that I can setup an object reference to Form1 so that it can be accessed from another method within the Program class (or in my case the FileOrganizer class). I am forced to use the "Application.Run" method; otherwise, the application will quit after it finishes executing my code. The Microsoft template places the "Run" call within Main. Because Main must be static, it will not let me create a GLOBAL reference to "Form1". I can create an object reference to Form1 within Main; however, it is not GLOBAL and I cannot access it from another method in the FileOrganizer class. I am sure that Microsoft, in their infinite wisdom, provided a mechanism to accomplish this in a simple fashion; however, it is not obvious to me. I had previously written another program (which I started in SharpDevelop). In this program, all of the modifications to the main form are done through events. However, in my current program, there are no events that get fired (i.e., mouse clicks). What I am doing is reading some data from a database trying to use that data to populate a TreeView (which resides on the main form). If I could create a GLOBAL variable, this would be incredibly simple to implement. Because I can't this becomes a major complication. Why it has to be this complicated is beyond me; however, I am willing to spend the time to understand it. If you know of some code I can reference in some book or some web reference that can help me to understand this I would be very grateful.

                      N 1 Reply Last reply
                      0
                      • M Mike Bluett

                        It should not be an advanced topic!! This is something ANY programmer would want to do with almost ANY program. Besides before posting here I have done extensive reading in all kinds of C# books and resources and cannot find an answer to my problem. Basically, I am trying to understand why Microsoft would create such a complicated approach to doing what should be incredibly simple. What I want to do is simply use a GLOBAL reference to Form1 so that I can setup an object reference to Form1 so that it can be accessed from another method within the Program class (or in my case the FileOrganizer class). I am forced to use the "Application.Run" method; otherwise, the application will quit after it finishes executing my code. The Microsoft template places the "Run" call within Main. Because Main must be static, it will not let me create a GLOBAL reference to "Form1". I can create an object reference to Form1 within Main; however, it is not GLOBAL and I cannot access it from another method in the FileOrganizer class. I am sure that Microsoft, in their infinite wisdom, provided a mechanism to accomplish this in a simple fashion; however, it is not obvious to me. I had previously written another program (which I started in SharpDevelop). In this program, all of the modifications to the main form are done through events. However, in my current program, there are no events that get fired (i.e., mouse clicks). What I am doing is reading some data from a database trying to use that data to populate a TreeView (which resides on the main form). If I could create a GLOBAL variable, this would be incredibly simple to implement. Because I can't this becomes a major complication. Why it has to be this complicated is beyond me; however, I am willing to spend the time to understand it. If you know of some code I can reference in some book or some web reference that can help me to understand this I would be very grateful.

                        N Offline
                        N Offline
                        Not Active
                        wrote on last edited by
                        #11

                        Mike Bluett wrote:

                        I have done extensive reading in all kinds of C# books and resources

                        It is quite obvious your research has been lacking. It is also quite apparent you don't have sufficient understanding of Windows programming to tackle this task. It is relatively easy to do and you have been given the answers, pay attention.

                        Mike Bluett wrote:

                        I am forced to use the "Application.Run" method

                        Correct, this is the way a Windows app is started. If you had the knowledge of this environment you would understand that. Form1 (or whatever you name it) is the application, not Program!

                        Mike Bluett wrote:

                        Why it has to be this complicated is beyond me

                        Yes it is. Hire someone to do it for you.

                        Mike Bluett wrote:

                        If you know of some code I can reference

                        Have your looked here[^] Since its Christmas Eve here, maybe you can hope Santa will bring you a clue, or at least a fully functional app all wrapped up in a pretty bow for you. Otherwise, I would suggest you stop complaining and actually learn the subject.


                        only two letters away from being an asset

                        M 1 Reply Last reply
                        0
                        • N Not Active

                          Mike Bluett wrote:

                          I have done extensive reading in all kinds of C# books and resources

                          It is quite obvious your research has been lacking. It is also quite apparent you don't have sufficient understanding of Windows programming to tackle this task. It is relatively easy to do and you have been given the answers, pay attention.

                          Mike Bluett wrote:

                          I am forced to use the "Application.Run" method

                          Correct, this is the way a Windows app is started. If you had the knowledge of this environment you would understand that. Form1 (or whatever you name it) is the application, not Program!

                          Mike Bluett wrote:

                          Why it has to be this complicated is beyond me

                          Yes it is. Hire someone to do it for you.

                          Mike Bluett wrote:

                          If you know of some code I can reference

                          Have your looked here[^] Since its Christmas Eve here, maybe you can hope Santa will bring you a clue, or at least a fully functional app all wrapped up in a pretty bow for you. Otherwise, I would suggest you stop complaining and actually learn the subject.


                          only two letters away from being an asset

                          M Offline
                          M Offline
                          Mike Bluett
                          wrote on last edited by
                          #12

                          Since it is Christmas I would think you would be a little more civil than you have been!!! My research maybe lacking and that is why I came to this forum to get me on the right track, but all I have been greeted with is ARROGANCE!!! I think you should take a couple of long breaths and relax a little. It might help your composure. Merry Xmas!!

                          1 Reply Last reply
                          0
                          • M Mike Bluett

                            PROBLEM: The problem I am having is, given the layout of the Microsoft template for developing a Windows App, I need to know what is the best way of implementing a method to make modifications to a control on my main form (Form1). NECESSARY BACKGROUND: A basic Windows program comes up with a Form1.cs and a Program.cs (I have renamed Program.cs to FileOrganizer.cs). In FileOrganizer.cs there is a Main method. Within this Main method is "Application.Run(new Form1());". What I want to do (within another method of the FileOrganizer class) is to update a control programmatically. The fact that Form1 is "run" the way it is (i.e., using "Application.Run"), makes it so there is no reference to an object. Because there is no way to reference the Form1 object I don't know of a way to modify one of the controls on Form1. If I create a global reference to Form1 and then create a "new" instance of Form1 (within Main) I receive an error ("Error 4 An object reference is required for the non-static field, method, or property 'FileOrganizer.FileOrganizer.mainForm'"). My implementation is as follows: namespace FileOrganizer { public class FileOrganizer { private Form1 mainForm; /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); mainForm = new Form1(); Application.Run(mainForm); I understand that this error occurs because the Main method is static although I don't understand why. One way of making this work is by doing the following: public class FileOrganizer { private Form1 mainForm; /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FileOrganizer fg = new FileOrganizer(); } public FileOrganizer() { mainForm = new Form1(); mainForm.Visible = true;But I am not sure if this is the proper way to do what I need to do (i.e., provide a way to modify a control on Form1 in another method of the FileOrganizer class).

                            M Offline
                            M Offline
                            Mike Bluett
                            wrote on last edited by
                            #13

                            I found the answer on another forum. All I needed to do was to make the GLOBAL definition static due to the fact that the the method it is called from is static. Very simple as I expected it might be. Code as follows: public class FileOrganizer { private **static** Form1 mainForm; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); mainForm = new Form1(); Application.Run(mainForm); } It's amazing how much of a run-around I was given on this forum.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups