6 Replies - 1756 Views - Last Post: 08 April 2012 - 03:51 PM Rate Topic: -----

#1 cloges4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-April 12

Trying to change paint color and size using radio buttons

Posted 07 April 2012 - 03:27 PM

I have written a basic painting application in windows form C#, the program itself works fine, the only issue I am having is getting input form tow different radio button groups, specifically color and size. Everything I have tried so far has failed. I have commented out my most recent attempts. I am hoping to get pointed in the right direction so I can figure this out myself.

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 DrawingPad
{
    public partial class DrawingPad : Form
    {
      //  Size pensize = new Size(8,8);
        Color pencolor = new Color();
       

        
        public DrawingPad()
        {
            InitializeComponent();
            draw = PanelDraw.CreateGraphics();
        }
        bool yesPaint = false; //setting bool variable to determine when to paint
       // bool yesDraw = false; 
        Graphics draw; // 
        private void PanelDraw_MouseDown(object sender, MouseEventArgs e)// method for use when the mouse is pressed
        {
             yesPaint = true;

           //  yesDraw = true;
        }

        private void PanelDraw_MouseUp(object sender, MouseEventArgs e)// method for use when mouse is released
        {
           // yesDraw = false;

            yesPaint = false;
            startx = null;
            starty = null;
        }
        int? startx = null;
        int? starty = null;
        private void PanelDraw_MouseMove(object sender, MouseEventArgs e)// method for use when mouse is moved
        {
            if (yesPaint)
            {
                Pen pen = new Pen(Color.Aquamarine);
                    draw.DrawLine(pen, new Point(startx ?? e.X, starty ?? e.Y), new Point(e.X, e.Y));
                    startx = e.X;
                    starty = e.Y;
            }
        }

        private void RBtnBlue_CheckedChanged(object sender, EventArgs e)// method to control the blue color radio button
        {
                
          //  pencolor = Color.Blue;
         //   pensize = new Pen(pencolor, pensize);
        }

        private void RBtnRed_CheckedChanged(object sender, EventArgs e)// method to control the red color radio button
        {
           
          //  pencolor = Color.Red;
          //  pensize = new Pen(pencolor, pensize);
        }

        private void RBtnOrange_CheckedChanged(object sender, EventArgs e)// method to control the orange color radio button
        {
          //  pencolor = Color.Orange;
           // pensize = new Pen(pencolor, pensize);
        }

        private void RBtnGreen_CheckedChanged(object sender, EventArgs e)// method to control the green color radio button
        {
          //  pencolor = Color.Green;
          //  pensize = new Pen(pencolor, pensize);
        }

        private void RBtnBlack_CheckedChanged(object sender, EventArgs e)// method to control the black color radio button
        {
          //  pencolor = Color.Black;
          //  pensize = (pencolor, pensize);
        }

        private void RBtnSmall_CheckedChanged(object sender, EventArgs e)// method make pen size small
        {

        }

        private void RBtnMedium_CheckedChanged(object sender, EventArgs e)// method to make pen size medium
        {

        }

        private void RBtnLarge_CheckedChanged(object sender, EventArgs e)// method to make pen size large
        {

        }

        private void BtnClear_Click(object sender, EventArgs e)// method to erase the entire drawing pad
        {
            draw.Clear(PanelDraw.BackColor);
        }

        private void PanelDraw_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to change paint color and size using radio buttons

#2 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Trying to change paint color and size using radio buttons

Posted 07 April 2012 - 04:53 PM

Try declaring a class level Pen (outside of any event or method).

Then in your RadioButton events, assign a new Pen() to that class level variable.

Finally, in your Mouse_Move event, don't use the local Pen, but the class level Pen, as it will now have the correct color you want to use.

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 DrawingPad
{
    public partial class DrawingPad : Form
    {       
        Pen actualPen = new Pen() <- NOT ACTUAL CODE.
        
        public DrawingPad()
        {
            InitializeComponent();
            draw = PanelDraw.CreateGraphics();
        }

        private void PanelDraw_MouseDown(object sender, MouseEventArgs e)
        {
             yesPaint = true;
        }

        private void PanelDraw_MouseUp(object sender, MouseEventArgs e)
        {
            yesPaint = false;
            startx = null;
            starty = null;
        }

        int? startx = null;
        int? starty = null;

        private void PanelDraw_MouseMove(object sender, MouseEventArgs e)
        {
            if (yesPaint)
            {
                draw.DrawLine(actualPen , new Point(startx ?? e.X, starty ?? e.Y), new Point(e.X, e.Y));
                startx = e.X;
                starty = e.Y;
            }
        }

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

        private void RBtnRed_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void RBtnOrange_CheckedChanged(object sender, EventArgs e)
        {
            pencolor = Color.Orange;
            actualPen = new Pen(pencolor, pensize);
        }


        private void BtnClear_Click(object sender, EventArgs e)// method to erase the entire drawing pad
        {
            draw.Clear(PanelDraw.BackColor);
        }

        private void PanelDraw_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}

This post has been edited by Sergio Tapia: 07 April 2012 - 04:57 PM

Was This Post Helpful? 1
  • +
  • -

#3 cloges4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-April 12

Re: Trying to change paint color and size using radio buttons

Posted 07 April 2012 - 05:51 PM

That makes perfect sense. Thank you for expert advice.
Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Trying to change paint color and size using radio buttons

Posted 07 April 2012 - 06:24 PM

Happy to help, let us know how it turns out. :)
Was This Post Helpful? 0
  • +
  • -

#5 cloges4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-April 12

Re: Trying to change paint color and size using radio buttons

Posted 08 April 2012 - 03:05 PM

Thought I had everything figured out I setup defualt values for the size and color radiobuttons so when application begins the auto selected buttons work as advertised. The problem occurs when I select a radiobutton to change line size. When this is done nothing occurs, but if I change color then select a size the program works perfectly. I believe if I can get the values of the defualt radiobuttons at the begining of the program instead of setting them everything will work fine, the problem is everything I try either does not work or the IDE doesn't like 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 DrawingPad
{
    public partial class DrawingPad : Form
    {
        Color color = new Color();
        Pen pen = new Pen(Color.Blue);
        float size = 0;
        



        public DrawingPad()
        {
            InitializeComponent();
            draw = PanelDraw.CreateGraphics();
        }
        bool yesPaint = false; //setting bool variable to determine when to paint

        Graphics draw;
        private void PanelDraw_MouseDown(object sender, MouseEventArgs e)// method for use when the mouse is pressed
        {
            yesPaint = true;
        }

        private void PanelDraw_MouseUp(object sender, MouseEventArgs e)// method for use when mouse is released
        {
            yesPaint = false;
            startx = null;
            starty = null;
        }
        int? startx = null;
        int? starty = null;

        private void PanelDraw_MouseMove(object sender, MouseEventArgs e)// method for use when mouse is moved
        {
            if (yesPaint)
            {
                draw.DrawLine(pen, new Point(startx ?? e.X, starty ?? e.Y), new Point(e.X, e.Y));
                startx = e.X;
                starty = e.Y;
            }
        }

        private void RBtnBlue_CheckedChanged(object sender, EventArgs e)// method to control the blue color radio button
        {
            color = Color.Blue;
            pen = new Pen(color, size);

        }

        private void RBtnRed_CheckedChanged(object sender, EventArgs e)// method to control the red color radio button
        {
            color = Color.Red;
            pen = new Pen(color, size);

        }

        private void RBtnOrange_CheckedChanged(object sender, EventArgs e)// method to control the orange color radio button
        {
            color = Color.Orange;
            pen = new Pen(color, size);

        }

        private void RBtnGreen_CheckedChanged(object sender, EventArgs e)// method to control the green color radio button
        {
            color = Color.Green;
            pen = new Pen(color, size);

        }

        private void RBtnBlack_CheckedChanged(object sender, EventArgs e)// method to control the black color radio button
        {
            color = Color.Black;
            pen = new Pen(color, size);

        }

        private void RBtnSmall_CheckedChanged(object sender, EventArgs e)// method make pen size small
        {
            
            size = 1;
            pen = new Pen(color,size);
        }

        private void RBtnMedium_CheckedChanged(object sender, EventArgs e)// method to make pen size medium
        {

            size = 5;
            pen = new Pen(color,size);
        }

        private void RBtnLarge_CheckedChanged(object sender, EventArgs e)// method to make pen size large
        {
            size = 25;
            pen = new Pen(color, size);
        }

        private void BtnClear_Click(object sender, EventArgs e)// method to erase the entire drawing pad
        {
            draw.Clear(PanelDraw.BackColor);
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Trying to change paint color and size using radio buttons

Posted 08 April 2012 - 03:24 PM

I think this is why you are getting unexpected results.

Notice you declare a class level variable that is empty.

Color color = new Color();


Then if you change the size first, you directly go into this code:

size = 1;
pen = new Pen(color,size); // "color", set above, is still empty. See?


I'm just guessing here, if you can please share the error message you're receiving. :)

As a tryout, try setting a default value when creating the "color" object.

Color slateBlue = Color.FromName("Black");

Was This Post Helpful? 0
  • +
  • -

#7 cloges4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-April 12

Re: Trying to change paint color and size using radio buttons

Posted 08 April 2012 - 03:51 PM

There is no error messages at all the code complies fine. The problem is the Pen does not seem to be accepting input from the either the size radiobuttons or the color radiobuttons until I click a different color radiobutton first.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1