Subsequent displays of custom Tooltip has ugly black edging around border - c#

I created a simple Tooltip class which I'd like to enhance with more features but with just a basic Tooltip I'm running into an issue of it getting an ugly edging along the border when it appears on the second time or thereafter. The first time it displays, it appears correct.
Can anyone tell me why this is happening?
using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
class CToolTip : ToolTip
{
public CToolTip()
{
this.OwnerDraw = true;
this.Popup += new PopupEventHandler(this.OnPopup);
this.Draw += new DrawToolTipEventHandler(this.OnDraw);
}
private void OnPopup(object sender, PopupEventArgs e)
{
e.ToolTipSize = new Size(200, 200);
}
private void OnDraw(object sender, DrawToolTipEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.FillRectangle(Brushes.LightYellow, new Rectangle(0, 0, 200, 200));
}
}
I added the CToolTip and a button to a form. Added an event handler for the hover event and display it on a button hover.
this.button1.MouseHover += new System.EventHandler(this.button1_MouseHover);
private void button1_MouseHover(object sender, EventArgs e)
{
cTip.SetToolTip(button1, "This is a test");
}

It ended up being this line. When I removed it, the problem went away.
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;

Related

Picture box not displaying Emgu.cv.mat.toBitmap image

Was following a tutorial on YouTube and tried to get my program to display the live video feed from my webcam into the forms picture box but for some reason the webcam comes on but the picture box isn't displaying anything
I have tried checking if the Mat class was returning null.
but it isn't this is my 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;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;
namespace FaceRecognitionAttandanceSystem
{
public partial class StudentAdd : Form
{
public string fileName { get; set; }
VideoCapture capture;
public StudentAdd()
{
InitializeComponent();
}
//Open Folder
private void metroButton3_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false };
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//open file explorer
fileName = openFileDialog.FileName;
//pick image from file
Image img = Image.FromFile(fileName);
//Rotates Image by 90degrees
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
pictureBox2.Image = img;
}
}
}
//Capture Image
private void metroButton4_Click(object sender, EventArgs e)
{
//If capture is empty start new video capture
if(capture == null)
{
capture = new Emgu.CV.VideoCapture(0);
}
capture.ImageGrabbed += Capture_ImageGrabbed;
capture.Start();
}
private void Capture_ImageGrabbed(object sender, EventArgs e)
{
try
{
Mat mat = new Mat();
if(mat == null)
{
Console.WriteLine("Here you Go");
}
capture.Retrieve(mat);
pictureBox1.Image = mat.ToImage<Bgr, Byte>().ToBitmap<Bgr, Byte>();
}
catch(Exception)
{
}
}
private void metroButton5_Click(object sender, EventArgs e)
{
}
}
}
The problem is very likely that the Capture_ImageGrabbed event is raised on a background thread. And you can only update the UI on the UI thread. You can place a break point in the event-handler and check the thread-debug window to confirm. To fix this you need to move execution to the UI thread. Try:
capture.Retrieve(mat);
var bitmap = mat.ToImage<Bgr, Byte>().ToBitmap<Bgr, Byte>();
pictureBox1.Invoke(() => pictureBox1.Image = bitmap );
You might need to write Invoke((Action)(() => pictureBox1.Image = bitmap) ), since I think there is some issues with the overload resolution of lambdas with the invoke-method.

C# PrintPage function from another class

Problem
I have an issue with moving the PrintPage function (SampleForm_PrintPage) to a new class (PrintPageDesign) also design of the PrintPage uses data from the main form and i have not been able to pull the data in to the new class.
Why?
I'm moving all PrintPage functions to individual classes as there are multiple page designs required in the application, having them all in the same main form seems hard to review and update when each page design requires any change.
Sample Code
To simplify my problem i have created a sample solution in visual basic,
Form1.cs (form code):
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace Sample_Print
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void BTN_Print_Click(object sender, EventArgs e)
{
PrintDialog PD_SamplePage = new PrintDialog();
PrintDocument Doc_SamplePage = new PrintDocument();
Doc_SamplePage.PrintPage += SampleForm_PrintPage;
PD_SamplePage.Document = Doc_SamplePage;
Doc_SamplePage.Print();
}
protected void SampleForm_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.CompositingMode = CompositingMode.SourceOver;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
e.Graphics.DrawString(TB_Name.Text.ToString(), new Font("Roboto Condensed",12, FontStyle.Bold), Brushes.Black, 10, 10);
}
}
}
Requirement
i would like to move function
SampleForm_PrintPage
to class PrintPageDesign , currently there is only visual studio generated code is in the class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sample_Print
{
class PrintPageDesign
{
}
}
i have tried several ways to get the value from the text box outside of the main form but resulted in null.
any help is highly appreciated.
As commented above, you can use partial classes to separate the Main Form members, methods and functionalities.
Press Shift+Alt+C to add a new class. Rename the file to PrintPageDesign and hit Add.
In the new class, add the partial modifier and change the name to Main (the exact name of the main Form). Note, we are creating a partial class here and not deriving from the Main form.
Now you are within the Main Form context and you can access its members.
Example
The Main Form class:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace Sample_Print
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void BTN_Print_Click(object sender, EventArgs e) => PrintJob1();
}
}
The PrintPageDesign class:
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.ComponentModel;
namespace Sample_Print
{
partial class Main
{
private void PrintJob1(bool preview = false)
{
using (var doc = new PrintDocument())
{
doc.PrintPage += (s, e) =>
{
var g = e.Graphics;
var r = new Rectangle(e.MarginBounds.X, e.MarginBounds.Y,
e.MarginBounds.Width, 32);
using (var sf = new StringFormat())
using (var fnt = new Font("Roboto Condensed", 12, FontStyle.Bold))
{
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(TB_Name.Text, fnt, Brushes.Black, r, sf);
r.Y += r.Height;
foreach (Control c in Controls)
{
g.DrawString(c.Name, fnt, Brushes.Black, r, sf);
r.Y += r.Height;
}
// ...
}
};
if (preview)
using (var ppd = new PrintPreviewDialog() { Document = doc })
ppd.ShowDialog();
else
{
using (var pd = new PrintDialog() { Document = doc })
{
if (pd.ShowDialog() == DialogResult.OK)
pd.Document.Print();
}
}
}
}
}
}

