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

In summary: So it seems you are asking how to create a new window with specific parameters. I would recommend looking at the window's code for more information.In summary, this window has 7 textboxes for input, each of which is an array.
  • #1
DivergentSpectrum
149
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
  • #2
Solved it by declaring the variables beforehand. Mods please close this
 
  • #3
Whoops I spoke too soon still having the same problem please help someone
 
  • #4
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.
 
  • #5
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)
 
  • #6
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
  • #7
sweet thanks
 
  • #8
No problem. Glad to help.
 
Last edited:

1. How can I access the values from an array of textboxes in another window using WPF?

To access the values from an array of textboxes in another window using WPF, you can use the Window.FindName() method to find the textbox by its name and then retrieve its value using the Text property.

2. Can I pass an array of textboxes as a parameter to another window in WPF?

Yes, you can pass an array of textboxes as a parameter to another window in WPF. You can create a custom constructor for the receiving window that accepts an array of textboxes as a parameter and then pass the array while creating an instance of the window.

3. How can I validate the input from an array of textboxes in WPF?

To validate the input from an array of textboxes in WPF, you can use the TextChanged event and handle it in the code-behind to check for any invalid input. You can also use data validation techniques such as using ValidationRules or implementing the INotifyDataErrorInfo interface.

4. Is it possible to dynamically add or remove textboxes from an array in WPF?

Yes, it is possible to dynamically add or remove textboxes from an array in WPF. You can use methods such as Add() and Remove() to add or remove textboxes from the array. You can also use data binding to dynamically add or remove textboxes based on a data source.

5. How can I access a specific textbox from an array of textboxes in another window using WPF?

To access a specific textbox from an array of textboxes in another window using WPF, you can use the Window.FindName() method and specify the name of the textbox you want to access. You can then retrieve its value using the Text property.

Similar threads

  • Sci-Fi Writing and World Building
2
Replies
52
Views
4K
  • Sci-Fi Writing and World Building
Replies
1
Views
3K
Back
Top