Passing values from one class to another class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Janice Hui
    New Member
    • Feb 2010
    • 16

    Passing values from one class to another class

    Hello,

    I am using the wpf and c# to do my system. In my system, i have two different classes which the first class is the main window, consists of tabcontrol and two tab items named tab1 and tab2. Another class is used to create a node in one of tabitem. The problem is i want to get the value of current tabitem opened in first class whenever a node is called to be created in second class. For example, If the tabitem is tab1, therefore the user is allowed to create the node.
    Is that any ways for me to do that.

    Thank you!
  • regalis
    New Member
    • Mar 2010
    • 9

    #2
    I hope i got it right!?
    You want to check if the tabpage1 is selected and if so enable the user to create a node (on a treeview or what?)

    Code:
    if(tabControl1.SelectedTab == tabPage1)
        makeNode();
    else
        NoNode();

    Comment

    • Janice Hui
      New Member
      • Feb 2010
      • 16

      #3
      Thank regalis for your reply,

      By the way, i am using the wpf to bulid the system. This system is allowed user to create two different models in each of the tabitem. The model is created using the drop and drag symbols provided on the tabitem surface.

      Code:
      <TabControl x:Name="tabControl1">
                      <TabItem Name="tab1" IsSelected="True">
                          <ms:Surface Name="ds" />
                      </TabItem>
                      <TabItem Name="tab2">
                          <ms:Surface Name="ds1"/>
                      </TabItem>
                  </TabControl>
      Code:
      public partial class Window1 : Window
      {
          public Window1()
          {
               InitializeComponent();
               .....
          }
      }
      Then, another class is used to create the node as below:


      Code:
      public class ProcessNode : NodeBuilder
          {
              public void CreateNode()
              {
                            
                  //function to create the node.
                 
                 //Here, i want to determine which tabitem is currently drag, 
                   if the surface is in tab1, the following function will be go on.
      
                 if (ab == tab1)
                 {
                     diagram.Nodes.RemoveAt(a - 1);
                 }
              }
          }
      Is that any way for me to get the current tabitem from Window1? Thank you!

      Comment

      • regalis
        New Member
        • Mar 2010
        • 9

        #4
        I don't know WPF i'm using WinForms to place my controls (i'm at the beginning using C#)
        But i think it should work if you qualify the fullname:
        Code:
        String ab = Window1.tabControl1.SelectedTab.Name;
        if (ab == "tab1")....
        If that isn't working i can't help you :(

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Originally posted by OriginalPoster
          How do I get my Form2 to react to something on my Form1?
          How do I make my Form1 control something on my Form2?
          Although you can have Form1 directly access items on Form2 it isn't the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.
          It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.
          It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.
          Events tutorial (including Form to Form which is the same as class to class)
          This tutorial for a cash register does exactly that: It makes a virtual numeric keyboard.
          Bad: Directly accessing controls of one class/form from another.
          Code:
          bool IsOn = Form2.button1.IsChecked;


          Good: Use a property to get such information
          Code:
          //Form1
          bool IsOn = Form2.IsOn;
          Code:
          //Form 2
          public bool IsOn
          {
             get { return button1.Checked; }
             set { button1.Checked = value; }
          }
          It's a subtle but important difference as your applications become more complex. Using properties means your target class/form (Form2) can be changed and updated a thousand different ways yet won't negatively impact the classes that are reading from it. If you change the name of a control for example: No break in all your other classes. If your target form evolves where it needs to do 10 things when it is turned on, then the responsibility stays within Form2, and that burden on not put on Form1 and a dozen other forms that might be using Form2. The goal is to compartimentali ze the work so that Form2 is responsiblity SOLELY for Form2. From1 should only have to say "Turn on" or "Turn Off"

          Code:
          Form2
          public bool IsOn
          {
             get { return btnMeaningfulName.Checked; }
             set {
                      btnMeaningfulName.Checked = value;
                      panelDashboard.Visible = value;
                      labelStatus = value == true ? "On" : "Off";
                      btnRunNow.Enabled = value;
          }
          Now when Form1 tells Form2 to turn on, Form2 will check a box, make an entire panel visible, change its Status label to say 'On' and enable a Run Now button.

          Comment

          Working...