Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I am posting this question again because the answer I got was "too" technical for me to understand..:D My understading of concepts is very basic. I am a bog standard amateur but I am very keen to learn graphics programming. But, for now I think I need answers in simple words.;) I would truly appreciate an example code, syntax or an illustration that can help my application scenario mentioned below.

Please also let me know about the changes I should make to my code given below.

The project has two windows.

The window1 has a bunch of textboxes and gets values from the user. For example, "age, runs scored, average, highest score, etc"

When the user clicks the submit button, the window2 should open.
I've achieved this by using "window2.show(); method"

The window2 is bit of a prat! It has a bunch of arrays. I want the values entered in the textboxes of window1 to be stored in the arrays of window2.


I came up with some preposterous syntactic ideas and ended in a fiasco.

one among them was this,

C#
double[] average= new double[10];
average[0]=Window1.averagebox.text; 

I was awarded with this error "An object reference is required for the non-static field, method, or property 'project12.Window1.averagebox'"

(i am guessing it also needs a conversion from string to double ? )

So, this is what i have in window1.cs

C#
"public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
 

}
 
private void submit_Click(object sender, RoutedEventArgs e)
{
 
var Window2 = new Window2();
 
Window2.Show();
}
}"
 
And this is a part of my code from window2.cs
 
"public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
double average[10] =new double[10];
average[0]=Window1.averagebox.text; 
}
}"

I might do something with the arrays in Window2 later but my primary objective right now is to access that "average" textbox from window1 and store it in window2's array.


Please help my clumsy code..!.

Cheers !
Posted
Updated 23-Aug-11 5:14am
v2

Okay, we have some work to do!

An object reference is required for the non-static field, method, or property 'project12.Window1.averagebox


The above simply means that you do not have a reference to Window1 from Window2. You likely don't want one. The most accepted way to pass data from one class to another (without messaging) is to pass the data through the constructor.

You'll need to modify your constructor in Form2, even to something like this:
public Form2(string userText){ 
// do something with the text here, like save a copy of it
_myLocalCopyOfText = userText;
}


Next, when you create the instance of Form2 to display it, you'll do something like this (in Form1):
Form2 frm = new Form2(averagebox.Text);
frm.Show();


Another way to do it would be to expose a public method on the form, like in this MSDN article[^].

Hope this gets you started.

Cheers.
 
Share this answer
 
Comments
steersteer 23-Aug-11 11:52am    
Thank you again for your reply. I will start working on that immediately. And yeah, checked out the MVVM link ! I guess I may need a whole weekend for that E-Tome.!!!.:D..will learn..Cheers
I didn't notice the WPF tag when I first answered your question, but you're going to need to roadmap your learning ;)

You might rather want to take the MVVM approach. This is likely a 300-level topic, so I want you to bookmark this link[^] and come back to it once you get your feet wet.

The MVVM approach is important, because it will allow you to use the same data on multiple forms with very little effort.

When you get your head wrapped around MVVM, check out the MVVM Light Toolkit[^] which makes supporting MVVM much easier and gives you ways to push messages back and forth.

Cheers.
 
Share this answer
 
Comments
BobJanova 23-Aug-11 11:37am    
Giving you a 5 for this one. The problem here is that the questioner is not using a separation of UI and business and that's spoiling his day.
MrJames's second solution is on the money. Your problem derives from the fact that you are thinking of data in terms of the input control you use to edit it, and therefore get confused about editing the same data in two places. What you want to do is make the forms simply views on the data, which is represented in a data model. The view-model (the 'VM' in MVVM) sits in between the data model and the UI and essentially translates data into a form that can be data bound (it is a bit more complex than that but that will do for a brief explanation).

I'm guessing that 'window2' is an aggregate of information submitted from multiple instances of 'window1'? In which case your data model is a bit like this:

class DataModel {
 ObservableCollection<Stats> Data { get; private set; }

 public DataModel() { Data = new ObservableCollection<Stats>(); }
}

class Stats {
 public string Name { get; set; }
 public int Age { get; set; } // or consider DateTime DateOfBirth
 public int Runs { get; set; }
 // etc
}


Window2 should contain a ListView which is bound to (ItemsSource set to) Data, and the form's binding context should be the main app instance of DataModel. Window1 should be bound to (BindingContext set to) a single instance of Stats, with each field bound to (with a Text="{Binding xxx}" attribute value) one of the properties of the Stats class. All that can be done declaratively in the XAML (not sure what level your WPF/XAML skills are at but you should be able to find introductory material on WPF data binding relatively easily).

On submitting a Form1, add the new entry to the collection, and the bound list should automatically refresh.

You can use ItemTemplate to do clever stuff with the display of rows on Window2. I can't provide detailed help on that because I've never actually done clever stuff with WPF.
 
Share this answer
 
Comments
steersteer 23-Aug-11 11:56am    
Thanks for the reply..

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