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. Interaction between a Form and a Class

Interaction between a Form and a Class

Scheduled Pinned Locked Moved C#
helptutorial
2 Posts 2 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.
  • J Offline
    J Offline
    JJF007
    wrote on last edited by
    #1

    I have a form where i call a class with and start a subroutine Class1 Class1 = Class1(); Class1.StartSub(); In my Mainform from where i call the class there is for example a label called "label1". Now i want to change the label1 from within the Class Subroutine ... I tried following: in the MainForm (Form1) set the label from private to public and then in the Class: Form1 Form1 = new Form1(); Form1.label1.text = "test"; It compiles without any error but it doesn't change the label. Thanks for you help!

    K 1 Reply Last reply
    0
    • J JJF007

      I have a form where i call a class with and start a subroutine Class1 Class1 = Class1(); Class1.StartSub(); In my Mainform from where i call the class there is for example a label called "label1". Now i want to change the label1 from within the Class Subroutine ... I tried following: in the MainForm (Form1) set the label from private to public and then in the Class: Form1 Form1 = new Form1(); Form1.label1.text = "test"; It compiles without any error but it doesn't change the label. Thanks for you help!

      K Offline
      K Offline
      KingTermite
      wrote on last edited by
      #2

      When you created the new Form1(), you created a whole new instantiation of that form in memory, so you changed HIS label and not the one that is visible. You could tackle that a couple of ways. You could put in the Class1's constructor an argument for a label like this Class1(label FormsLabelIWantToChange) { FormsLabelIWantToChange = "test"; } Another way to do it (I think) would be to make Form1's label member s public static, then as you are in the same namespace you could do something like this in your class: Form1.label1.text = "test";


      There are only 10 types of people in this world....those that understand binary, and those that do not.

      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