WinForm显示版本相关信息

本文使用WinForm演示

属WinForm小知识系列文章第一篇



     本文内容在软件上有版本相关显示的需求时,可以用到。


     演示效果:


一、窗体展示

     黄色框从上到下分别是panel1到panel5的大致区域:

用到的控件:tableLayoutPanel,panel,label,textbox

这里的作用:

①tableLayoutPanel:将界面按需求划分区域块,Dock属性设置为Fill,编辑行和列为1列5行

②panel:一个容器,可以把label控件和textbox控件放入,Dock属性设置为Fill

③label:一个标签控件

④textbox:一个文本框


二、代码展示

1.代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyDemoVersion
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Text = String.Format("关于 {0}", AssemblyTitle);
            this.labelProductName.Text = AssemblyProduct;
            this.labelVersion.Text = String.Format("版本 {0}", AssemblyVersion);
            this.labelCopyright.Text = AssemblyCopyright;
            this.labelCompanyName.Text = AssemblyCompany;
            this.textBoxDescription.Text = AssemblyDescription;
        }

        #region 程序集特性访问器

        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion
    }
}

2.可能遇到的问题:

缺少引用

如图所示直接引用即可:


三、版本信息配置

鼠标右击项目选择属性,打开程序集信息配置相关版本信息,配置完成运行即可:


四、总结

主要掌握窗体内代码的应用,就可以实际运用到其他项目中去。

谢谢观看。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Winform 中,可以通过 MessageBox、Dialog 或者自定义控件等方式弹窗显示详情信息。 1. MessageBox MessageBox 是一种简单易用的弹窗控件,可以用来显示简单的信息或者提示。如果需要显示详细的信息,可以在 MessageBox 中添加一个“详细信息”按钮,点击后再弹出一个 Dialog 窗口显示完整信息。 示例代码: ``` string message = "这是一个示例信息"; string caption = "提示"; MessageBoxButtons buttons = MessageBoxButtons.OKCancel; DialogResult result = MessageBox.Show(message, caption, buttons); if (result == DialogResult.OK) { // 确认按钮被点击 } else if (result == DialogResult.Cancel) { // 取消按钮被点击 } ``` 2. Dialog Dialog 是一种更加灵活的弹窗控件,可以自定义窗口的样式和布局,以及添加各种控件和交互逻辑。可以通过 Dialog 显示详细的信息,并且可以添加按钮等交互元素。 示例代码: ``` // 在 Form 中添加一个按钮,点击后弹出 Dialog 窗口 private void button1_Click(object sender, EventArgs e) { string message = "这是一个示例信息"; string caption = "详细信息"; DialogForm dialog = new DialogForm(message, caption); dialog.ShowDialog(); } // 自定义 Dialog 窗口 public partial class DialogForm : Form { public DialogForm(string message, string caption) { InitializeComponent(); this.Text = caption; this.messageLabel.Text = message; } private void okButton_Click(object sender, EventArgs e) { this.Close(); } } ``` 3. 自定义控件 如果需要更加复杂的界面或者交互逻辑,可以自定义控件来实现。可以通过继承 Control 或者 UserControl 等类来创建自定义控件,然后将其添加到窗口中。自定义控件可以包含各种控件和逻辑,可以实现更加灵活和复杂的界面和交互。 示例代码: ``` // 创建一个自定义控件,用来显示详细信息 public partial class DetailControl : UserControl { public DetailControl(string message) { InitializeComponent(); this.messageLabel.Text = message; } } // 在 Form 中添加一个按钮和一个 Panel,点击按钮后在 Panel 中显示自定义控件 private void button1_Click(object sender, EventArgs e) { string message = "这是一个示例信息"; DetailControl detailControl = new DetailControl(message); this.detailPanel.Controls.Clear(); this.detailPanel.Controls.Add(detailControl); } ``` 以上是 Winform 中弹窗显示详情信息的几种方式,可以根据实际需求选择不同的方式来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值