Writing to CSV File in C#

I am creating a clock for clocking into a business. Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
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;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
String Code;
String Name;
String InOut;
Boolean Luke = true;
String csvPath = "C:/users/luke/documents/C#/csvProject.csv";
StringBuilder Header = new StringBuilder();
StringBuilder csvData = new StringBuilder();
public Form1()
{
InitializeComponent();
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
Header.AppendLine("Timestamp, Name");
File.AppendAllText(csvPath, Header.ToString());
textBox1.Font = new Font("Arial", 30, FontStyle.Bold);
}
private void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
Code = Code + button.Text;
textBox1.Text = Code;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
TopMost = false;
}
}
private void button13_Click(object sender, EventArgs e)
{
//clear
Code = null;
textBox1.Text = Code;
}
private void button10_Click(object sender, EventArgs e)
{
//in or out
DateTime timeStamp = DateTime.Now;
if (Code == "123")
{
Name = "Luke";
}
Button button = (Button)sender;
csvData.AppendLine(timeStamp + "," + Name + "," + button.Text);
File.AppendAllText(csvPath, csvData.ToString());
Code = null;
textBox1.Text = Code;
}
private void button14_Click(object sender, EventArgs e)
{
}
}
}
My layout consists of a number pad, in button, and out button. When the user presses the in button after they enter their code, the program should write in the CSV file: Timestamp, Name, In. When I tested the code by clocking in, the program writes one row correctly. When I clock in and then clock out, it creates two rows of me clocking in and one row of me clocking out. I was wondering if anyone could help me find what is going wrong in the code. Thanks.
You need to empty csvData after writing it to the file.

Change file attribute pragmatically

im kinda new to programming and As university project,i have to write a program which changes a file info like a virus and then undo the changes just like anti virus.
i wrote the code for changing attribute on read only,But what about Hidden or system file ?
and what is the way for undoing it !
where im going wrong in coding ??
Here is my main form 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 WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OpenFileDialog fDialog;
void button1_Click(object sender, EventArgs e) // Browse button
{
fDialog = new OpenFileDialog();
fDialog.Title = "Open a Text File";
fDialog.Filter = "TXT Files|*.txt|doc Files|*.doc";
fDialog.InitialDirectory = #"C:\";
if (fDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(fDialog.FileName.ToString());
}
textBox1.Text = fDialog.FileName;
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
}
private void textBox1_TextChanged(object sender, EventArgs e)//the path showing text box
{
}
private void button2_Click(object sender, EventArgs e)//read-only button
{
fDialog.ReadOnlyChecked = true;
}
private void button3_Click(object sender, EventArgs e) //Hidden button
{
}
}
}

Trouble displaying an Image on my Panel

I'm trying to draw images for a game on to a Panel in C#. I get no images being drawn and I can't figure out why this method is never called:
private void playerPanel_Paint(object sender, PaintEventArgs e)
Here is my 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 FlightOfTheNavigator
{
public partial class Form1 : Form
{
// Load Sprites
public Bitmap playerShip = new Bitmap(FlightOfTheNavigator.Properties.Resources.testship);
public Form1()
{
InitializeComponent();
SetupGame();
}
public void SetupGame()
{
// Setup Console
txtConsole.Text = "Loading Ship Bios v3.4.12c ..." +
Environment.NewLine +
"Console Ready" +
Environment.NewLine +
"----------------------------------------------------------------------------------------" +
Environment.NewLine +
Environment.NewLine;
// Setup Basic Weapons
listWeapons.Items.Add("Pulse Lazers");
listWeapons.Items.Add("Cluster Missiles");
// Set Shield Perecentage
txtShields.Text = "0%";
}
private void trackShield_Scroll(object sender, EventArgs e)
{
txtShields.Text = "" + trackShield.Value + "%";
}
private void playerPanel_Paint(object sender, PaintEventArgs e)
{
Graphics g = playerPanel.CreateGraphics();
g.DrawImage(playerShip, 0, 0,100,100);
}
private void button1_Click(object sender, EventArgs e)
{
// Invalidate the panel. This will lead to a call of 'playerPanel_Paint'
playerPanel.Refresh();
}
}
}
Make sure the Paint event of the panel is attached to playerPanel_Paint method.
Open the Desinger, select the panel (playerPanel), press F4 to bring up the Properties window, then click the lightning bolt above the Properties window to show the events. Check the Paint event there. If it is empty, open the drop-down and select playerPanel_Paint method.
You can also do it in code. Put this into the form's constructor after InitializeComponent():
this.playerPanel.Paint += PaintEventHandler(playerPanel_Paint);

Categories

Resources