C# textbox issue - c#

I need a bit of help with this code I am writing to produce and test the different types of loop conditions It has several lines that have errors that are causing the compiler to not build the solution. Don't know if it is the specific library that I am not calling right or if I am missing something in particular with the code. I created a particular folder that stores the correct images that I want the loop to use for the pictureBox, but it still has errors where it states the variable is out of text; it now states that System.OutOfMemoryException: 'Out of memory.'
:
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 PictureBox
{
public partial class Form1 : Form
{
int num1, num2;
Image[] Bully = new Image[10];
int randomNum;
Image dog= Image.FromFile(#"C:\Users\Empiric Library\source\repos\PictureBox\PictureBox\Form1.cs");
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
pictureBox1.Image = Bully[i];
i++;
Bully[0] = Image.FromFile("Bully1.jpeg");
Bully[1] = Image.FromFile("Bully2.jpeg");
Bully[2] = Image.FromFile("Bully3.jpeg");
Bully[3] = Image.FromFile("Bully4.jpeg");
Bully[4] = Image.FromFile("Bully5.jpeg");
Bully[5] = Image.FromFile("Bully6.jpeg");
if (int.TryParse(textBox1.Text, out num1))
{
if (int.TryParse(textBox2.Text, out num2))
{
while ( i < num2)
{
MessageBox.Show("i=" + i);
pictureBox1.Image = Bully[i];
i++;
}
}
}
}
private void Form2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
Bully[0] = Image.FromFile("Bully1.jpeg");
Bully[1] = Image.FromFile("Bully2.jpeg");
Bully[2] = Image.FromFile("Bully3.jpeg");
Bully[3] = Image.FromFile("Bully4.jpeg");
Bully[4] = Image.FromFile("Bully5.jpeg");
Bully[5] = Image.FromFile("Bully6.jpeg");
if (int.TryParse(textBox2.Text, out num2))
{
for (int i = num1; i <= num2; i++)
{
MessageBox.Show("Bully Number= " + i);
pictureBox1.Image = Bully[i];
}
}
}
private double Average(int num1, int num2)
{
double result = (num1 + num2) / 2;
return result;
}
private void avg1_Click(object sender, EventArgs e)
{
double res = Average(1, 100);
textBox4.Text = res.ToString();
}
private void button4_Click(object sender, EventArgs e, string textLine)
{
StreamReader fileIn = new StreamReader(#"C:\Users\Empiric Library\source\repos\PictureBox\PictureBox\Form1.cs");
// This Reads The First Line Of Text
// Read The Next Line Of Text
textLine = fileIn.ReadLine();
MessageBox.Show("From File ..." + textLine);
fileIn.Close();
}
private void button5_Click(object sender, EventArgs e)
{
StreamWriter FileOut = new StreamWriter(#"C:\Users\Empiric Library\source\repos\PictureBox\PictureBox\Form1.cs");
string str = "BullyProof";
int number = 16;
FileOut.WriteLine(str);
FileOut.WriteLine(number);
// To Write In Output Stream
FileOut.Flush();
// To Close The Stream
FileOut.Close();
}
private void button3_Click(object sender, EventArgs e)
{
Random random = new Random();
randomNum = random.Next(1, 42);
textBox3.Text = randomNum.ToString();
}
}
}

Related

How to change label text and color when a condition is met? (C#)

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

How do I make a WinForms bubblesort program?

We just started using c# and the whole class knows nothing.
Yet, the teacher told us to use WinForms and c# to make a program that used bubblesort.
I looked around the web and only found validation for the text boxes (numbers only).
I'm new here but I would like to ask for help if possible.
We have to deliver this work today and we've got nothing so far.
This is the code I have.
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 maedofeixeira
{
public partial class Form1 : Form
{
int[] elementos = new int[5];
public Form1()
{
InitializeComponent();
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]"))
{
MessageBox.Show("SO NUMEROS!!!CRL.");
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
}
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox2.Text, "[^0-9]"))
{
MessageBox.Show("SO NUMEROS!!!CRL.");
textBox2.Text = textBox2.Text.Remove(textBox2.Text.Length - 1);
}
}
private void TextBox3_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox3.Text, "[^0-9]"))
{
MessageBox.Show("SO NUMEROS!!!CRL.");
textBox3.Text = textBox3.Text.Remove(textBox3.Text.Length - 1);
}
}
private void TextBox4_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox4.Text, "[^0-9]"))
{
MessageBox.Show("SO NUMEROS!!!CRL.");
textBox4.Text = textBox4.Text.Remove(textBox4.Text.Length - 1);
}
}
private void TextBox5_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(textBox5.Text, "[^0-9]"))
{
MessageBox.Show("SO NUMEROS!!!CRL.");
textBox5.Text = textBox5.Text.Remove(textBox5.Text.Length - 1);
}
}
private void Bubblesort()
{
int refos = 0;
for (int i=0;i<elementos.Length-1;i++)
{
for (int j = 0; j < elementos.Length - (i + 1); j++)
{
if (elementos[j] > elementos[j + 1])
{
refos = elementos[j];
elementos[j] = elementos[j + 1];
elementos[j + 1] = refos;
}
}
}
}
private void Button1_Click(object sender, EventArgs e)
{
Bubblesort();
for(int i=0;i<elementos.Length;i++)
{
lbOrdenada.Items.Add(elementos[i]);
}
}
}
}
Screenshot:
Problem so far: When we hit the "Ordenar" button, it shows 5 zeros.
You need to put the values from the text boxes into the elementos array. Because of the general messiness of the code, this can't be done elegantly, but something like this should do it:
private void Button1_Click(object sender, EventArgs e)
{
elementos[0] = Convert.ToInt32(TextBox1.Text);
elementos[1] = Convert.ToInt32(TextBox2.Text);
elementos[2] = Convert.ToInt32(TextBox3.Text);
elementos[3] = Convert.ToInt32(TextBox4.Text);
elementos[4] = Convert.ToInt32(TextBox5.Text);
Bubblesort();
for(int i=0;i<elementos.Length;i++)
{
lbOrdenada.Items.Add(elementos[i]);
}
}

