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
Related
So, i try to learn how to Drag and Drop an Image from one PictureBox to another and that works well. But how can i drag and drop the Image TAG from pictureox1 to picturebox2?
i currently have 3 source images and 3 drop boxes.
the dropbox6 is locked with a countdown after a buttonclick (see button2)
(later i will lock all dropboxes)
after i hit this button a countdown starts and if the countdown is 0 (ZERO) only then i can drag one of the 3 images to this box.
however, i have given each of these 3 images in the souceboxes a TAG name.
how can i drop this tagname also to the dropbox?
here is what i have so far:
(the Label labeled "Counter" is actually Label4 in the code
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 Game
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
// pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Copy);
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox4.AllowDrop = true;
pictureBox5.AllowDrop = true;
pictureBox6.AllowDrop = true;
pictureBox6.Enabled = false;
}
private void pictureBox4_DragEnter(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
}
private void pictureBox4_DragLeave(object sender, EventArgs e)
{
}
private void pictureBox4_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap) e.Data.GetData(DataFormats.Bitmap);
pictureBox4.Image = getPicture;
}
private void pictureBox5_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
}
private void pictureBox5_DragLeave(object sender, EventArgs e)
{
}
private void pictureBox5_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox5.Image = getPicture;
}
private void pictureBox6_DragDrop(object sender, DragEventArgs e)
{
Image getPicture = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox6.Image = getPicture;
timer1.Enabled = false;
}
private void pictureBox6_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
timer1.Enabled = false;
}
}
private void pictureBox6_DragLeave(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void pictureBox4_Click(object sender, EventArgs e)
{
MessageBox.Show("test");
}
private void timer1_Tick(object sender, EventArgs e)
{
counter--;
if (counter == 0)
timer1.Stop();
label4.Text = counter.ToString();
if(counter == 0)
{
pictureBox6.Enabled = true;
label4.Enabled = false;
timer1.Stop();
}
}
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
private void pictureBox6_Click(object sender, EventArgs e)
{
MessageBox.Show(pictureBox6.Tag.ToString());
}
private int counter = 3;
private void button2_Click(object sender, EventArgs e)
{
int counter = 3;
timer1 = new Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000; // 1 second
timer1.Start();
label4.Text = counter.ToString();
if(label4.Text == "0")
{
timer1.Stop();
}
pictureBox6.Image = Properties.Resources.shoreSite_d_1_l_x_x;
button2.Visible=false;
}
}
internal class bild1
{
private Bitmap shoreSite_d_1_l_x_x;
public bild1(Bitmap shoreSite_d_1_l_x_x)
{
this.shoreSite_d_1_l_x_x = shoreSite_d_1_l_x_x;
}
}
}
In MouseDown you are saying what you want to transmit in the DoDragDrop call. In your current case you are transmitting the image ((PictureBox)sender).Image but you can chose to transmit the tag as well...
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Image, DragDropEffects.Copy);
((PictureBox)sender).DoDragDrop(((PictureBox)sender).Tag, DragDropEffects.Copy);
}
...Then make sure to parse for each possible input type in DragDrop
private void pictureBox6_DragDrop(object sender, DragEventArgs e)
{
var image = e.Data.GetData(DataFormats.Bitmap) as Bitmap;
var tag = e.Data.GetData(DataFormats.Text) as string;
if (image != null)
{
pictureBox4.Image = image;
}
if (tag != null)
{
pictureBox4.Tag = tag;
}
timer1.Enabled = false;
}
Beware, that you will need to update all the code that checks that the data is Bitmap before it arrives at DragDrop ie in DragEnter and write code that handles both Bitmap and Text, otherwise the Drag functionality will break.
AWESOME!!
It works now. I will continue this Project.
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'm trying to create a media player that can load MULTIPLE CLIPS at a time.
Just load. Not play.
When you load the program, I was hoping that the user can click "load" and select different files for each of the buttons.
Once the clips have all been loaded, the user can go through and click a "play" button that corresponds with the Load Button.
I also hoped that beside each "play" button, there be a Loop Checkbox (if ticked, it loops the video)
The Program so far:
Current Source:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MediaPlayer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
MediaPly _mp = null;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button2_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button4_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
private void button5_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button6_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
}
}
You are saving all clips to the same variable (_mp).
Try to save each in a different variable:
MediaPly _mp = null;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp = new MediaPly();
}
}
private void button2_Click(object sender, EventArgs e)
{
_mp.LoadFile(openFileDialog1.FileName, this.panel1);
}
MediaPly _mp2 = null;
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp2 = new MediaPly();
}
}
private void button4_Click(object sender, EventArgs e)
{
_mp2.LoadFile(openFileDialog1.FileName, this.panel1);
}
MediaPly _mp3 = null;
private void button5_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
_mp3 = new MediaPly();
}
}
private void button6_Click(object sender, EventArgs e)
{
_mp3.LoadFile(openFileDialog1.FileName, this.panel1);
}
I use virtual COM ports for testing my program. I want to Serial Write with COM8 and Serial read with COM9. When a want to write the values from textbox1, i get this error:
IOException was unhandled (The parameter is incorrect)
How do i get rid of this?
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.IO.Ports;
namespace Flowerpod_User_Interface
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// show list of valid COM ports
foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
{
comboBox1.Items.Add(s);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.WriteLine(textBox1.Text);
}
else
{
MessageBox.Show("SerialPort1 is not open");
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Connect_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
serialPort1.PortName = comboBox1.SelectedItem.ToString();
serialPort1.Open();
textBox3.Text = "Open";
}
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
Random slumpGenerator = new Random();
// Or whatever limits you want... Next() returns a double
int tal = slumpGenerator.Next(1000, 10000);
textBox1.Text = tal.ToString();
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void serialPort1_DataReceived(object sender,SerialDataReceivedEventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
if (!serialPortRead.IsOpen)
{
serialPort1.PortName = "COM9";
serialPortRead.Open();
textBox4.Text = serialPortRead.ReadLine();
}
}
}
}
There wasn't any bridge between the two ports which caused the problem. The virtual COM port software tricked me !.
I greatly appreciate any help. I have 20+ buttons, each with a word, or a space or period. Each time I click on a button, the pre-existing word is wiped out and replaced with the new word. I need each word and/or space to remain in place until I click the "Clear" button.
Maybe this has been previously asked/answered under different search terms? I tend to believe I need to identify a string variable, but have no idea how to begin.
==============
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 frmSentenceBuilder : Form
{
public frmSentenceBuilder()
{
InitializeComponent();
}
private void frmSentenceBuilder_Load(object sender, EventArgs e)
{
}
private void btnA_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnA.Text;
}
private void btn_a_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_a.Text;
}
private void btnAn_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnAn.Text;
}
private void btn_an_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_an.Text;
}
private void btnThe_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnThe.Text;
}
private void btn_the_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_the.Text;
}
private void btnman_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnman.Text;
}
private void btnwoman_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnwoman.Text;
}
private void btndog_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btndog.Text;
}
private void btncat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btncat.Text;
}
private void btncar_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btncar.Text;
}
private void btnbicycle_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbicycle.Text;
}
private void btnbeautiful_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbeautiful.Text;
}
private void btnbig_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbig.Text;
}
private void btnsmall_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnsmall.Text;
}
private void btnstrange_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnstrange.Text;
}
private void btnlookedat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnlookedat.Text;
}
private void btnrode_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnrode.Text;
}
private void btnspoketo_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnspoketo.Text;
}
private void btnlaughedat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnlaughedat.Text;
}
private void btndrove_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btndrove.Text;
}
private void btnSpace_Click(object sender, EventArgs e)
{
lblSentenceText.Text = " ";
}
private void btnperiod_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnperiod.Text;
}
private void btnexclam_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnexclam.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
lblSentenceText.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Use lb1SentenceText.Text+=whatever.Text;.
+= is equivalent to lbSentence.Text = lblSentenceText.Text + whatever.Text.
Basically, it appends or concatenates the right hand side string to the string on the left hand side. Hope it makes sense?
So:
string rhs="Hello " ;
string lhs = "World";
string rhs = rhs + lhs;//Hello World
Please be inspired. You have a lot of redundant code.
btnA.Click += AppendButtonText;
btn_a.Click += AppendButtonText;
...
private void AppendButtonText(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null)
{
lblSentenceText.Text += button.Text;
}
}
On the button click events, change it to += instead of = (except in the clear button). This is equivalent to writing something = something + newValue;.
Try:
lb1SentenceText.Text = lb1SentenceText.Text + *something*.text
The += operator means add to, and the variable modified is equal to the added (Int, String) appended to the original value (x = 1; x += 3; x is now 4)
Try this:
button.Click += new System.EventHandler(ButtonClick);
button1.Click += new System.EventHandler(ButtonClick);
// And for each button, one of those.
private void ButtonClick(object sender, System.EventArgs e)
{
// Do whatever you want to do here, you can place the TEXT to be appended on the button, if so:
lb1SentenceText.Text += sender.Text;
}
//Simple.Create a global variable and within each button click event do this;
string yourStrVar = ""; //Must be global
yourStrVar+= ((Button)sender).Text