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]);
}
}
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 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();
}
}
}
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!";
//}
}
}
}
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.
Im trying to make a cookie maker!
it works great and all but every time i click on the cookie, it takes it about 2 clicks to add just one to the cookie count. I want to make the counting system go faster, since if i click 2 times a second, it will only count as one atm...
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 System.Threading;
namespace FormalCookie
{
public partial class Form1 : Form
{
int cnum = 0;
int add = 1;
public Form1()
{
InitializeComponent();
label1.Text = ("Price For Mouse:" + " 50");
textBox1.Text = ("Cookies:" + " " + cnum);
mouse.Enabled = false;
for (int i = 0; i< add; i++)
{
if (add > 1 && add % 2 == 0)
{
cnum += 1 * add;
}
Thread.Sleep(1000);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Mouse
if (cnum >= add * add * 50)
{
cnum -= (add * 100);
add+=2;
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if (cnum >= (add * add * 50))
{
mouse.Enabled = true;
}
else
{
mouse.Enabled = false;
}
cnum += (add * add);
textBox1.Text = ("Cookies:" + " " + cnum);
label1.Text = ("Price For Mouse:" + " " + (add * add * 50));
}
private void label1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, MouseEventArgs e)
{
}
}
}