I'm making a program and I have a lot of labels that when you click on them they do the same thing and instead of writing them around 40 times is there a way to reduce it
private: System::Void lblHB15_Click(System::Object^ sender, System::EventArgs^ e) {
if (HBColumn1 == true) {
drop (1);
}
if (row1 == 6) {
btnRow1->Enabled = false;
HBColumn1 = false;
}
}
Related
textBox3 is just the result textBox , all i want is a validation check for textBox1 and textBox2 so the only input is numbers
#pragma endregion
private: System::Void Calculate(System::Object^ sender, System::EventArgs^ e) {
if (sender == button_plus) {
textBox3->Text = Convert::ToString(Convert::ToDouble(textBox1->Text) + Convert::ToDouble(textBox2->Text));
}
else if (sender == button_minus) {
textBox3->Text = Convert::ToString(Convert::ToDouble(textBox1->Text) - Convert::ToDouble(textBox2->Text));
}
else if (sender == button_multi) {
textBox3->Text = Convert::ToString(Convert::ToDouble(textBox1->Text) * Convert::ToDouble(textBox2->Text));
}
else if (sender == button_div) {
textBox3->Text = Convert::ToString(Convert::ToDouble(textBox1->Text) / Convert::ToDouble(textBox2->Text));
}
else if (sender == button_exit) {
this->Close();
}
}
I fianlly figured it out i just make a try, catch and solve this one
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
double num1;
try {
num1 = System::Double::Parse(this->textBox1->Text);
}
catch (System::Exception^ exception) {
num1 = 0;
}
this->textBox1->Text = num1.ToString();}
private: System::Void textBox2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
double num2;
try {
num2 = System::Double::Parse(this->textBox2->Text);
}
catch (System::Exception^ exception) {
num2 = 0;
}
this->textBox2->Text = num2.ToString();}
I'm a new developer.I'm trying to make a simple program in which we can draw multiple shapes like Rectangle, Ellipse, Line, etc. The problem which I'm facing right now is that when I click the mouse and drag to draw on mouseDown and mouseMove event I'm not able to see the shape. The shape appears on the pictureBox only when I leave the mouse.
I want the shape to appear and move with the mouse as the user draws it just like in paint application by Microsoft.
I tried using Refresh() function while drawing but it removes the old shapes which was already drawn in the pictureBox.
Below is the code I'm using. need some suggestions.
bool draw = false;
bool isMouseDown = false;
int x, y, lx, ly = 0;
Item currItem;
Pen pen = new Pen(Color.Red,1);
public enum Item
{
Rectangle, Ellipse, Line, Pencil, Eraser
}
private void btnROIDraw_Click(object sender, EventArgs e)
{
if (menuStripRoi.Visible == false)
{
menuStripRoi.Visible = true;
}
else
{
menuStripRoi.Visible = false;
draw = false;
}
}
private void pictureBoxROI_MouseDown(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
isMouseDown = true;
//draw = true;
}
private void pictureBoxROI_MouseUp(object sender, MouseEventArgs e)
{
lx = e.X;
ly = e.Y;
if (draw)
{
Graphics g = pictureBoxROI.CreateGraphics();
switch (currItem)
{
case Item.Rectangle:
g.DrawRectangle(pen, x, y, e.X - x, e.Y - y);
break;
case Item.Ellipse:
g.DrawEllipse(pen, x, y, e.X - x, e.Y - y);
break;
case Item.Line:
g.DrawLine(pen, x, y, e.X, e.Y);
break;
}
g.Dispose();
}
isMouseDown = false;
}
private void pictureBoxROI_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
if (draw)
{
Graphics g = pictureBoxROI.CreateGraphics();
switch (currItem)
{
case Item.Pencil:
g.FillEllipse(new SolidBrush(Color.Red), e.X - x + x, e.Y - y + y, 2, 2);
break;
case Item.Eraser:
g.FillEllipse(new SolidBrush(pictureBoxROI.BackColor), e.X - x + x, e.Y - y + y, 20, 20);
break;
}
g.Dispose();
}
}
}
private void pictureBoxROI_Paint(object sender, PaintEventArgs e)
{
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBoxROI.Refresh();
}
private void rectangleToolStripMenuItem_Click(object sender, EventArgs e)
{
currItem = Item.Rectangle;
draw = true;
}
private void ellipseToolStripMenuItem_Click(object sender, EventArgs e)
{
currItem = Item.Ellipse;
draw = true;
}
private void lineToolStripMenuItem_Click(object sender, EventArgs e)
{
currItem = Item.Line;
draw = true;
}
private void pencilToolStripMenuItem_Click(object sender, EventArgs e)
{
currItem = Item.Pencil;
draw = true;
}
private void eracerToolStripMenuItem_Click(object sender, EventArgs e)
{
currItem = Item.Eraser;
draw = true;
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBoxROI.Refresh();
pictureBoxROI.Image = null;
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog o = new OpenFileDialog();
o.Filter = "Png files|*.png|jpeg files|*jpg|bitmaps|*.bmp";
if (o.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pictureBoxROI.Image = (Image)Image.FromFile(o.FileName).Clone();
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(pictureBoxROI.Width, pictureBoxROI.Height);
Graphics g = Graphics.FromImage(bmp);
Rectangle rect = pictureBoxROI.RectangleToScreen(pictureBoxROI.ClientRectangle);
g.CopyFromScreen(rect.Location, Point.Empty, pictureBoxROI.Size);
g.Dispose();
SaveFileDialog s = new SaveFileDialog();
s.Filter = "Png files|*.png|jpeg files|*jpg|bitmaps|*.bmp";
if (s.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (File.Exists(s.FileName))
{
File.Delete(s.FileName);
}
if (s.FileName.Contains(".jpg"))
{
bmp.Save(s.FileName, ImageFormat.Jpeg);
}
else if (s.FileName.Contains(".png"))
{
bmp.Save(s.FileName, ImageFormat.Png);
}
else if (s.FileName.Contains(".bmp"))
{
bmp.Save(s.FileName, ImageFormat.Bmp);
}
}
}
Found a solution to my question.
This article explains in detail everything about drawing with GDI+. Worth having a look
http://www.codeproject.com/Articles/17893/Extensions-to-DrawTools
I am trying to pass the textBox1 value from Form5 to Form1. However it does not seem to remember or see the value when it gets to Form1. I want paf_path to be what paf_path2 is.
System::Void Form5::button1_Click(System::Object^ sender, System::EventArgs^ e) {
System::String^ paf_path2 = textBox1->Text;
FolderBrowserDialog^ folderBrowserDialog1;
folderBrowserDialog1 = gcnew System::Windows::Forms::FolderBrowserDialog;
folderBrowserDialog1->Description = L"Select the directory of your MAF files ";
folderBrowserDialog1->ShowNewFolderButton = false;
// Show the FolderBrowserDialog.
System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog();
if (result == ::DialogResult::OK)
{
paf_path2 = folderBrowserDialog1->SelectedPath;
}
textBox1->Text = paf_path2;
}
System::Void Form5::button2_Click(System::Object^ sender, System::EventArgs^ e){
Form1 ^dos3 = gcnew Form1(this);
dos3->Show();
this->Hide();
}
System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e) {
System::String^ test = textBox1->Text;
if (test == "")
{
MessageBox::Show("Please enter partial address!");
}
else
{
Form5^doss = gcnew Form5(this);
System::String^ paf_path2 = doss->textBox1->Text;
char* paf_path = (char*) (void*) Marshal::StringToHGlobalAnsi(paf_path2);
This program should work like join the dots , the user selects the number of dots to display and is able to draw on the canvas, the problem is with the save , whenever i try to save it isnt working at all , it gives me a black rectangle..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Painting2
{
public partial class Form1 : Form
{
Bitmap drawing;
bool draw = false;
Color C = Color.Black;
int size = 14;
bool startdraw = false;
public Form1(string parameter1)
{
InitializeComponent();
this.Text = parameter1;
}
private void Form1_Load(object sender, EventArgs e)
{
drawing = new Bitmap (pnlcanvas.Width, pnlcanvas.Height, pnlcanvas.CreateGraphics());
cbdots.Items.Add("2");
cbdots.Items.Add("3");
cbdots.Items.Add("4");
cbdots.Items.Add("5");
cbdots.Items.Add("6");
cbdots.Items.Add("7");
cbdots.Items.Add("8");
cbdots.Items.Add("9");
cbdots.Items.Add("10");
cbdots.Items.Add("11");
cbdots.Items.Add("12");
cbdots.Items.Add("13");
cbdots.Items.Add("14");
cbdots.Items.Add("15");
}
private void pnlcanvas_MouseDown(object sender, MouseEventArgs e)
{
draw = true;
Graphics g = Graphics.FromImage(drawing);
SolidBrush b = new SolidBrush(C);
if (startdraw == true)
{
g.FillEllipse(b, e.X, e.Y, size, size);
pnlcanvas.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
}
}
private void pnlcanvas_MouseUp(object sender, MouseEventArgs e)
{
draw = false;
}
private void pnlcanvas_Move(object sender, EventArgs e)
{
}
private void pnlcanvas_MouseMove(object sender, MouseEventArgs e)
{
if (draw == true && startdraw == true)
{
Graphics g = Graphics.FromImage(drawing);
SolidBrush b = new SolidBrush(C);
g.FillEllipse(b, e.X, e.Y, size, size);
pnlcanvas.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
}
}
private void btncolor_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
C = Color.White;
size = 25;
}
private void button2_Click(object sender, EventArgs e)
{
drawing = new Bitmap(pnlcanvas.Width, pnlcanvas.Height, pnlcanvas.CreateGraphics());
pnlcanvas.CreateGraphics().Clear(Color.White);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btnsave_Click(object sender, EventArgs e)
{
//if (saveDialog.ShowDialog() == DialogResult.OK)
//{
// Bitmap tosave = new Bitmap(pnlcanvas.Width, pnlcanvas.Height);
// pnlcanvas.DrawToBitmap(tosave, new Rectangle(0, 0, pnlcanvas.Width, pnlcanvas.Height));
// tosave.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
//}
using (Bitmap bitmap = new Bitmap(pnlcanvas.ClientSize.Width,
pnlcanvas.ClientSize.Height))
{
pnlcanvas.DrawToBitmap(bitmap, pnlcanvas.ClientRectangle);
bitmap.Save(#"C:\Users\xejns_000\Desktop\TestDrawToBitmap.bmp", ImageFormat.Bmp);
}
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void btnstart_Click(object sender, EventArgs e)
{
DialogResult result = cd.ShowDialog();
if (result == DialogResult.OK)
{
C = cd.Color;
}
startdraw = true;
}
private void cbdots_SelectedIndexChanged(object sender, EventArgs e)
{
drawing = new Bitmap(pnlcanvas.Width, pnlcanvas.Height, pnlcanvas.CreateGraphics()); //clearing
pnlcanvas.CreateGraphics().Clear(Color.White); //clearing
Random r = new Random();
for (int i = 0; i <= Convert.ToInt32(cbdots.SelectedItem.ToString()) -1; i++)
{
Color RandC = Color.Black; ;
int randomcolor = r.Next(1, 11);
int randomx = r.Next(10, 730);
int randomy = r.Next(10, 390);
switch (randomcolor) // choosing random color
{
case 1:
RandC = Color.PaleVioletRed;
break;
case 2:
RandC = Color.PowderBlue;
break;
case 3:
RandC = Color.Fuchsia;
break;
case 4:
RandC = Color.DarkGoldenrod;
break;
case 5:
RandC = Color.LavenderBlush;
break;
case 6:
RandC = Color.Tomato;
break;
case 7:
RandC = Color.SpringGreen;
break;
case 8:
RandC = Color.Wheat;
break;
case 9:
RandC = Color.Salmon;
break;
case 10:
RandC = Color.LightSteelBlue;
break;
}
Graphics g = Graphics.FromImage(drawing); // painting dots
SolidBrush b = new SolidBrush(RandC); // painting dots
g.FillEllipse(b, randomx, randomy, 40, 40); // painting dots
pnlcanvas.CreateGraphics().DrawImageUnscaled(drawing, new Point(0, 0));
}
}
}
}
I have a little problem with my script. I would like to check if the button1 was clicked in another event (pictureBox_Click). How could I do it?
It should works like this:
private: System::Void pictureBox_Click(System::Object^ sender, System::EventArgs^ e) {
if (button1 is clicked=true)
{
code;
code;
code;
}
if (button2 is clicked=true)
{
code;
code;
code;
}
}
I will be grateful for help.
You'd need to store when the button was clicked in a variable. Add an event handler for the button click events, and store the values.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
buttonClicked = true;
}
private: System::Void pictureBox_Click(System::Object^ sender, System::EventArgs^ e)
{
if (buttonClicked)
{
// ...