Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.

C#
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
Posted

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.
 
Share this answer
 
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
 
Share this answer
 
Comments
Curtdawg123 14-Oct-12 7:46am    
Thanks a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900