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
Click For Summary

Discussion Overview

The discussion revolves around implementing an n-body solver in a WPF application, specifically focusing on how to manage input from an array of textboxes for particle properties such as position, velocity, and mass. Participants explore issues related to passing these arrays to another window within the application.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant describes the creation of multiple textboxes for inputting particle properties and expresses confusion about how to pass these arrays to a new window.
  • Another participant suggests that the issue may stem from variable scope, indicating that the arrays need to be class accessible to be used in the event handler.
  • A later reply provides a potential solution by recommending that the arrays be declared as private class variables and assigned values from the constructor, allowing them to be accessed in other methods.
  • One participant initially claims to have solved the issue but later indicates that the problem persists, seeking further assistance.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to handle the arrays, as some express confusion and seek clarification while others provide differing suggestions.

Contextual Notes

There are limitations regarding the handling of variable scope and the passing of parameters between methods, which remain unresolved in the discussion.

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   Reactions: 1 person
sweet thanks
 
No problem. Glad to help.
 
Last edited:

Similar threads

  • · Replies 52 ·
2
Replies
52
Views
8K
  • · Replies 1 ·
Replies
1
Views
4K