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);
Related
I am just learning C# and I was trying to implement a webcam picture capture program. I'm using the Aforge library, the thing is that my picturebox is not displaying the webcam image and I don't understand why. If anyone knows my error, please let me know. Thank you in advance.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DarrenLee.Media;
namespace test4
{
public partial class MainForm : Form
{
int count = 0;
Camera myCamera = new Camera();
public MainForm()
{
InitializeComponent();
GetInfo();
myCamera.OnFrameArrived += myCamera_OnFrameArrived;
}
private void GetInfo()
{
var cameraDevices = myCamera.GetCameraSources();
var cameraResolutions = myCamera.GetSupportedResolutions();
foreach (var d in cameraDevices)
cmbCameraDevices.Items.Add(d);
foreach (var r in cameraResolutions)
cmbCameraResolutions.Items.Add(r);
cmbCameraDevices.SelectedIndex = 0;
cmbCameraDevices.SelectedIndex = 0;
}
private void myCamera_OnFrameArrived(object source, FrameArrivedEventArgs e)
{
Image img = e.GetFrame();
picCamera.Image = img;
}
void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
{
myCamera.ChangeCamera(cmbCameraDevices.SelectedIndex);
}
void ComboBox2SelectedIndexChanged(object sender, EventArgs e)
{
myCamera.Start(cmbCameraDevices.SelectedIndex);
}
void Form1FormClosing(object sender, FormClosingEventArgs e)
{
myCamera.Stop();
}
void BTTsaveClick(object sender, EventArgs e)
{
string filename = Application.StartupPath + #"\" + "Image" + count.ToString();
myCamera.Capture(filename);
count++;
}
}
}
Picture of how it looks when I compile it:
https://i.gyazo.com/6956a07405cd4bf5e74c20bc321bd32e.png
I am connecting the picture box with the content in this line:
Image img = e.GetFrame();
picCamera.Image = img;
Make sure you click on the object before writing the code.
Example: Click on the button in the design form then paste the code for BTTsaveClick in the newly created method after clicking on the button.
If you are using Visual Studio, you can check if the methods are referenced. If it's not referenced, that means the code won't be read.
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.
I want to show current form Title inside the textBox
But the code below doesnt do it,it only works if I set it on Button1_click.
And After clicking the button it will change to the form title
But I need it to set the form title in the textbox instantly on load
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.Diagnostics;
namespace ShowTitle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Process currentp = Process.GetCurrentProcess();
textBox1.Text = currentp.MainWindowTitle;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
This simple code demonstrate that when your event handler for Form.Load event is called there is no MainWindowTitle to read from the currentp, while if you execute the same code in the Form.Shown event handler there is a MainWindowTitle in the currentp variable
Form f;
TextBox t;
void Main()
{
f = new Form();
f.Text = "This is a test";
t = new TextBox();
f.Controls.Add(t);
f.Load += onLoad;
f.Shown += onShow;
f.Show();
}
void onLoad(object sender, EventArgs e)
{
Process currentp = Process.GetCurrentProcess();
if(!string.IsNullOrWhiteSpace(currentp.MainWindowTitle))
t.Text = currentp.MainWindowTitle;
else
t.Text = "NO TITLE";
}
void onShow(object sender, EventArgs e)
{
// Uncomment these line to see the differences
// if(!string.IsNullOrWhiteSpace(currentp.MainWindowTitle))
// t.Text = currentp.MainWindowTitle;
// else
// t.Text = "NO TITLE";
}
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
{
}
}
}
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;