Getting input from an array of textboxes for another window (wpf)

  • Thread starter Thread starter DivergentSpectrum
  • Start date Start date
  • Tags Tags
    Array Input Window
AI Thread Summary
The discussion revolves around the development of an n-body solver using WPF in C#. The main focus is on creating a user interface that allows input for multiple particles, specifically their coordinates, velocities, and masses through dynamically generated TextBox arrays. The challenge presented is how to manage the scope of these arrays so they can be accessed in a method triggered by a button click, specifically the "calculate" method. The solution involves declaring the TextBox arrays as class-level variables, allowing them to be accessible throughout the class. This adjustment enables the user to pass these arrays to a new window, DisplayWindow, when the button is clicked. The conversation highlights the importance of variable scope and proper initialization within the constructor to ensure the arrays are available for further calculations. The final code structure reflects these changes, demonstrating a successful implementation of the discussed solutions.
DivergentSpectrum
Messages
149
Reaction score
15
Hi, I am working on an n-body solver.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Wpftrajectory
{
    /// <summary>
    /// Interaction logic for Infogetter.xaml
    /// </summary>
    public partial class Infogetter : Window
    {
        public Infogetter(String numberit, String endt, String numpart)
        {
            InitializeComponent();
            TextBox[] xcoord = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] ycoord = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] zcoord = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] xvel = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] yvel = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] zvel = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] mass = new TextBox[Convert.ToInt32(numpart)];
            int i = 0;
                while(i<Convert.ToInt32(numpart))
                {
                    xcoord[i]=new TextBox();
                     ycoord[i] = new TextBox();
                     zcoord[i] = new TextBox();
                     xvel[i] = new TextBox();
                  yvel[i] = new TextBox();
                   zvel[i] = new TextBox();
                   mass[i] = new TextBox();
 xcoord[i].Width=120;
 xcoord[i].Height=20;

 ycoord[i].Width=120;
 ycoord[i].Height=20;

 zcoord[i].Width=120;
 zcoord[i].Height=20;

 xvel[i].Width=120;
 xvel[i].Height=20;

 yvel[i].Width=120;
 yvel[i].Height=20;

 zvel[i].Width=120;
 zvel[i].Height=20;
 mass[i].Width = 120;
 mass[i].Height = 20;

canvas.Children.Add(xcoord[i]);
canvas.Children.Add(ycoord[i]);         
canvas.Children.Add(zcoord[i]);
canvas.Children.Add(xvel[i]);
canvas.Children.Add(yvel[i]);         
canvas.Children.Add(zvel[i]);
canvas.Children.Add(mass[i]);
xcoord[i].Margin = new Thickness(0,25 * i ,0,0);
ycoord[i].Margin = new Thickness(125,25 * i ,0,0);
zcoord[i].Margin = new Thickness(250,25 * i,0,0);
  xvel[i].Margin = new Thickness(375,25 * i ,0,0);
yvel[i].Margin = new Thickness(500,25 * i ,0,0);
 zvel[i].Margin = new Thickness(625,25 * i ,0,0);
mass[i].Margin = new Thickness(750,25*i,0,0);
            i++;
        }
           
               

        }

       public void calculate(object sender, RoutedEventArgs e)
        {
           DisplayWindow displaywindow= new DisplayWindow(? );

           displaywindow.Show();
        }  

        }    }
}

The previous window got the variable "numpart" (number of particles), and then this current window displays 7 times numpart textboxes, for input on the position and velocity and mass.
the items
xcoord
ycoord
zcoord
xvel
yvel
zvel
mass
are all arrays of textboxes, and normally id use mass.Text to get the string value of mass, but when theyre arrays i get a lot of errorsI used an the event handler "calculate" , which is activated when a button is clicked, and where you see the ? i have no idea what to doto simplify my question, I am trying to open a new window with parameters, and the parameters are arrays.
 
Last edited:
Technology news on Phys.org
Solved it by declaring the variables beforehand. Mods please close this
 
Whoops I spoke too soon still having the same problem please help someone
 
I've never seen an object of type DisplayWindow in C#. Is DisplayWindow a custom object of yours? If so, what parameters does its constructor has?

Also, your variable lives are currently attached to the constructor. They cannot be used outside of it unless you pass the results of your constructor variables to class accesible variables. Only then you can use your arrays in the calculate method.
 
Calculate is an event handler activated when the user clicks a button in the gui. And DisplayWindow is just the name i gave to another window contained in the same wpf project(whose parameters are the input contained in the array of textboxes).
Hmm I see what you mean by class accessible. How do I "save" a variable within the infogetter method to be used by the calculate method? (Sorry I am kinda new at this)
 
The following way:

Code:
public partial class Infogetter : Window {
//Notice the location of variables in the code
private TextBox[] xcoord; 
private TextBox[] ycoord;
private TextBox[] zcoord;
private TextBox[] xvel;
private TextBox[] yvel;
private TextBox[] zvel;
private TextBox[] mass;

public Infogetter(String numberit, String endt, String numpart) {
	InitializeComponent(); 
        TextBox[] xcoord = new TextBox[Convert.ToInt32(numpart)]; 
        TextBox[] ycoord = new TextBox[Convert.ToInt32(numpart)];

        //... the rest of your code
       //right before ending this method called Infogetter pass your values onto the private variables     like:

       this.xcoord = xcoord;
       this.ycoord = ycoord;
//...etc

}

}

In the end it should look like this:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Wpftrajectory
{
    /// <summary>
    /// Interaction logic for Infogetter.xaml
    /// </summary>
    public partial class Infogetter : Window
    {
	private TextBox[] xcoord;
	private TextBox[] ycoord;
	private TextBox[] zcoord;
	private TextBox[] xvel;
	private TextBox[] yvel;
	private TextBox[] mass;

        public Infogetter(String numberit, String endt, String numpart)
        {
            InitializeComponent();
            TextBox[] xcoord = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] ycoord = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] zcoord = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] xvel = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] yvel = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] zvel = new TextBox[Convert.ToInt32(numpart)];
            TextBox[] mass = new TextBox[Convert.ToInt32(numpart)];
            int i = 0;
                while(i<Convert.ToInt32(numpart))
                {
                    xcoord[i]=new TextBox();
                     ycoord[i] = new TextBox();
                     zcoord[i] = new TextBox();
                     xvel[i] = new TextBox();
                  yvel[i] = new TextBox();
                   zvel[i] = new TextBox();
                   mass[i] = new TextBox();
 xcoord[i].Width=120;
 xcoord[i].Height=20;

 ycoord[i].Width=120;
 ycoord[i].Height=20;

 zcoord[i].Width=120;
 zcoord[i].Height=20;

 xvel[i].Width=120;
 xvel[i].Height=20;

 yvel[i].Width=120;
 yvel[i].Height=20;

 zvel[i].Width=120;
 zvel[i].Height=20;
 mass[i].Width = 120;
 mass[i].Height = 20;

canvas.Children.Add(xcoord[i]);
canvas.Children.Add(ycoord[i]);         
canvas.Children.Add(zcoord[i]);
canvas.Children.Add(xvel[i]);
canvas.Children.Add(yvel[i]);         
canvas.Children.Add(zvel[i]);
canvas.Children.Add(mass[i]);
xcoord[i].Margin = new Thickness(0,25 * i ,0,0);
ycoord[i].Margin = new Thickness(125,25 * i ,0,0);
zcoord[i].Margin = new Thickness(250,25 * i,0,0);
  xvel[i].Margin = new Thickness(375,25 * i ,0,0);
yvel[i].Margin = new Thickness(500,25 * i ,0,0);
 zvel[i].Margin = new Thickness(625,25 * i ,0,0);
mass[i].Margin = new Thickness(750,25*i,0,0);
            i++;
        }

	this.xcoord = xcoord;
	this.ycoord = ycoord;
	this.zcoord = zcoord;
	this.xvel = xvel;
	this.yvel = yvel;
	this.mass = mass;
           
               

        }

       public void calculate(object sender, RoutedEventArgs e)
        {
		//Now you can access those variables here by using the word this.variable
		//For example: this.xcoord
           DisplayWindow displaywindow= new DisplayWindow(//class accessible variables );

           displaywindow.Show();
        }

        }

    }
}
 
  • Like
Likes 1 person
sweet thanks
 
No problem. Glad to help.
 
Last edited:

Similar threads

2
Replies
52
Views
7K
Replies
1
Views
4K
Back
Top