【问题标题】:How can I display as text the properties of an element in a textbox in windows form application c#?如何在 windows 窗体应用程序 c# 的文本框中将元素的属性显示为文本?
【发布时间】:2017-08-07 18:02:40
【问题描述】:

我正在编写一个计算开普勒元素的程序。主函数获取2行tle,返回对象kep,其属性为卫星轨道参数。我必须在文本框中显示这些参数,但我不明白我应该怎么做。 任何帮助都会得到帮助:) 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)
        {

        }
    }

我的kepelments课:

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; }  


    }  

和主程序:

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;

        }


    }

【问题讨论】:

  • 添加代码将帮助我们了解您在做什么,并让我们进一步帮助您。
  • 应该如何创建kep对象?当用户点击一个按钮?您的表单至少必须在某处声明 kep 对象,以便您可以引用它。
  • @LarsTech 好的,我更正了我的代码。现在该函数是无效的,并且该对象在主函数中声明。但我仍然无法从 Form1.cs 中引用它。
  • 如果您更正了您的代码,您并未与我们分享。

标签: c# winforms output


【解决方案1】:

您可以使用 $"Label: {value}\r\n" 手动添加所有带有标签的值,也可以使用 StringBuilder 类。另一种选择是对保存数据的类使用反射,并通过自动查看属性及其值来构建文本输出。然后只需将构建的文本字符串分配给文本框。

您还可以在 KepElements 类中使用字符串覆盖 ToString()。

要求:

using System.Reflection;

例子:

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;
    }
}

【讨论】:

  • 如果您能多一点反映第二个选项,我将非常感激 - 我不确定我是否完全理解。谢谢。
  • 刚刚在看,会发布示例。
  • 有多种方法可以满足您的需求,但这是我过去使用过的方法。
  • 嗨,我自己设法做到了。有时你只需要休息一下然后去睡觉,事情就会在一秒钟内弄清楚。
猜你喜欢
  • 2023-01-20
  • 2013-01-05
  • 2011-08-24
  • 2012-01-23
  • 2014-12-03
  • 2011-05-21
  • 1970-01-01
  • 2013-04-28
相关资源
最近更新 更多