简体   繁体   中英

How can I display as text the properties of an element in a textbox in windows form application c#?

I am writing a program that calculates keplerian elements. the main function gets 2 lines of tle, and return the object kep and his properties are the parameters of the sattelite orbit. I have to display these parameters in a textbox, but I dont understand how exactly am I supposed to do it. any help will be appriciated :) form1.cs:

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void inputtext_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void retrieveInput_Click(object sender, EventArgs e)
        {
            String line1 = line1String.Text;
            String line2 = line2String.Text;
            //what happens after the recieving of the strings is the calculation and displating the kep properties in textbox "ShowBox"
        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }
    }

my kepelments class:

class KepElements
    {
        public double raan { get; set; }
        public double argperi { get; set; }
        public double meanan { get; set; }
        public double meanmotion { get; set; }
        public double eccentricity { get; set; }
        public double bstar { get; set; }
        public double epochYear { get; set; }
        public double inclination { get; set; }
        public double epochDay { get; set; }  


    }  

and the main program:

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

        }
        //function that gets 2 strings and calculates all elemnts 
        public static KepElements calculating(String line1, String line2)
        {
            double raan, argperi, meanan, meanmotion, eccentricity, bstar, epochYear, inclination, SepochDay, sec, epochDay;
            int CurrentSec, sec2, sec3;

            KepElements kep = new KepElements();


            //setting eccentricity
            eccentricity = Convert.ToDouble(line2.Substring(28, 7));
            kep.eccentricity = eccentricity;

            //setting the bstar
            bstar = Convert.ToDouble(line2.Substring(55, 8));
            kep.bstar = bstar;

            //setting the epochYear
            epochYear = Convert.ToDouble(line2.Substring(20, 2));
            kep.epochYear = epochYear;

            //calculating the EpochDay
            SepochDay = Convert.ToDouble(line2.Substring(22, 12));
            sec = ((SepochDay - Math.Truncate(SepochDay)) * 100000000); //the time of seconds (after the decimal point)
            sec3 = (int)sec;
            sec2 = (int)SepochDay;
            sec2 = sec2 * 86400;//sec2 is now the number of sec in a day*number of days since beggining of the current year 
            CurrentSec = (DateTime.Now.Year - 1970) * 31556926;
            epochDay = CurrentSec + sec + sec2;
            kep.epochDay = epochDay;

            //calculating the inclination 
            inclination = Convert.ToDouble(line2.Substring(10, 8));
            inclination = (inclination / 180) * Math.PI;
            kep.inclination = inclination;

            //calculating the meananomaly
            meanan = Convert.ToDouble(line2.Substring(45, 8));
            meanan = (meanan / 180) * Math.PI;
            kep.meanan = meanan;

            //calculating the argue of perigee
            argperi = Convert.ToDouble(line2.Substring(36, 8));
            argperi = (argperi / 180) * Math.PI;
            kep.argperi = argperi;

            //calculating the mean motion 
            meanmotion = Convert.ToDouble(line2.Substring(54, 11));
            meanmotion = (meanmotion / 1440) * Math.PI * 2;
            kep.meanmotion = meanmotion;

            //calculating the raan
            raan = Convert.ToDouble(line2.Substring(19, 8));
            raan = (raan / 180) * Math.PI;
            kep.raan = raan;
            return kep;

        }


    }

You can either manually add all the values with labels using $"Label: {value}\\r\\n" or use a StringBuilder class. Another option is to use reflection on the class holding the data and build your text output by automatically looking at the properties and their values. Then just assign the built text string to the text box.

You could also use the string override ToString() in your KepElements class.

Requires:

using System.Reflection;

Example:

class KepElements
{
    public double raan { get; set; }
    public double argperi { get; set; }
    public double meanan { get; set; }
    [UserFriendlyName("Mean Motion")]
    public double meanmotion { get; set; }
    public double eccentricity { get; set; }
    public double bstar { get; set; }
    public double epochYear { get; set; }
    public double inclination { get; set; }
    public double epochDay { get; set; }

    public override string ToString()
    {
        StringBuilder build = new StringBuilder();
        foreach (var property in typeof(KepElements).GetProperties())
        {
            build.AppendLine($"{property.GetUserFriendlyName()}: {property.GetValue(this)}");
        }
        return build.ToString();
    }
}

[AttributeUsage(AttributeTargets.Property)]
public class UserFriendlyName : Attribute
{
    public UserFriendlyName(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }
}

public static class GetUserFriendlyNameExt
{
    public static string GetUserFriendlyName(this PropertyInfo obj)
    {
        object[] attribs = obj.GetCustomAttributes(typeof(UserFriendlyName),false);
        if (attribs != null && attribs.Length > 0)
            return (attribs[0] as UserFriendlyName)?.Name;
        else
            return obj.Name;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM