如何缩短此代码? [英] How can I shorten this code?

查看:64
本文介绍了如何缩短此代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我13岁,对C#来说还很陌生.我正在为我的学校编写拉丁语闪存卡的代码.我现在正在做的事情将永远长久,我想知道你们中是否有人知道可以缩短它的方法.

I''m 13 and pretty new to C#. I''m writing a code for Latin flash cards for my school. The way I''m doing it now is going to take forever and I was wondering if any of you guys know a way shorten it.

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

namespace WindowsFormsApplication1
{
    public partial class ltnmain : Form
    {
        Font CardFont = new System.Drawing.Font("Arial", 20);
        Pen CardBorder = new Pen(System.Drawing.Color.Black, 2);
        Pen CardCurve = new Pen(System.Drawing.Color.Black, 2);

        public ltnmain()
        {
            InitializeComponent();
        }

        private void ltnmain_Load(object sender, EventArgs e)
        {
            
        }

        public void DrawNewCard(string txt1, string txt2, int l1, int l2, int l11, int l22)
        {
            Point[] rtt = new Point[6];
            rtt[0] = new Point(40, 55);
            rtt[1] = new Point(41, 52);
            rtt[2] = new Point(43, 49);
            rtt[3] = new Point(44, 48);
            rtt[4] = new Point(47, 46);
            rtt[5] = new Point(50, 45);

            Point[] ltt = new Point[6];
            ltt[0] = new Point(210, 55);
            ltt[1] = new Point(209, 52);
            ltt[2] = new Point(207, 49);
            ltt[3] = new Point(206, 48);
            ltt[4] = new Point(203, 46);
            ltt[5] = new Point(200, 45);

            System.Drawing.Graphics CardTxta = null;
            CardTxta = CreateGraphics();
            CardTxta.DrawString(txt1, CardFont, System.Drawing.Brushes.Black, l1, l2);
            CardTxta.Dispose();

            System.Drawing.Graphics CardTxtb = null;
            CardTxtb = CreateGraphics();
            CardTxtb.DrawString(txt2, CardFont, System.Drawing.Brushes.Black, l11, l22);

            Rectangle Cardsides = new Rectangle(40, 50, 40, 250);

            System.Drawing.Graphics CardsidesRight = null;
            CardsidesRight = CreateGraphics();
            CardsidesRight.DrawLine(CardBorder, 40, 55, 40, 250);

            System.Drawing.Graphics CardsidesTop = null;
            CardsidesTop = CreateGraphics();
            CardsidesTop.DrawLine(CardBorder, 50, 45, 200, 45);

            System.Drawing.Graphics CardsidesLeft = null;
            CardsidesLeft = CreateGraphics();
            CardsidesLeft.DrawLine(CardBorder, 210, 55, 210, 250);

            System.Drawing.Graphics CardarchToptoRight = null;
            CardarchToptoRight = CreateGraphics();
            CardarchToptoRight.DrawCurve(CardCurve, rtt);

            System.Drawing.Graphics CardarchToptoLeft = null;
            CardarchToptoLeft = CreateGraphics();
            CardarchToptoLeft.DrawCurve(CardCurve, ltt);

            //I will have the rest of the card here
        }
        
        private void latinToEnglishToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChooseWordW1();
        }
        static int ranw1b;
        private void ChooseWordW1()
        {
            string txt1;
            string txt2;

            Random ranw1a = new Random();
            ranw1b = ranw1a.Next(1, 47);

            if (ranw1b == 1)
            {
                txt1 = "aestas, ";
                txt2 = "aestatis (f.)";
                DrawNewCard(txt1, txt2, 75, 90, 55, 160);
                btnCheck.Click +=new EventHandler(btnCheckClick);
            }  
       
            //Rest of week 1 words 2 - 47
        }

        private void btnCheckClick(object obj, EventArgs ea)
        {
            if (ranw1b == 1)
            {
                MessageBox.Show("The awnser was 'Summer'!", "Awnser", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //Rest of week 1 words 2 - 47
        }

    }
}



将会有几周的时间,还会有数百个单词.如果有人可以向我展示一种更简单的方法来完成此任务,这将非常有帮助.

谢谢



There are going to be a couple more weeks and hundreds more words. It would be very helpful if anybody could show me a simpler way to accomplish this same task.

Thanks

推荐答案

我必须承认,我不确定什么是拉丁闪存卡,以及您的应用程序应该做什么,但是我有我的想法.无论如何,我不会打扰自己从代码中提取.我会使用图元文件或svg.我会使用 InkScape [ MetaFile [ http://svg.codeplex.com/ [ SharpVectors-重新加载SVG#:简介 [ ^ ]).
文本部分应存储在存储库中.在这种情况下,我将使用文本文件,而不是数据库. XML看起来是一种很好的方法,您可以在其中存储单词,问题等.

为什么选择SVG?因为使用SVG,您可以简单地将这两个部分结合在一起,所以图元文件在这方面是一种只读的东西.
I have to admit, I am not sure what latin flash cards are, and what your application should do exactly, but I have my ideas. Anyway, I would not bother myself drawing from code. I would either use metafile or svg. I would use a drawing program like InkScape[^] to draw the graphical part and save in emf or svg. As .net supports MetaFile[^], you can simply draw it on a canvas - and you can find SVG support also (http://svg.codeplex.com/[^], SharpVectors - SVG# Reloaded: An Introduction[^]).
The textual part should be stored in a repository. I would use a text file, not a database in this case. XML looks a good approach, there you could store words, questions, and so on.

Why SVG? Because with SVG you could simply combine both parts, metafiles are sort of read-only things in this aspect.



就像一般建议一样,尝试将所有硬编码的行封装到某个对象(类或结构)中,并定义一个函数将与之通信的接口,对应于您的方法签名(字符串txt1,字符串txt2,int l1, int l2,int l11,int l22).然后,不必担心内部复杂性,因为它将被有效地封装.
希望这会有所帮助.
问候,
AB
Hi,
Just as a general recommendation, try to encapsulate all that hard-corded lines into some object (either class or struct) and define an interface that your function will communicate with, corresponding to your method signature (string txt1, string txt2, int l1, int l2, int l11, int l22). Then, do not bother with the internal complexity as it will be effectively encapsulated.
Hope this will help.
Regards,
AB


这篇关于如何缩短此代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