三国情

标题: 用C#写一段按钮的代码 [打印本页]

作者: 程序员没有节日    时间: 2023-3-11 19:54
标题: 用C#写一段按钮的代码
用C#写一段按钮的代码,根据用户的选择的时间公历或农历,显示出对应的天干地支。

这一段按钮的代码是要整合到一个八字算命软件中的,目前还没有根据用户输入的时间判断对应天干地支的功能。

注释要用简体中文写。其中无论用户选择的的是公历还是农历,

在点击此按钮后都要转换为对应的农历天干地支。

此功能要整合到已经存在的软件中。

作者: tiger008    时间: 2023-3-11 19:54
本帖最后由 tiger008 于 2023-3-11 19:59 编辑

来了来了来了来了,请结贴
作者: tiger008    时间: 2023-3-11 20:06
用C#写一段按钮的代码,根据用户的选择的时间公历或农历,显示出对应的天干地支。

这一段按钮的代码是要整合到一个八字算命软件中的,目前还没有根据用户输入的时间判断对应天干地支的功能。

注释要用简体中文写。其中无论用户选择的的是公历还是农历,

在点击此按钮后都要转换为对应的农历天干地支。

此功能要整合到已经存在的软件中。
以下是用C#编写的按钮代码,用于根据用户选择的时间公历或农历,显示出对应的天干地支:


// 引用相关命名空间
using System;
using System.Windows.Forms;
using System.Globalization;

n**espace 八字算命软件
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        // 点击按钮事件处理方法
        private void button1_Click(object sender, EventArgs e)
        {
            // 获取用户选择的时间
            DateTime dateTime = dateTimePicker1.Value;

            // 判断用户选择的是公历还是农历
            bool isGregorian = radioButton1.Checked;
            bool isLunar = radioButton2.Checked;

            // 根据用户选择的时间类型,获取对应的天干地支
            string ganZhi;
            if (isGregorian)
            {
                // 公历转换为农历
                ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar();
                int year = calendar.GetYear(dateTime);
                int month = calendar.GetMonth(dateTime);
                int day = calendar.GetDayOfMonth(dateTime);
                int leapMonth = calendar.GetLeapMonth(year);
                bool isLeapMonth = month == leapMonth;
                ganZhi = GetGanZhi(calendar, year, month, day, isLeapMonth);
            }
            else if (isLunar)
            {
                // 直接获取农历天干地支
                ChineseLunisolarCalendar calendar = new ChineseLunisolarCalendar();
                int year = dateTime.Year;
                int month = dateTime.Month;
                int day = dateTime.Day;
                bool isLeapMonth = calendar.IsLeapMonth(year, month);
                ganZhi = GetGanZhi(calendar, year, month, day, isLeapMonth);
            }
            else
            {
                // 用户未选择时间类型,提示错误信息
                MessageBox.Show("请选择时间类型(公历或农历)!");
                return;
            }

            // 显示天干地支
            label1.Text = ganZhi;
        }

        // 根据年月日获取天干地支
        private string GetGanZhi(ChineseLunisolarCalendar calendar, int year, int month, int day, bool isLeapMonth)
        {
            // 获取年的天干地支
            int ganIndex = calendar.GetCelestialStem(year) - 1;
            int zhiIndex = calendar.GetTerrestrialBranch(year) - 1;
            string ganZhi = GetGanZhiString(ganIndex, zhiIndex);

            if (isLeapMonth)
            {
                // 获取闰月的天干地支
                int leapMonthIndex = calendar.GetLeapMonth(year);
                if (month == leapMonthIndex)
                {
                    ganIndex = 13;
                    zhiIndex = calendar.GetTerrestrialBranch(year, month) - 1;
                    ganZhi += "闰" + GetGanZhiString(ganIndex, zhiIndex);
                }
                else if (month > leapMonthIndex)
                {
                    // 如果当前月份在闰月之后,则需要减一
                    month--;
                }
            }

            // 获取月和日的天干地支
            ganIndex = (month - 1) % 10;
            zhiIndex = (day - 1) % 12;
            ganZhi += GetGan


        ZhiString(ganIndex, zhiIndex);

        return ganZhi;
    }

    // 根据天干地支序号获取对应的天干地支字符串
    private string GetGanZhiString(int ganIndex, int zhiIndex)
    {
        string[] ganArray = new string[] { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
        string[] zhiArray = new string[] { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };

        string gan = ganArray[ganIndex];
        string zhi = zhiArray[zhiIndex];

        return gan + zhi;
    }
}
}


这段代码中,首先判断用户选择的时间类型是公历还是农历,然后根据用户选择的时间类型,获取对应的天干地支。如果用户未选择时间类型,则弹出错误提示。在获取天干地支时,如果选择的时间是农历,并且包含闰月,需要特殊处理。

此功能可以在现有的八字算命软件中进行整合。用户选择时间后,点击按钮即可显示对应的天干地支。








欢迎光临 三国情 (https://www.975w.com/) Powered by Discuz! X3.3