When I launch my application it gives me an error at " Form1 Test = new Form1();" in my class. Here is my code. I want to use labels from my form so therefore I used "form1 test".
I made a class so I can call my methods from it in my Mainform as I need to code my application with classes. When I launched the application for the first time it worked, but then after trying again it didn't work anymore.
Main form:
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 Tester
{
public partial class Form1 : Form
{
Zombie zombie = new Zombie();
int levens = 3;
public Form1()
{
InitializeComponent();
test1.Text = "Levens: " + "" + levens;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void Zombie()
{
foreach (Control control in Controls)
{
PictureBox pic = control as PictureBox;
if (pic != null)
{
pic.Top += 1;
if (pic.Top > 600 && pic.Visible == true)
{
pic.Top = 0;
test1.Text = $"Levens: {--levens}";
}
else if (pic.Top > 600 && pic.Visible == false)
{
pic.Visible = true;
pic.Top = 0;
}
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
zombie.MakeZombie(5, this);
}
}
}
Class:
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 Tester
{
class Zombie
{
Random random = new Random();
Form1 Test = new Form1();
private int score = 0;
public void MakeZombie(int aantal, Form formInstance)
{
for (int i = 0; i < aantal; i++)
{
PictureBox picture = new PictureBox();
picture.Image = Properties.Resources.ZombieDik;
picture.Size = new Size(200, 200);
picture.Location = new Point(random.Next(1500), 0);
picture.SizeMode = PictureBoxSizeMode.Zoom;
picture.Click += zombie_Click;
picture.BackColor = Color.Transparent;
formInstance.Controls.Add(picture);
}
}
void zombie_Click(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
pic.Visible = false;
score++;
Test.label2.Text = $"Score: {score}";
Test.Controls.Remove(pic);
pic.Dispose();
}
}
}
In zombie_Click() you can get a reference to the Form from the sender itself:
void zombie_Click(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
Form1 f1 = pic.FindForm() as Form1;
score++;
f1.label2.Text = $"Score: {score}";
pic.Dispose();
}
Related
I am making a password generator and on websites when you enter certain conditions are met the strength of the password changes how can I change the color and text of the label when the password strength is >= 8, <8<10, >12?
Here is 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 Password_Generator
{
public partial class PassGen : Form
{
int currentPasswordLength = 0;
Random Character = new Random();
private void PasswordGenerator(int PasswordLength)
{
String validChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!##$&?";
String randomPassword = "";
//25
for(int i = 0; i < PasswordLength; i++)
{
int randomNum = Character.Next(0, validChars.Length);
randomPassword += validChars[randomNum];
}
Password.Text = randomPassword;
}
public PassGen()
{
InitializeComponent();
PasswordLengthSlider.Minimum = 5;
PasswordLengthSlider.Maximum = 22;
PasswordGenerator(5);
}
private void Label1_Click(object sender, EventArgs e)
{
}
private void Copy_Click(object sender, EventArgs e)
{
Clipboard.SetText(Password.Text);
}
//52
private void PasswordLength_Click(object sender, EventArgs e)
{
}
private void PasswordLengthSlider_Scroll(object sender, EventArgs e)
{
PasswordLength.Text = "Password Length:" + " " + PasswordLengthSlider.Value.ToString();
currentPasswordLength = PasswordLengthSlider.Value;
PasswordGenerator(currentPasswordLength);
}
private void pswdStrengthTest()
{
if (currentPasswordLength <= 8)
{
pswdStrength.Text = "weak";
pswdStrength.ForeColor = Color.Red;
} else if (currentPasswordLength<= 9)
{
pswdStrength.Text = "ok";
pswdStrength.ForeColor = Color.Blue;
}
}
//78
private void pswdStrength_Click(object sender, EventArgs e)
{
}
}
}
If anyone could help me with this it would be greatly appreciated. This is based off a tutorial I found on YouTube. I'm not sure what the video is called but if it helps I could search for it and update my posting.
Try this:
Password.TextChanged += (s1, e1) =>
{
if (Password.Text.Length > 10)
pswdStrength.ForeColor = Color.Green
else if (Password.Text.Length > 8)
pswdStrength.ForeColor = Color.Blue
else
pswdStrength.ForeColor = Color.Red
};
Your code looks like a windows form application.
If you have for example one objetc txt_password, check to code some of these events:
TextChanged: this occurs when your textbox has been changed
Others events could be:
KeyPress or KeyDown
I have a simple windows form and I have added a picturebox that I want to act as a dark mode on/off switch for my application. I have created an event for mouse click, but it only works one way:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WeatherApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// The below two lines are used to move my WindowsForm window
private bool mouseDown;
private Point lastlocation;
public void picDarkMode_MouseClick(object sender, MouseEventArgs e)
{
//var frmForm1 = new Form1();
//frmForm1.BackColor = Color.Black;
//var darkTheme = this.BackColor = System.Drawing.Color.DarkGray;
this.BackColor = System.Drawing.Color.DarkGray;
if (System.Drawing.Color.DarkGray == System.Drawing.Color.DarkGray)
{
this.BackColor = System.Drawing.Color.LightYellow;
}
else
{
this.BackColor = System.Drawing.Color.DarkGray;
}
//this.BackColor = System.Drawing.Color.LightYellow;
}
I tried to play with if statements, but cannot understand why it is not working...
You are making two mistakes. You are setting the backcolor to DarkGray before the if statement, so it will always give the same result, and you are comparing DarkGray to DarkGray instead of the forms backcolor to DarkGray. So...
Get rid of this line:
this.BackColor = System.Drawing.Color.DarkGray;
And change this:
if (System.Drawing.Color.DarkGray == System.Drawing.Color.DarkGray)
To this:
if (this.BackColor == System.Drawing.Color.DarkGray)
This is the whole click event:
public void picDarkMode_MouseClick(object sender, MouseEventArgs e)
{
if (this.BackColor == System.Drawing.Color.DarkGray)
{
this.BackColor = System.Drawing.Color.LightYellow;
}
else
{
this.BackColor = System.Drawing.Color.DarkGray;
}
}
I have three Forms in my project. Form2 Connects the Serial Port. Form1 writes and reads data from the Serial Port. Form 3 only needs to write to the serial port, however when i send data to the serial port I get an "Port is closed error". I do not see any difference in the way i set up form 2 and form 3 so i am not sure why Visual Studios is giving me the "port closed" error.
Form 2 will connect to serial port
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; // added this
namespace ECE_323_LAB_10
{
public partial class Bluetooth_Settings : Form
{
public SerialPort _serial = new SerialPort(); // added this
public Bluetooth_Settings()
{
InitializeComponent();
_serial.BaudRate = int.Parse(baud_rate.Text); // added this
foreach (string s in SerialPort.GetPortNames()) // added this
{
com_port.Items.Add(s);
}
}
private void connet_button_Click(object sender, EventArgs e)
{
try
{
_serial.PortName = com_port.SelectedItem.ToString();
_serial.BaudRate = Convert.ToInt32(baud_rate.SelectedItem);
_serial.Open();
this.Close();
Form1 _main = new Form1();
foreach (Form1 tmpform in Application.OpenForms)
{
if (tmpform.Name == "Form1")
{
_main = tmpform;
break;
}
}
_main.toolStripStatusLabel1.Text = " Connected: " + _serial.PortName.ToString();
_main.toolStripStatusLabel1.ForeColor = Color.Green;
_main.toolStripProgressBar1.Value = 100;
}
catch
{
MessageBox.Show("Please select COM Port/ Baud Rate");
}
}
}
}
Form1 can read and write data to serial port
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;
using ZedGraph;
using System.Text.RegularExpressions;
namespace ECE_323_LAB_10
{
public partial class Form1 : Form
{
PointPairList list = new PointPairList();
double temp;
int flag = 1;
double x = 0;
int init = 0;
int digit = 0;
double temp1;
private T_settings t_settings;
private Bluetooth_Settings _setting = new Bluetooth_Settings();
public Form1()
{
InitializeComponent();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
int text_length = 0;
text_length = richTextBox1.TextLength;
char send_ch = richTextBox1.Text[text_length - 1]; // extracting the last character
char[] ch = new char[1];
ch[0] = send_ch;
if (send_ch == '\n')
{
_setting._serial.Write("\r"); // sending carraige return
}
else
{
_setting._serial.Write(ch, 0, 1); // sending char to microcontroller
}
}
private void Toggle_Click(object sender, EventArgs e)
{
// 2 = F , 1 = C
char[] ch = new char[1];
ch[0] = 'D';
_setting._serial.Write(ch, 0, 1); // sending char to microcontroller
}
private void Settings_Click(object sender, EventArgs e)
{
t_settings = new T_settings();
t_settings.Show();
}
}
}
Form1 has no errors when i read and write to the serial port. I have left a few lines of codes out for readability.
Now here is the code for Form3, I think i have done setup exactly the same as Form1, however I am getting port is closed error.
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; // added this
namespace ECE_323_LAB_10
{
public partial class T_settings : Form
{
private Bluetooth_Settings _enter = new Bluetooth_Settings();
public T_settings()
{
InitializeComponent();
}
string Sampling_text;
private void Text_Sampling_TextChanged(object sender, EventArgs e)
{
Sampling_text = Text_Sampling.Text;
}
private void Send_Sampling_Click(object sender, EventArgs e)
{
int text_length = 0;
text_length = Sampling_text.Length;
char send_ch = Text_Sampling.Text[text_length - 1]; // extracting the last character
char[] ch = new char[1];
ch[0] = send_ch;
if (send_ch == '\n')
{
_enter._serial.Write("\r"); // sending carraige return
}
else
{
_enter._serial.Write(ch, 0, 1); // sending char to microcontroller
}
char[] enter = new char[1];
}
}
}
Can someone tell me what I need to add to Form3 so I do not get a port closed error. In my send sampling click method.
Ok, if you're totally new then concepts around OOP will take time to learn. Let me try and give you a solution to get you out of trouble.
private T_settings t_settings = null;
private Bluetooth_Settings _bsSettings = null;
public Form1()
{
InitializeComponent();
if (_bsSettings == null) _bsSettings = new Bluetooth_Settings();
_bsSettings.Show();
}
private void Form1_Shown(Object sender, EventArgs e) {
{
//Either here or in the constructor instantiate T_settings with a reference to the
//_bsSettings this way it will still be in scope, the same instance you've connected.
if (t_settings == null) t_settings = new T_settings(_bsSettings);
}
public partial class T_settings : Form
{
private Bluetooth_Settings _bsSettings = new Bluetooth_Settings();
public T_settings(Bluetooth_Settings bsSettings)
{
InitializeComponent();
//Pass a reference of the BS Settings class (one that's already connected)
this._bsSettings = bsSettings;
}
...
private void Send_Sampling_Click(object sender, EventArgs e)
{
int text_length = 0;
text_length = Sampling_text.Length;
char send_ch = Text_Sampling.Text[text_length - 1]; // extracting the last character
char[] ch = new char[1];
ch[0] = send_ch;
if (send_ch == '\n')
{
_bsSettings._serial.Write("\r"); // sending carraige return
}
else
{
_bsSettings._serial.Write(ch, 0, 1); // sending char to microcontroller
}
}
I have a windows form application that basically pings an ip and then returns an image with a tooltip that displays the rtt to that ip.
What i want to do is have the the form ping that ip every 20 seconds, so that the form and images change. If i could get that to work then I would like to some how store maybe 4 rtt's and then show an average of the 4 in the tooltip.
So far the form is only pinging once, I've played around with a timer but I don't really know what I am doing. Here is my code so far.
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;
using System.Net.NetworkInformation;
using System.ServiceProcess;
using System.Threading;
using System.ComponentModel;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
Ping pingClass = new Ping();
PingReply pingReply = pingClass.Send("10.209.123.123");
label4.Text = (pingReply.RoundtripTime.ToString());
//+ "ms");
label5.Text = (pingReply.Status.ToString());
if (Convert.ToInt32(label4.Text) > 0 && Convert.ToInt32(label4.Text) < 100)
this.pictureBox1.Load("greenLAT.png");
if (Convert.ToInt32(label4.Text) > 100 && Convert.ToInt32(label4.Text) < 200)
this.pictureBox1.Load("yellowLAT.png");
if (Convert.ToInt32(label4.Text) > 200 && Convert.ToInt32(label4.Text) < 1000)
this.pictureBox1.Load("redLAT.png");
ToolTip tt = new ToolTip();
tt.SetToolTip(this.pictureBox1, "Your current network delay is " + label4.Text + "ms");
timer1.Interval = 1000;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
//MessageBox.Show("Timeout!");
Refresh();
}
}
}
Try this:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Net;
using System.Windows.Forms;
using System.Net.NetworkInformation;
namespace DXWindowsApplication4
{
public partial class Form2 : Form
{
private readonly Timer _timer;
private readonly Ping _pingClass;
private readonly IPAddress _ipAddress;
private readonly int _timeout;
private Image _greenImage;
private Image _yellowImage;
private Image _redImage;
private int _pingCount;
private long _avgRtt;
public Form2()
{
InitializeComponent();
IPAddress.TryParse("98.138.253.109", out _ipAddress); // yahoo.com Ip address
_timer = new Timer();
_timeout = 3000;
_pingClass = new Ping();
_pingClass.PingCompleted += PingClassPingCompleted;
}
void PingClassPingCompleted(object sender, PingCompletedEventArgs e)
{
RefreshPing(e.Reply);
}
public void FormLoad(object sender, EventArgs e)
{
_timer.Tick += TimerTick;
_timer.Interval = 4000;
_timer.Start();
}
private void TimerTick(object sender, EventArgs e)
{
_pingClass.SendAsync(_ipAddress, _timeout);
}
private void RefreshPing(PingReply pingReply)
{
label4.Text = (pingReply.RoundtripTime.ToString(CultureInfo.InstalledUICulture));
label5.Text = (pingReply.Status.ToString());
_avgRtt = (_avgRtt * _pingCount++ + pingReply.RoundtripTime)/_pingCount;
if (Convert.ToInt32(label4.Text) > 0 && Convert.ToInt32(label4.Text) < 100)
{
SetImage(pictureBox1, Images.Green);
}
if (Convert.ToInt32(label4.Text) > 100 && Convert.ToInt32(label4.Text) < 200)
{
SetImage(pictureBox1, Images.Yellow);
}
if (Convert.ToInt32(label4.Text) > 200 && Convert.ToInt32(label4.Text) < 1000)
{
SetImage(pictureBox1, Images.Red);
}
ToolTip tt = new ToolTip();
tt.SetToolTip(pictureBox1, "Your average network delay is " + _avgRtt + "ms");
Refresh();
}
private void SetImage(PictureBox pBox, Images images)
{
switch (images)
{
case Images.Green:
if (_greenImage == null)
{
_greenImage = new Bitmap("greenImage.png");
}
pictureBox1.Image = _greenImage;
break;
case Images.Yellow:
if (_greenImage == null)
{
_yellowImage = new Bitmap("yellowImage.png");
}
pictureBox1.Image = _yellowImage;
break;
case Images.Red:
if (_redImage == null)
{
_redImage = new Bitmap("redImage.png");
}
pictureBox1.Image = _greenImage;
break;
default:
throw new InvalidEnumArgumentException("invalid enum name");
}
}
}
internal enum Images
{
Green,
Yellow,
Red
}
}
I have a program that gets the incoming number, date and time. I want to check however if the person who is ringing me has put the phone down, how can I do this?
Below is the code which I currently have:
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;
using System.IO.Ports;
namespace CallerID
{
public partial class CallerID : Form
{
int timesTicked = 0;
Point defaultLocation = new Point();
Point newLocation = new Point();
public CallerID()
{
InitializeComponent();
port.Open();
SetModem(); // SetModem(); originally went after WatchModem();
WatchModem();
//SetModem();
telephoneTimer.Interval = 16;
telephoneTimer.Tick += new EventHandler(telephoneTimer_Tick);
defaultLocation = pictureBox1.Location;
newLocation = pictureBox1.Location;
}
void telephoneTimer_Tick(object sender, EventArgs e)
{
if (timesTicked <= 2)
newLocation.X++;
if (timesTicked >= 4)
newLocation.X--;
if (timesTicked == 6)
{
timesTicked = 0;
pictureBox1.Location = defaultLocation;
newLocation = defaultLocation;
}
pictureBox1.Location = newLocation;
timesTicked++;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
WatchModem();
}
private SerialPort port = new SerialPort("COM3");
string CallName;
string CallNumber;
string ReadData;
private void SetModem()
{
port.WriteLine("AT+VCID=1\n");
//port.WriteLine("AT+VCID=1");
port.RtsEnable = true;
}
private void WatchModem()
{
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
public delegate void SetCallerIdText();
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
ReadData = port.ReadExisting();
//Add code to split up/decode the incoming data
//if (lblCallerIDTitle.InvokeRequired)
if (ReadData.Contains("NMBR"))
{
lblData.Invoke(new SetCallerIdText(() => lblData.Text = ReadData));
}
//else
// lblCallerIDTitle.Text = ReadData;
}
private void lblData_TextChanged(object sender, EventArgs e)
{
telephoneTimer.Start();
button1.Visible = true;
}
}
}
Please ignore the Timer Code as that is just for animation.
Have you tried the PinChanged event? Normally Carrier Detect will go low when the remote end disconnects.