Jump to content




Recent Status Updates

  • Photo
      15 Sep
    Error

    Programming is something that I enjoy and want to make a career out of. But, I usually tend to start things and not finish them. Any advice on how I can finish what I start?

    Show comments (2)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

How to Resize a Checkbox using the .NET Framework

checkbox .net resize framework help 3.5 .net framework

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1 LinkChef

LinkChef

    CC Regular

  • Member
  • PipPipPip
  • 47 posts

Posted 12 January 2013 - 09:06 PM

Hello everyone! I am currently using Visual Studio 2010 to make a Windows Form Application designed to be similar to a restaurant receipt/order maker, (similar to a tool used by waiters and waitresses). I am designing this to be as close to the source material as I can, and as user friendly as I can. My problem is that I want to make the check boxes bigger, not just the check box "box", but the individual check box. I have scoured the internet looking for some answers, and it seems the only way to do so is to make my own check box instead of using the ones in .NET. So, my question is can you resize the check boxes at all in the .NET Framework in C#? And if you cannot, where would be a good place to start in making my own check box. Keep in mind all I would need to change is the size of the boxes, so if there happens to be a starter code for making a check box, and you can change a few properties for size, that would work very nicely.

Thank you very much for reading, and if you answer, for answering.

I'm Batman!  ~Batman


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3,371 posts

Posted 13 January 2013 - 09:13 AM

If you want just the resizing, you can just set the property autosize to false.
Then you will be able to resize the checkbox with the mouse in the form design view.
If you need to make the checkbox check square bigger you need to code your own cb like this:

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 Form1 : Form
    {
	    public Form1()
	    {
		    InitializeComponent();
	    }

	    private void Form1_Load(object sender, EventArgs e)
	    {
		    BigCheckBox bcb = new BigCheckBox();
		    this.Controls.Add(bcb);
	    }

	    class BigCheckBox : CheckBox
	    {
		    public BigCheckBox()
		    {
			    this.Text = "Approved";
			    this.TextAlign = ContentAlignment.MiddleRight;			    
		    }

		    public override bool AutoSize
		    {
			    set { base.AutoSize = false; }
			    get { return base.AutoSize; }
		    }

		    protected override void OnPaint(PaintEventArgs e)
		    {
			    base.OnPaint(e);
							    
			    this.Height = 100;
			    this.Width = 200;
			    int squareSide = 80;

			    Rectangle rect = new Rectangle(new Point(0, 1), new Size(squareSide, squareSide));

			    ControlPaint.DrawCheckBox(e.Graphics, rect, this.Checked ? ButtonState.Checked : ButtonState.Normal);
		    }
	    }


    }
}
 

I copied the whole form code, I tested it, you got a biiig checkbox on there :D

Any doubt make me know.

#3 LinkChef

LinkChef

    CC Regular

  • Member
  • PipPipPip
  • 47 posts

Posted 13 January 2013 - 02:12 PM

Hey there BlackRabbit. Well, before we start, I'd like to admit I'm new to Windows Forms which means I might sound stupid when trying to explain this code, so I can try to understand it, and hopefully I won't ask any stupid questions. Now, I do want to say and explain that I tried to use this code setting this up with the same default program name, without modifying it anywhere, and putting it into the same Form1.cs file that is the only file using any using directives. However, even though it ran with no errors, I could not see the checkbox anywhere on the form when I launched it. I'm assuming that I will need to input it manually into the design form code file. If so, I'm not entirely sure how to do this. Any explanation would be awesome. However, I seem to understand that you're adding a new class that you made, based off of the old CheckBox that I would assume is the modified checkbox, and then you used control.Add to input it into the form.

All in all, an explanation of what you did with the Form1.cs file, and how to add it into the actual form would be a big help. I am also sorry if it is super obvious and I could not understand it completely.

Just to restate, I am using the .NET version 4, and am using Visual Studio C# Express 2010. Thank you again.

I'm Batman!  ~Batman


#4 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3,371 posts

Posted 13 January 2013 - 09:44 PM

LinkChef, no shame when learning, neither later :D

I will help you with the procedure, you need to follow it as in a recipe.

1. Open Visual Studio, the C# one :D

2. Create a new project, choose it to be a windows form, don't even change the project's default name, just give it enter and let it work.

3. Go to the form's design view

4. Put the mouse in the middle of the form and double click

5. It will go back to the code editor, you will notice it will leave you inside a new method named something like form1_load, that means the code there will be executed just before the form is displayed ;)

6. copy this code inside that method:
		   BigCheckBox bcb = new BigCheckBox();
		    this.Controls.Add(bcb);
That means when the form is loaded a BigCheckBox object is created and added to the form, dynamically, you don't need to drag it from the toolbox.

7. Right after the closing curly for that load method place this code:
	    class BigCheckBox : CheckBox
	    {
		    public BigCheckBox()
		    {
			    this.Text = "Approved";
			    this.TextAlign = ContentAlignment.MiddleRight;			    
		    }

		    public override bool AutoSize
		    {
			    set { base.AutoSize = false; }
			    get { return base.AutoSize; }
		    }

		    protected override void OnPaint(PaintEventArgs e)
		    {
			    base.OnPaint(e);
							    
			    this.Height = 100;
			    this.Width = 200;
			    int squareSide = 80;

			    Rectangle rect = new Rectangle(new Point(0, 1), new Size(squareSide, squareSide));

			    ControlPaint.DrawCheckBox(e.Graphics, rect, this.Checked ? ButtonState.Checked : ButtonState.Normal);
		    }
	    }

That is the definition of BigCheckBox which is a checkbox with some methods overriden, as in we taking control on how the checkbox is drawn ;)

After that, well, just execute and you will see the big checkbox

#5 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,249 posts

Posted 14 January 2013 - 06:17 AM

BlackRabbit, just to remind you. Form1_Load is not a method, it is event handler and Load is event not method. It is a big difference ;)

Microsoft Student Partner, Microsoft Certified Professional


#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3,371 posts

Posted 14 January 2013 - 08:06 AM

He! of course it is Tonchi, but please don't mess with our rookie friend's head. The guy is doing his first steps, he doesn't need any extra info yet ;)

So, LinkChef, how did it go?

#7 LinkChef

LinkChef

    CC Regular

  • Member
  • PipPipPip
  • 47 posts

Posted 14 January 2013 - 06:13 PM

That is one biiiiiiig Checkbox :P. Thank you. Tried it out on a new form with nothing changed and it works like a dream. Now because of your descriptions, I am going to try to incorporate this into the program I'm working on currently. The only problem is I don't get the easy customization that the design file gives me, but that's alright. Now to move on to radio buttons, and if this code file can be copied with a few tweaks, I should be fine! Thank you. Something I always try to pride myself in is when I ask a question, I always try to understand what is going on, so I can use that to build upon in the future. Just copy / pasting until it works doesn't solve the learning problem, it only solves the problem at hand. Anyway, I'll post back here and tell you if I got it working in my original program. Thank you again!

I'm Batman!  ~Batman