Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey Guys,

I am creating a small program to generate cards for my parents company. Each card consists of a Logo, Title, and Description. Since I'm a very independent person and I just started making this without putting much thought into it, I spent an hour learning the basics of the Graphics class, drawing lines, images, e.t.c, and I managed to dynamically generate this card
[Excuse the bad design, I made it simple as my parents aren't the best with adapting to new technologies].

http://i.imgur.com/W8AioFG.png[^]

The result is how I expected, generated dynamically, I congratulated myself. However, whenever I add another instance of the controls, stuff goes crazy.
I present to you:

http://s23.postimg.org/g8tdlaoex/Such_Doge.gif[^]

Here's the source, you can hate on the code, but keep in mind I started programming 6 months ago, and I'm 14.However, I do hate ageism at the same time :/. Such Hippocritical.

http://www.mediafire.com/download/rzxj2b17enr5nbj/SuchCardGen.rar[^]

Here is the relevant part of the code that generates the card.
C#
public partial class Card : UserControl
 {
     Graphics g;
     private string Title, Description;
     public Card(string Title, string Description)
     {
         InitializeComponent();
         this.Title = Title;
         this.Description = Description;
         g = CreateGraphics();

     }
     private Rectangle DrawBox()
     {
         Rectangle rect = new Rectangle(4, 4, 323, 204);
         g.DrawRectangle(new Pen(Color.Black, 2), rect);
         return rect;
     }
     private void DrawLine(Color c, int sx, int sy, int es, int ey)
     {
         g.DrawLine(new Pen(c), new Point(sx, sy), new Point(es, ey));
     }
     public static Image resizeImage(Image imgToResize, Size size)
     {
         return (Image)(new Bitmap(imgToResize, size));
     }

     private void DrawTitle(string Text, float FontSize)
     {
         Rectangle newRect = ClientRectangle;
         newRect.Height -= 65;
         StringFormat format = new StringFormat();
         format.LineAlignment = StringAlignment.Center;
         format.Alignment = StringAlignment.Center;
         String drawString = Text;
         Font drawFont = new Font("Segoe UI", 16);
         SolidBrush drawBrush = new SolidBrush(Color.Black);
         g.DrawString(Text, new Font("Segoe UI", FontSize), Brushes.Black, newRect, format);
     }
     private void DrawDescription(string Text,float FontSize)
     {
         Rectangle newRect = ClientRectangle;
         StringFormat format = new StringFormat();
         format.LineAlignment = StringAlignment.Center;
         format.Alignment = StringAlignment.Center;
         String drawString = Text;
         Font drawFont = new Font("Segoe UI", 16);
         SolidBrush drawBrush = new SolidBrush(Color.Black);
         g.DrawString(Text, new Font("Segoe UI", FontSize), Brushes.Black, newRect, format);
     }
     public void RefreshCard(string Titlez, string Descriptionz, float titleFontSizez, float descriptionFontSizez)
     {
         g.Clear(Color.White );
         DrawBox();
         AddJaxLogo();
         DrawTitle(Titlez, titleFontSizez);
         DrawDescription(Descriptionz, descriptionFontSizez);
         DrawLine(Color.Black, 120, 67, 195, 67);
     }
     private void FillCard()
     {
         DrawBox();
         AddJaxLogo();
         DrawTitle(Title, 18f);
         DrawDescription(Description, 12f);
         DrawLine(Color.Black, 120, 67, 195, 67);
     }
     private void AddJaxLogo()
     {
         g.DrawImage(resizeImage(Jax_Generator.Properties.Resources.Jax_logo, new Size(89, 64)), 115, 2);
     }
     private void tmr_Tick(object sender, EventArgs e)
     {
         FillCard();
         tmr.Enabled = false;
     }
 }


To emulate it, create a user control, drag a timer onto the form, and name it tmr, then assign the appropriate event handlers.

Oh, right, and the question is. Why is it doing that, and how do I fix it?

Thanks guys!
Posted
Updated 7-Jan-14 16:32pm
v6
Comments
Ron Beyer 7-Jan-14 21:23pm    
First, nobody is going to download your code, not that we don't trust you, but we don't trust you. You need to post the relevant parts of your code. Second, I understand you're 14, but try to act professional, and despite the different wording, please don't swear, it doesn't make me (and probably others) want to help you.
Joseph Croweds 7-Jan-14 21:50pm    
Sorry, I do realise now how daft I was being writing such a poorly worded question on this website, thanks for the suggestion, I have now fixed it!
BillWoodruff 8-Jan-14 1:05am    
Please describe exactly what you mean when you say "whenever I add another instance of the controls, stuff goes crazy." Looking at your animated .gif file I can't tell what the problem is.

What are you doing with the Timer ?
Joseph Croweds 8-Jan-14 1:10am    
I have pinpointed and fixed the problem, what it was is that the FlowLayoutPanel was refreshed whenever I added a control, which then made all the controls within it repaint themselved, I had to add picture boxes, bitmap's, and put the graphics into the bitmap, which them drew it onto the picturebox.

The timer is used to delay the start because if I called FillCard(); from within the contructor, it wouldn't work.
thatraja 8-Jan-14 8:02am    
1) Solved your issue?
2) How did you create animated gif? details please

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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