I am working in Win Forms here. I have a panel that I've coded to allow for freehand drawing. This is going to act as a box for users to sign their names. Also, I have a combobox that is pulling names from a datagridview. This is how I'd like it to work:
1.) User selects one of the names from combo box
2.) User then "signs" the panel and clicks a button to transfer the "signature"
3.) The signature shows up in a signature column in the same row as the selected name from the combo box.
Does that make sense?
If there's an easier way to do this, please let me know. I am a total novice when it comes to Win forms/coding in general. I'm so grateful for any assistance!
I tried finding code for this in a listview. I tried doing a picturebox instead of a panel.
Here is the code that I have so far that is at least somewhat relevant, I believe:
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Data;
using System.Windows.Forms;
using System.IO;
using PagedList;
using MySql.Data.MySqlClient;
namespace QTRSIGNINSHEETv_0._5
{
public partial class Form1 : Form
{
public Point current = new Point();
public Point old = new Point();
public Graphics g;
public Graphics graph;
public Pen pen = new Pen(Color.Black, 5);
Bitmap surface;
Bitmap memoryImage;
DataTable table = new DataTable();
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
g = canvasPanel.CreateGraphics();
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
pen.SetLineCap(System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.LineCap.Round, System.Drawing.Drawing2D.DashCap.Round);
surface = new Bitmap(canvasPanel.Width, canvasPanel.Height);
graph = Graphics.FromImage(surface);
canvasPanel.BackgroundImage = surface;
canvasPanel.BackgroundImageLayout = ImageLayout.None;
}
//Ignore this, it doesn't work currently.
private void button1_Click(object sender, EventArgs e)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = textBox2.Text + ", " + textBox1.Text;
dataGridView1.Rows[n].Cells[1].Value = "";
dataGridView1.Rows[n].Cells[2].Value = "2";
dataGridView1.Rows[n].Cells[3].Value = "630";
dataGridView1.Rows[n].Cells[4].Value = "50";
dataGridView1.Rows[n].Cells[5].Value = "50";
dataGridView1.Rows[n].Cells[6].Value = "50";
dataGridView1.Rows[n].Cells[7].Value = "10";
dataGridView1.Rows[n].Cells[8].Value = "60";
dataGridView1.Rows[n].Cells[9].Value = "0";
dataGridView1.Rows[n].Cells[10].Value = "30";
dataGridView1.Rows[n].Cells[11].Value = "20";
dataGridView1.Rows[n].Cells[12].Value = "0";
}
private void canvas_MouseDown(object sender, MouseEventArgs e)
{
old = e.Location;
}
private void canvas_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
current = e.Location;
g.DrawLine(pen, old, current);
graph.DrawLine(pen, old, current);
old = current;
}
}
private void button2_Click(object sender, EventArgs e)
{
graph.Clear(Color.White);
canvasPanel.Invalidate();
}
//Pulls names from datagridview1 and puts them in combobox.
private void button4_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
foreach (DataGridViewRow row in dataGridView1.Rows)
if (row.Cells[0].Value != null)
{
comboBox1.Items.Add(row.Cells[0].Value);
}
}
private void button5_Click(object sender, System.EventArgs e)
{
this.dataGridView1.ClearSelection();
int DGVOriginalHeight = dataGridView1.Height;
dataGridView1.Height = (dataGridView1.RowCount * dataGridView1.RowTemplate.Height) + dataGridView1.ColumnHeadersHeight;
using (Bitmap bitmap = new Bitmap(dataGridView1.Width, dataGridView1.Height))
{
string filedate = "QTR_" + DateTime.Now.ToString(" MM.dd.yyyy") + ".png";
dataGridView1.DrawToBitmap(bitmap, new Rectangle(Point.Empty, dataGridView1.Size));
string DesktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
bitmap.Save(Path.Combine(DesktopFolder, filedate), ImageFormat.Png);
}
dataGridView1.Height = DGVOriginalHeight;
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
printDocument1.DefaultPageSettings.Landscape = true;
Bitmap imagebmp = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(imagebmp, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
e.Graphics.DrawImage(imagebmp, 0, 0);
}
private void button6_Click(object sender, EventArgs e)
{
printDocument1.DefaultPageSettings.Landscape = true;
this.dataGridView1.ClearSelection();
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
private void Form1_Load(object sender, EventArgs e)
{
ActiveControl = dataGridView1;
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Signature", typeof(string));
table.Columns.Add("In", typeof(int));
table.Columns.Add("Out", typeof(int));
table.Columns.Add("Academics", typeof(int));
table.Columns.Add("Life Skills", typeof(int));
table.Columns.Add("Mentoring", typeof(int));
table.Columns.Add("Parental Involvement", typeof(int));
table.Columns.Add("Sports & Recreation", typeof(int));
table.Columns.Add("Service Learning", typeof(int));
table.Columns.Add("STEM", typeof(int));
table.Columns.Add("Meal", typeof(int));
table.Columns.Add("Paid Employee", typeof(int));
table.Columns.Add("Non-Enrolled", typeof(bool));
dataGridView1.DataSource = table;
}
private void btnPrevious_Click(object sender, EventArgs e)
{
}
private void btnNext_Click(object sender, EventArgs e)
{
}
//Pulling data from text file
private void button7_Click_1(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(@"C:\Users\Owner\Desktop\Programming\C#\TABLE.txt");
string[] values;
for (int i = 0; i < lines.Length; i++)
{
values = lines[i].ToString().Split('/');
string[] row = new string[values.Length];
for (int j = 0; j < values.Length; j++)
{
row[j] = values[j].Trim();
}
table.Rows.Add(row);
}
}
}
}