I need the comboBox index to change the color of the brush/pen when it is selected, I know it should be a simple fix, but I cant seem to get it.
public partial class Form1 : Form
{
private bool penDown = false;
private int radius = 5;
private SolidBrush brush = new SolidBrush(Color.IndianRed);
private Color[] colors = { Color.IndianRed, Color.Blue, Color.Green, Color.Yellow, Color.Purple};
public Form1()
{
InitializeComponent();
}
private void btnClr_Click(object sender, EventArgs e)
{
Graphics g = panel1.CreateGraphics();
g.Clear(panel1.BackColor);
g.DrawImage(panel1.BackgroundImage, panel1.ClientRectangle, 0, 0, panel1.BackgroundImage.Width,
panel1.BackgroundImage.Height, GraphicsUnit.Pixel);
g.Dispose();
}
private void btnQuit_Click(object sender, EventArgs e)
{
base.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (!penDown)
{
return;
}
Graphics g = panel1.CreateGraphics();
g.FillEllipse(brush, new Rectangle(e.X - radius, e.Y - radius, 2 * radius, 2 * radius));
g.Dispose();
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
penDown = true;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
penDown = false;
}
private void colorCB_SelectedIndexChanged(object sender, EventArgs e)
{
//int i = ((color
}
}
Just use the combo box SelectedIndex property to retrieve the color from your colors array:
private void colorCB_SelectedIndexChanged(object sender, EventArgs e)
{
brush = new SolidBrush(colors[colorCB.SelectedIndex]);
}
Related
This is my code and my goal is to make the cloned images(Bitmap) opacity adjustable using the color matrix however, the tutorial on YouTube is insufficient for me hence, I need help on my project. Thank you very much in advance for helping me! If there are questions regarding on my project, just comment in and I will try to reply as soon as possible guys.
This is my code:
private FilterInfoCollection CaptureDevices;
private VideoCaptureDevice videoSource;
private void Form1_Load(object sender, EventArgs e)
{
{
CaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevices)
{
comboBox1.Items.Add(Device.Name);
comboBox2.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
videoSource = new VideoCaptureDevice();
comboBox2.SelectedIndex = 0;
videoSource = new VideoCaptureDevice();
}
}
private void s1_Click(object sender, EventArgs e)
{
videoSource = new VideoCaptureDevice(CaptureDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrame);
videoSource.Start();
}
private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void s2_Click(object sender, EventArgs e)
{
videoSource = new VideoCaptureDevice(CaptureDevices[comboBox2.SelectedIndex].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(VideoSource_NewFrames);
videoSource.Start();
}
private void VideoSource_NewFrames(object sender, NewFrameEventArgs eventArgs)
{
pictureBox2.Image = (Bitmap)eventArgs.Frame.Clone();
}
private void r1_Click(object sender, EventArgs e)
{
videoSource.Stop();
pictureBox1.Image = null;
pictureBox1.Invalidate();
pictureBox3.Image = null;
pictureBox3.Invalidate();
}
private void r2_Click(object sender, EventArgs e)
{
videoSource.Stop();
pictureBox2.Image = null;
pictureBox2.Invalidate();
pictureBox4.Image = null;
pictureBox4.Invalidate();
}
private void c1_Click(object sender, EventArgs e)
{
pictureBox3.Image = (Bitmap)pictureBox1.Image.Clone();
}
private void c2_Click(object sender, EventArgs e)
{
pictureBox4.Image = (Bitmap)pictureBox2.Image.Clone();
}
I have created a Desktop app using Windows Form (c#) in visual Studio 2019.first, in my form I created a panel and several buttons for navigation. for the crud, I have created a user control and now I need to get this user control to the form but it shows in the toolbox neither in the codes
form Home
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WinFormsApp1
{
public partial class Home : Form
{
static Home _obj;
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);
public static Home Instance
{
get {
if (_obj == null) {
_obj = new Home();
}
return _obj;
}
}
public Home()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
panel2.Height = 46;
btnSideAdd.Visible = false;
_obj = this;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
public Button BtnSideAdd {
get { return btnSideAdd; }
set { btnSideAdd = value; }
}
public Panel PanelControl1
{
get { return PanelControl1; }
set { PanelControl1 = value; }
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
sidePanel.Height = button4.Height;
sidePanel.Top = button4.Top;
}
private void button5_Click(object sender, EventArgs e)
{
sidePanel.Height = button5.Height;
sidePanel.Top = button5.Top;
}
private void button7_Click(object sender, EventArgs e)
{
sidePanel.Height = button7.Height;
sidePanel.Top = button7.Top;
}
private void button10_Click(object sender, EventArgs e)
{
sidePanel.Height = button10.Height;
sidePanel.Top = button10.Top;
}
private void button2_Click(object sender, EventArgs e)
{
sidePanel.Height = button2.Height;
sidePanel.Top = button2.Top;
}
private void button1_Click(object sender, EventArgs e)
{
sidePanel.Height = btnSideAdd.Height;
sidePanel.Top = btnSideAdd.Top;
}
private void button8_Click(object sender, EventArgs e)
{
if (panel2.Height == 140)
{
panel2.Height = 46;
}
else
{
panel2.Height = 140;
}
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
}
private void panel2_Paint_1(object sender, PaintEventArgs e)
{
}
private void button11_Click(object sender, EventArgs e)
{
}
private void button12_Click(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
sidePanel.Height = button6.Height;
sidePanel.Top = button6.Top;
}
private void button3_Click(object sender, EventArgs e)
{
sidePanel.Height = button3.Height;
sidePanel.Top = button3.Top;
}
private void button9_Click(object sender, EventArgs e)
{
sidePanel.Height = button9.Height;
sidePanel.Top = button9.Top;
}
}
}
user control AddWorks
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WinFormsApp1.Asram
{
public partial class AddWork : UserControl
{
public AddWork()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
Im a beginner and this my assignment project I need to fix this so that I can move in to other parts
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MiniPaint
{
public partial class Form1 : Form
{
Graphics g;
Pen p = new Pen(Color.Black, 1);
Point sp = new Point(0, 0);
Point ep = new Point(0, 0);
int k = 0;
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void red_Click(object sender, EventArgs e)
{
p.Color = red.BackColor;
default1.BackColor = red.BackColor;
}
private void green_Click(object sender, EventArgs e)
{
p.Color = green.BackColor;
default1.BackColor = green.BackColor;
}
private void blue_Click(object sender, EventArgs e)
{
p.Color = blue.BackColor;
default1.BackColor = blue.BackColor;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
sp = e.Location;
if (e.Button == MouseButtons.Left);
k = 1;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
k = 0;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (k == 1)
{
ep = e.Location;
g = this.CreateGraphics();
g.DrawLine(p, sp, ep);
}
sp = ep;
}
private void del1_Click(object sender, EventArgs e)
{
g.Clear(Color.White);
}
private void yellow_Click(object sender, EventArgs e)
{
p.Color = yellow.BackColor;
default1.BackColor = yellow.BackColor;
}
private void purple_Click(object sender, EventArgs e)
{
p.Color = purple.BackColor;
default1.BackColor = purple.BackColor;
}
private void brown_Click(object sender, EventArgs e)
{
p.Color = brown.BackColor;
default1.BackColor = brown.BackColor;
}
private void black_Click(object sender, EventArgs e)
{
p.Color = black.BackColor;
default1.BackColor = black.BackColor;
}
private void nud1_ValueChanged(object sender, EventArgs e)
{
}
private void white_Click(object sender, EventArgs e)
{
p.Color = white.BackColor;
default1.BackColor = white.BackColor;
}
}
}
I'm almost done with my minipaint code in c#. I wanted to add one more thing to it and it's that I can change the thickness(width) of the line im going to draw via numericupadown box and I'm trying to make it work for over an hour. Can someone help me with it please and is there a simple way of doing it? ( nud1 is the numereric updown box)
On numeric up down value changed event, change the thickness of the pen you're using, I would recommend storing the current color in a variable that you can assign back to your pen on the value changed event when you re-initialize.
This MSDN link shows details on the Pen class from System.Drawing.
p = new Pen(Color, (float)nud1.Value);
I have created a winform application for school project. But in certain situation I want to let user to move the form only vertically by dragging the form.
So, I have tried this.
private bool dragging = false;
private Point pointClicked;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
Point pointMoveTo;
pointMoveTo = this.PointToScreen(new Point(e.Y,e.Y));
pointMoveTo.Offset(-pointClicked.X, -pointClicked.Y);
this.Location = pointMoveTo;
}
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
pointClicked = new Point(e.X, e.Y);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
dragging = false;
}
But it doesn't seem to work. It moves the form across the screen. So, is there a way to restrict form movement to vertical?
You should not set this.Location, but only the this.Location.Y value:
this.Location = pointToMoveTo;
should become
this.Top = pointToMoveTo.Y;
Your original code changed both the X and Y coordinates, effectively doing a diagonal move.
Try something like this:
public partial class Form1 : Form
{
private bool dragging = false;
private int pointClickedY;
public Form1() {
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e) {
dragging = true;
pointClickedY = e.Y;
}
private void Form1_MouseUp(object sender, MouseEventArgs e) {
dragging = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e) {
if (dragging) {
int pointMoveToY = e.Y;
int delta = pointMoveToY - pointClickedY;
Location = new Point(Location.X, Location.Y + delta);
}
}
}
I have a simple bitmap editor for small monochrome bitmaps. And I have a menus which allows to choose thickness of the pen and robber. The problem is that I don't like the following code. Is there any way to make it more compact?
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 3;
}
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 2;
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 1;
}
private void toolStripMenuItem5_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 4;
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 5;
}
private void toolStripMenuItem7_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 6;
}
private void toolStripMenuItem8_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 7;
}
private void toolStripMenuItem9_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 8;
}
private void toolStripButton3_Click_1(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.Clear;
}
private void toolStripDropDownButton2_Click(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.FreeHand;
}
private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
{
CurrentPanel.EditMode = EditMode.Clear;
}
private void toolStripMenuItem10_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 1;
}
private void toolStripMenuItem11_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 2;
}
private void toolStripMenuItem12_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 3;
}
private void toolStripMenuItem13_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 4;
}
private void toolStripMenuItem14_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 5;
}
private void toolStripMenuItem15_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 6;
}
private void toolStripMenuItem16_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 7;
}
private void toolStripMenuItem17_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 8;
}
private void toolStripMenuItem18_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 16;
}
private void toolStripMenuItem19_Click(object sender, EventArgs e)
{
CurrentPanel.RobberThickness = 32;
}
private void toolStripMenuItem21_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 32;
}
private void toolStripMenuItem20_Click(object sender, EventArgs e)
{
CurrentPanel.PenThickness = 16;
}
private void toolStripMenuItemCommon_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem) sender;
int thickness = (int)item.Tag;
CurrentPanel.PenThickness = thickness ;
}
Of course you need to initialize Tag of each ToolStripMenuItem and set toolStripMenuItemCommon_Click as a handler for Click event. You can do it in a for cycle for example:
for(int i = 1; i < 8; i++)
{
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = "Set thickness: " + i;
item.Click += toolStripMenuItemCommon_Click;
item.Tag = i;
// add item to strip container
}
Yes, there is:
private void SetPenThickness(int thickness)
{
CurrentPanel.PenThickness = thickness;
}
toolStripMenuItem1.Click += (s,e) => SetPenThickness(1);
toolStripMenuItem2.Click += (s,e) => SetPenThickness(2);
toolStripMenuItem3.Click += (s,e) => SetPenThickness(3);
toolStripMenuItem4.Click += (s,e) => SetPenThickness(4);
// ...
Try this:
private void toolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem) sender;
switch (toolStripMenuItem.ID)
{
case "toolStripMenuItem4":
{
CurrentPanel.PenThickness = 3;
break;
}
..........
..........
}
}