Answered Change the color of the drawn figures

Joined
May 25, 2020
Messages
8
Location
Sofia, Bulgaria
Programming Experience
Beginner
here is my form1 and you can see my options buttons:
Без име.png


this is for draw shapes:
C#:
private void drawBtn_Click(object sender, EventArgs e)
        {
            label1.Text = "Click somewhere to draw shape. The shape drawing when click with mouse.";
            option2Moving = false;
            option4ShowAreas = false;
            Form2 drawoptions = new Form2();
            drawoptions.ShowDialog();
        }

and this is form2, where the user selects one of the forms and then enters its' sides:
Без име1.png


when press enter - open form1 and where click with left button of the mouse, it's show the shape.

my question is: how change the color of drawn shape. i create "change color" button and i imagined it - by pressing the button to change the color and a shape like this will appear:

unnamed.gif


and choose one of these colors and change the color of that shape. is it possible what i want? Can someone help me, please?
 
Use a ColorDialog Class (System.Windows.Forms) and create a Pen with the selected Color.
By the way, it is not possible to change the color of drawn shape, but you can draw a new shape with a new color.
 
As @JohnH suggests, you can't change anything about a GDI+ drawing once it's drawn because it's not an object; it's just pixels on a surface. That said, GDI+ is designed such that controls are repeatedly redrawn so you simply use a different colour when you redraw them. You need to store the data that describes your drawing in persistent variables and then, on each Paint event, you draw whatever those variables describe. If you want to be able to change the colour that the user sees, you need to store that colour with the rest of the data. On a Paint event, you create a Pen and/or Brush based on that colour and draw with it.

What I would tend to suggest is that you start with a Shape class that contains the common functionality for all shapes. That might include a Color property, a LineWidth property and an abstract Draw method that has a parameter of type Graphics. You can then inherit that class for each individual shape and add any shape-specific functionality, including overriding the Draw method and using the Graphics object provided to draw the appropriate shape in the appropriate colour with the appropriate line. You would then have a single variable of type List<Shape> and, in the Paint event handler, you can simply loop over that list and call Draw on each one. If you want to change the colour of a shape you simply set the Color property of the appropriate Shape object and then call Invalidate on the control you're drawing them on.
 
As @JohnH suggests, you can't change anything about a GDI+ drawing once it's drawn because it's not an object; it's just pixels on a surface. That said, GDI+ is designed such that controls are repeatedly redrawn so you simply use a different colour when you redraw them. You need to store the data that describes your drawing in persistent variables and then, on each Paint event, you draw whatever those variables describe. If you want to be able to change the colour that the user sees, you need to store that colour with the rest of the data. On a Paint event, you create a Pen and/or Brush based on that colour and draw with it.

What I would tend to suggest is that you start with a Shape class that contains the common functionality for all shapes. That might include a Color property, a LineWidth property and an abstract Draw method that has a parameter of type Graphics. You can then inherit that class for each individual shape and add any shape-specific functionality, including overriding the Draw method and using the Graphics object provided to draw the appropriate shape in the appropriate colour with the appropriate line. You would then have a single variable of type List<Shape> and, in the Paint event handler, you can simply loop over that list and call Draw on each one. If you want to change the colour of a shape you simply set the Color property of the appropriate Shape object and then call Invalidate on the control you're drawing them on.


here is my shape class, you can see what i have:
C#:
abstract class Shapes
    {
        public int a { get; set; }
        public int b { get; set; }
        public int c { get; set; }

        public Point Location { get; set; }
        public Color color { get; set; }
        
        public string PenColor { get; set; }
        public string FillColor { get; set; }
        public int X1 { get; set; }
        public int Y1 { get; set; }
        public int X2 { get; set; }
        public int Y2 { get; set; }

        public abstract void Paint(Graphics graphics);
        public abstract bool ConteinsCordinate(Point point);

        public abstract double P();
        public abstract double S();
    }
 
Didn't you see the code example in that page?
 
Can you tell me how do that, because i searched everywhere on the internet and don't found anything..
You've already been provided with a link to the documentation for the dialogue, so no searching necessary. While you're already at the Microsoft Docs site, maybe look up the Pen type too. You don't always have to find code to copy and paste. If you know what types and/or members you need to use, ALWAYS read the documentation. Once you understand what they can do, you can do it yourself.
 
You've already been provided with a link to the documentation for the dialogue, so no searching necessary. While you're already at the Microsoft Docs site, maybe look up the Pen type too. You don't always have to find code to copy and paste. If you know what types and/or members you need to use, ALWAYS read the documentation. Once you understand what they can do, you can do it yourself.

i udnerstant, but don't know how to write it. i try so many options, but it doesn't work. and now i need some help..
 
i try so many options, but it doesn't work. and now i need some help..
Then show us your best attempt and we can help you fix it. Don't ask us to do everything from scratch for you when, for all we know, you're already 99% of the way there.
 
Then show us your best attempt and we can help you fix it. Don't ask us to do everything from scratch for you when, for all we know, you're already 99% of the way there.

Look, I tried and I can't, just ask for help because I'm a beginner. I'm not asking you to do the whole project for me, I've done what I can. I did't have this task for the project, it came to me as an idea and I wanted to do it for my project. I'm sorry I asked for help when I needed it.
 
Last edited:
Show us some of those attempts that failed.
 
Look, I tried and I can't,
Coming from someone who never forgot what it was like to be a beginner. That's the most defeatist attitude I've ever read. There is no such thing as "I can't". You are restraining your own abilities to progress forward with that type of attitude.

Instead of glancing at it and feeling inferior, read the actual documentation you've been provided with. And then copy and paste the code into your program. Study it, and look up any types, members that don't make sense as already suggested by @jmcilhinney. Once you try and attempt to write this yourself.

Share with us the code you've tried and you will likely be surprised how much help you may actually get in return from the other guys. We like to see users making an effort, but if you are unwilling to show us what you've tried or attempted, then there is little more we can do to help you.

By refusing to show us the code which you have changed or tried to change, you are also limiting us from helping you. :)


 
Coming from someone who never forgot what it was like to be a beginner. That's the most defeatist attitude I've ever read. There is no such thing as "I can't". You are restraining your own abilities to progress forward with that type of attitude.

Instead of glancing at it and feeling inferior, read the actual documentation you've been provided with. And then copy and paste the code into your program. Study it, and look up any types, members that don't make sense as already suggested by @jmcilhinney. Once you try and attempt to write this yourself.

Share with us the code you've tried and you will likely be surprised how much help you may actually get in return from the other guys. We like to see users making an effort, but if you are unwilling to show us what you've tried or attempted, then there is little more we can do to help you.

By refusing to show us the code which you have changed or tried to change, you are also limiting us from helping you. :)




In fact, what I want to get with my code I don't even know if it's possible. Let me just ask you one last thing ... which of my code do you want me to send you? Тhe code in which there are 100 or the one in which there are 200 errors? Plus, I don't know how to send code that I don't have anymore because it's deleted. Nevermind, thank you. :)
 
which of my code do you want me to send you? Тhe code in which there are 100 or the one in which there are 200 errors?
Either one would have helped us try to figure out which approach you were taking to try to solve the problem you ran into.
 
Back
Top Bottom