C# Aforge MJPEG stream motiondetection

I am still new at programming, I code as a hobby :)
I wanted to make a MJPEG streaming aplication with an option to record a video and save it on computer when it detects a moving. So far i've managed to get stream to videosourceplayer in WPF, but it seems that i can't manage to get the information whether moving is detected or not. What am i doing wrong? i've tried many things ( commented ). but it seems that i can't compare float MotionDetected to limited (in my case 0.02). my label5 simply doesnt change.
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 AForge.Vision.Motion;
using AForge.Video;
using AForge.Video.DirectShow;
using Accord.Video.FFMPEG;
using System.Globalization;
using AForge.Controls;
namespace AnotherAForge
{
public partial class Form1 : Form
{
MotionDetector Detector;
float f;
float MotionDetected;
int frames;
private VideoFileWriter _writer;
private MJPEGStream stream = new MJPEGStream("link to the cam i can't share");
int uporabimotiondetection = 0;
private IVideoSource _videoSource;
public Form1()
{
InitializeComponent();
}
Form form1 = new Form();
public void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
{
if (uporabimotiondetection == 1)
{
MotionDetected = Detector.ProcessFrame(image);
//if (label5.InvokeRequired) {
// label5.Text = NivelDeDeteccion.ToString();
//}
//if (Detector.ProcessFrame(image) > 0.02)
//{
// label5.Text = "1";
//}
}
frames++;
}
private void alarm() {
label2.Text = MotionDetected.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
//VideoStream.NewFrame += VideoStream_NewFrame;
//VideoStream.NewFrame += new NewFrameEventHandler(ProcessNewFrame);
stream.Start();
//stream.NewFrame += Stream_NewFrame;
//videoSourcePlayer1.NewFrame += videoSourcePlayer1_NewFrame;
videoSourcePlayer1.VideoSource = stream;
timer1.Enabled = true;
timer1.Start();
timer2.Start();
//pictureBox1.Image = image;
GC.Collect();
}
private void Form1_Load(object sender, EventArgs e)
{
Detector = new MotionDetector(new TwoFramesDifferenceDetector(),new MotionAreaHighlighting());
MotionDetected = 0;
}
private void timer1_Tick(object sender, EventArgs e)
{
// textBox1.Text = NivelDeDeteccion.ToString();
label3.Text = frames.ToString() + " frames";
label2.Text = MotionDetected.ToString();
if (MotionDetected <= 0.02 && MotionDetected >= 0)
{
label5.Text = "1";
}
else {
label5.Text = "0";
}
}
private void button2_Click(object sender, EventArgs e)
{
Console.Beep();
}
int time;
private void timer2_Tick(object sender, EventArgs e)
{
time++;
label4.Text = time.ToString();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
stream.Stop();
}
private void button3_Click(object sender, EventArgs e)
{
stream.Stop();
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == true)
{
uporabimotiondetection = 1;
}
else {
uporabimotiondetection = 0;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//textBox1.Text = string.Format("{0:0.00000}", textBox1.Text);
//float motionlevel = float.Parse(MotionDetected);
//float a = 1;
//textBox2.Text = a.ToString();
//float b = MotionDetected / 100;
//textBox1.Text = b.ToString();
//double limiter = Convert.ToDouble("0,02");
//textBox2.Text = limiter.ToString();
//if (MotionDetected < (0.02f / 1000))
//{
// label5.Text = "gibanje";
//}
//else
//{
// label5.Text = "ni gibanja!";
//}
}
}
}

