473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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!
Mar 21 '10 #1
4 7658
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?)

Expand|Select|Wrap|Line Numbers
  1. if(tabControl1.SelectedTab == tabPage1)
  2.     makeNode();
  3. else
  4.     NoNode();
  5.  
Mar 21 '10 #2
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.

Expand|Select|Wrap|Line Numbers
  1. <TabControl x:Name="tabControl1">
  2.                 <TabItem Name="tab1" IsSelected="True">
  3.                     <ms:Surface Name="ds" />
  4.                 </TabItem>
  5.                 <TabItem Name="tab2">
  6.                     <ms:Surface Name="ds1"/>
  7.                 </TabItem>
  8.             </TabControl>
Expand|Select|Wrap|Line Numbers
  1. public partial class Window1 : Window
  2. {
  3.     public Window1()
  4.     {
  5.          InitializeComponent();
  6.          .....
  7.     }
  8. }
Then, another class is used to create the node as below:


Expand|Select|Wrap|Line Numbers
  1. public class ProcessNode : NodeBuilder
  2.     {
  3.         public void CreateNode()
  4.         {
  5.  
  6.             //function to create the node.
  7.  
  8.            //Here, i want to determine which tabitem is currently drag, 
  9.              if the surface is in tab1, the following function will be go on.
  10.  
  11.            if (ab == tab1)
  12.            {
  13.                diagram.Nodes.RemoveAt(a - 1);
  14.            }
  15.         }
  16.     } 
Is that any way for me to get the current tabitem from Window1? Thank you!
Mar 21 '10 #3
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:
Expand|Select|Wrap|Line Numbers
  1. String ab = Window1.tabControl1.SelectedTab.Name;
  2. if (ab == "tab1")....
If that isn't working i can't help you :(
Mar 21 '10 #4
tlhintoq
3,525 Expert 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. bool IsOn = Form2.button1.IsChecked;


Good: Use a property to get such information
Expand|Select|Wrap|Line Numbers
  1. //Form1
  2. bool IsOn = Form2.IsOn;
Expand|Select|Wrap|Line Numbers
  1. //Form 2
  2. public bool IsOn
  3. {
  4.    get { return button1.Checked; }
  5.    set { button1.Checked = value; }
  6. }
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 compartimentalize the work so that Form2 is responsiblity SOLELY for Form2. From1 should only have to say "Turn on" or "Turn Off"

Expand|Select|Wrap|Line Numbers
  1. Form2
  2. public bool IsOn
  3. {
  4.    get { return btnMeaningfulName.Checked; }
  5.    set {
  6.             btnMeaningfulName.Checked = value;
  7.             panelDashboard.Visible = value;
  8.             labelStatus = value == true ? "On" : "Off";
  9.             btnRunNow.Enabled = value;
  10. }
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.
Mar 21 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Garma | last post by:
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so? Sometimes some objects or their pointers have to be shared among...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
by: Ron Rohrssen | last post by:
I want to show a dialog and when the form (dialog) is closed, return to the calling form. The calling form should then be able to pass the child form to another object with the form as a...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
3
by: Joe Bloggs | last post by:
Does anyone know if its possible to pass parameters or the values of Request.QueryString from a web page to a custom control class? I'm using a C# Web Application. For Example I have Web Page1...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
8
by: JJ | last post by:
Hi, What's the preferred way to pass variables around to different pages now? Or if my reading servers me right they are retained in memory for the life of the app, correct? How do I access...
10
by: Stan | last post by:
There are two ways to pass structured data to a web service: xml === <Order OrderId="123" OrderAmount="234" /> or class =====
5
by: Markus Ernst | last post by:
Hello A class that composes the output of shop-related data gets some info from the main shop class. Now I wonder whether it is faster to store the info in the output class or get it from the...
3
by: Buckaroo Banzai | last post by:
Hi, I just started learning JAVA and I'm really NOT sure how to manipulate values in different classes. for example. I have a class 'class1' which updates some variables for its own use. then I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.