Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Sep 5, 2014 #1
    Hi, im working on an n-body solver.
    Code (Text):
    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 alot of errors


    I 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 do


    to simplify my question, im trying to open a new window with parameters, and the parameters are arrays.
     
    Last edited: Sep 5, 2014
  2. jcsd
  3. Sep 6, 2014 #2
    Solved it by declaring the variables beforehand. Mods please close this
     
  4. Sep 6, 2014 #3
    Whoops I spoke too soon still having the same problem please help someone
     
  5. Sep 6, 2014 #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.
     
  6. Sep 6, 2014 #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 im kinda new at this)
     
  7. Sep 6, 2014 #6
    The following way:

    Code (Text):
    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 (Text):
    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();
            }

            }

        }
    }
     
  8. Sep 7, 2014 #7
    sweet thanks
     
  9. Sep 7, 2014 #8
    No problem. Glad to help.
     
    Last edited: Sep 7, 2014
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Getting input from an array of textboxes for another window (wpf)
  1. Gets and string arrays (Replies: 4)

  2. C - getting input (Replies: 3)

Loading...