Array/String of dynamically created textBoxes in Visual Studio - C#

I created, dynamically, a number of textBoxes. The user will write input in those textBoxes and then press a button ("SAVE").
I want to save the info from those textBoxes in an array(or string). Is it possible to implement something like this: for(i = 0; i < the_number_of_textBoxes; i++) MY_ARRAY_or_MY_STRING += textBox[i].Text; //I know this isn't correct
I'm sure there should be something similar to my "way of thinking", but I can't find it.
If I would always have the same number of texboxes, lets say 2, I could implement it like this: MY_ARRAY_or_MY_STRING = textBox1.Text + textBox2.Text;, but my textBoxes are dynamicaly generated.
Here is the code I use (although I don't think it will be of need), my program has 2 Forms (Form1 is noted as frm1) - It has nothing to do with my question, but maybe it helps, idk. If you need more info about what the program does, ask away, I wont write it here, because it may be useless info.
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;
//Add MySql Library
using MySql.Data.MySqlClient;
namespace Proiect
{
public partial class Form2 : Form
{
public Form1 frm1;
public int numar_textBox = 5;
public int numar_eticheta = 5;
public int numar_groupBox = 0;
public int LastTextBoxLeft = 15;
public int LastEtichetaLeft = 15;
public int LastGroupBoxLeft = 3;
public Form2()
{
InitializeComponent();
frm1 = new Form1();
}
private void Form2_Load(object sender, EventArgs e)
{
int i;
for (i = 0; i < frm1.get_nr_coloane(); i++)
AddNewGroupBox();
}
private void Form2_Click(object sender, EventArgs e)
{
Deselect.Focus(); //Deseleceaza orice element, mutand focusul pe un label fara text
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
/*
public int verifica_spatii(TextBox New_text_box)
{
int i, k=0;
if (New_text_box.Text == "") return 1;
else
{
for (i = 0; i < New_text_box.TextLength; i++)
if (New_text_box.Text[i] != ' ')
{
return 1;
}
return 0; // are numai spatii
}
}
*/
public void On_click()
{
}
public event EventHandler Click_event;
public System.Windows.Forms.GroupBox AddNewGroupBox()
{
EventHandler handler = Click_event;
System.Windows.Forms.GroupBox grp = new System.Windows.Forms.GroupBox();
this.Controls.Add(grp);
grp.Width = 133;
grp.Height = 50;
grp.Top = 10;
grp.Left = LastGroupBoxLeft;
LastGroupBoxLeft += grp.Width;
grp.Text = frm1.get_nume_coloane()[numar_groupBox];
numar_groupBox += 1;
TextBox txt = new TextBox();
txt.Top = 20;
txt.Left = 10;
numar_textBox = numar_textBox + 1;
grp.Controls.Add(txt);
txt.Leave += textBoxGeneral_LostFocus;
/////New_text_box.Click += handler(this,);
return grp;
}
private void textBoxGeneral_LostFocus(object sender, EventArgs e)
{
//ToolStripItem item = (ToolStripItem)sender;
// MessageBox.Show(item.Text);
}
private void button1_Click(object sender, EventArgs e)
{
string valorile_de_adaugat="";
int i;
for(i=0;i<numar_groupBox-1;i++)
valorile_de_adaugat+=textBox[i]
//MySqlCommand Salveaza_in_baza_de_date = new MySqlCommand("INSERT INTO " + frm1.get_nume_tabel_selectat() + " ("+ frm1.get_unirea_coloanelor + ") VALUES(22056, 15, 2000, 2004));
}
/*
Textbox myTxtbx = new Textbox();
myTxtbx.Text = "Enter text here...";
myTxtbx.GotFocus += GotFocus.EventHandle(RemoveText);
myTxtbx.LostFocus += LostFocus.EventHandle(AddText);
public RemoveText(object sender, EventArgs e)
{
myTxtbx.Text = "";
}
public AddText(object sender, EventArgs e)
{
if (String.IsNullOrWhiteSpace(myTxtbx.Text))
myTxtbx.Text = "Enter text here...";
}
*/
}
}
Thank you for your time,
Vlad

How do I get my If else loop to loop through an array C# .NET

I got a list of 10 items.
when a user puts in a value in "listText" its comparing to the first item in displayArraysString.
Lets say its not the first item in the displayArraysString list, then it doesnt do anything (Because I dont have a loop)
How do I create a loop that will check through my list and display the messagebox once it finds it. I tried with a try catch loop but that didnt work for me.
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 Arrays
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int[] numbers = new int[5];
List<int> numbersList = new List<int> ();
string text = System.IO.File.ReadAllText(#"C:Directory\list.txt");
private void Form1_Load(object sender, EventArgs e)
{
//numbers[0] = 12;
//numbers[1] = 10;
//numbers[2] = 25;
//numbers[3] = 10;
//numbers[4] = 15;
//numbersList.Add(23);
//numbersList.Add(32);
//numbersList.Add(35);
}
//Array Print
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < numbers.Length; i++)
displayArrays.Text += numbers[i].ToString() + ", ";
}
//List Print
private void button2_Click(object sender, EventArgs e)
{
for (int o = 0; o < text.Length; o++)
{
displayArraysString.Text += text[o].ToString();
if (listText.Text == displayArraysString.Text)
{
MessageBox.Show("Found a match!");
}
else
{
//Something.
}
}
}
}
}
You are trying something wrong here,
as the file you have read from the path in string , it will match a single character for listText, so there will be never a match,
I did it with string array, to convert the text data into string array of every words in it. If you search now than match will be found for listText.
try this code:
namespace Arrays
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int[] numbers = new int[5];
List<int> numbersList = new List<int> ();
string text = System.IO.File.ReadAllText.Text(#"C:\Directory\list.txt");
string[] displayStringArrays = null;
private void Form1_Load(object sender, EventArgs e)
{
//numbers[0] = 12;
//numbers[1] = 10;
//numbers[2] = 25;
//numbers[3] = 10;
//numbers[4] = 15;
//numbersList.Add(23);
//numbersList.Add(32);
//numbersList.Add(35);
}
//Array Print
private void button1_Click(object sender, EventArgs e)
{
displayArrays.Text = listText.Text;
}
//List Print
private void button2_Click(object sender, EventArgs e)
{
displayStringArrays = text.Split('\n').ToArray();
foreach (var item in displayStringArrays)
{
displayArraysString.Text += item;
if (listText.Text == item.Substring(0, item.Length - 1) || listText.Text == item)
{
MessageBox.Show("Found a match!");
}
else
{
//Something.
}
}
}
}
}
replace this code with your code. I checked this it is working fine now.

Categories