I would like to do a program that after entering the number into the textbox and will display the appropriate images. For example, if you type 1 and 2, will show candle and a swan, the program will help in remembering the mnemonic system of numbers. 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.Windows.Forms;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string dane = textBox1.Text;
if (dane == "")
{
MessageBox.Show("No number");
}
else
{
bool jest_liczba = true;
try
{
double dane_ok = System.Convert.ToDouble(dane);
}
catch
{
MessageBox.Show("no int");
jest_liczba = false;
}
if (jest_liczba == true)
{
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string temp = textBox1.Text; //table
for (int i = 0; i < temp.Length; i++)
{
textBox1.Text = char.ToString(temp[i]);
}
string _katalog = #"c:\obrazki\"; //load picture
string _typ = ".jpg";
int _liczba;
if (Int32.TryParse(textBox1.Text, out _liczba))
{
pictureBox1.Image = Image.FromFile(_katalog + _liczba + _typ);
}
}
}
}
Program after entering a couple of numbers, displays only one image. Please help
I'm not sure what your for..loop is accomplishing. Just comment it out:
// string temp = textBox1.Text; //table
// for (int i = 0; i < temp.Length; i++)
// {
// textBox1.Text = char.ToString(temp[i]);
// }
That loop currently replaces the TextBox text with the last character of the temp string.
Related
so the assignment is to read from the student file into an array and read into the answer key array, compare the two and output a grade based on the array comparison.
the issue i'm having is that when i try to load the answer key into it's array it's like its not even getting the data, because all the questions output as wrong.
below is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AshleyBrown_CPT185A01S_Chapter7Lab
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//variables
private const int SIZE = 20; //current # of q's on test
private int index = 0, count = 1; //counter variables
private int wrong = 0, right = 0; //grade variables
these are the arrays that are used for the answers:
//arrays
private char[] studentAnswers = new char[SIZE];
private char[] answerKey = new char[SIZE];
private void calculateBtn_Click(object sender, EventArgs e)
{
//prevents any file errors
try
{
ReadStudentFile();
ReadAnswerKey();
CompareAnswers();
}
catch
{
MessageBox.Show("File doesn't exist or has the wrong name.");
}
}
private void clearBtn_Click(object sender, EventArgs e)
{
//Clear Form
studentAListBox.Items.Clear();
correctAListBox.Items.Clear();
wrongAListBox.Items.Clear();
incorrectBox.Text = "";
correctBox.Text = "";
percentBox.Text = "";
}
private void exitBtn_Click(object sender, EventArgs e)
{
//close program
Close();
}
method for reading the student file that works:
private void ReadStudentFile()
{
//Stream Reader Setup
StreamReader studentFile;
studentFile = File.OpenText("C:\\Users\\aabro\\Documents\\_CPT 185\\AshleyBrown_CPT185A01S_Chapter7Lab\\Student File.txt");
//Read Student Answers into studentAnswers Array
while (index < studentAnswers.Length && !studentFile.EndOfStream)
{
studentAnswers[index] = char.Parse(studentFile.ReadLine());
index++;
}
//Close Student Answer file
studentFile.Close();
//Display Student Answers
foreach (char answer in studentAnswers)
{
studentAListBox.Items.Add(count + ". " + answer.ToString());
count++;
}
}
method for reading answer key that populates with no data:
private void ReadAnswerKey()
{
//Stream Reader Setup
StreamReader answerFile;
answerFile = File.OpenText("C:\\Users\\aabro\\Documents\\_CPT 185\\AshleyBrown_CPT185A01S_Chapter7Lab\\Answer Key.txt");
//Read Answer Key in answerKey Array
while (index < answerKey.Length && !answerFile.EndOfStream)
{
answerKey[index] = char.Parse(answerFile.ReadLine());
index++;
}
//Close answer key file
answerFile.Close();
//clear count
count = 1;
//display answer key in correct answer list box
foreach (char key in answerKey)
{
correctAListBox.Items.Add(count + ". " + key.ToString());
count++;
}
}
private void CompareAnswers()
{
//reset count
count = 1;
for (index = 0; index < answerKey.Length; index++)
{
//determine if answer is right
if (studentAnswers[index] != answerKey[index])
{
wrongAListBox.Items.Add(count + ". " + answerKey[index]);
wrong++;
count++;
}
}
//fail display
if (wrong > 5)
{
MessageBox.Show("Student has failed");
}
//calculations
double pointPerQ = 5;
double wrongTotal = wrong;
double wrongPointTotal = wrong * pointPerQ;
double grade = 100 - wrongPointTotal;
//output grade information
incorrectBox.Text = wrong.ToString();
correctBox.Text = right.ToString();
percentBox.Text = grade.ToString("p0");
}
}
}
this is the current output from running the program, I did double check the file names and contents as well.
output of current code
One issue that I saw in code is using of index in multiple while loops without previously assigning to 0.
I suggest using of local variables(variable which exists only in the current block of code).
Also in my opinion it will be good to declare and initialize new variable in for-loop like this:
for(int index = 0; index < answerKey.Length; index++)
{
// your code
}
it will be more readable and easier if you want later to separate loops in methods or move in service or helper.
Hi guys every time I pressed the button to generate the sorted string the output gets concatenated in the previous label output. I'm lost on how to clear the previous output on the label before showing the new one here's 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 You_Source
{
public partial class Form1 : Form
{
string method;
public Form1()
{
InitializeComponent();
}
private void radio_checked(object sender, EventArgs e)
{
RadioButton radio = (RadioButton)sender;
method = radio.Text;
//label_Output.Text = method;
}
private void button_Sort_Click(object sender, EventArgs e)
{
string input = textBox_Input.Text;
label_Output.Clear();
if ( method == "Bubble Sort")
{
char[] charInput = input.ToCharArray();
char temp;
for (int j = 0; j <= charInput.Length - 2; j++)
{
for (int i = 0; i <= charInput.Length - 2; i++)
{
if (charInput[i] > charInput[i + 1])
{
temp = charInput[i + 1];
charInput[i + 1] = charInput[i];
charInput[i] = temp;
}
}
}
foreach (char letter in charInput)
label_Output.Text = label_Output.Text+letter;
}
}
}
can anyone give me a hint on what to do.
example( if i enter "cba" the output would be "abc", then when i entered another input in the text box "zyx" the new label output would be "abcxyz". I just one the new one "xyz" to be shown.
You can just set the string to "" at some point of your code, and overwrite it with the new string
I'm trying to program the textboxes, so they do their instructions AFTER the textbox is left. Any ideas on how to do it? I tried smth with KeyUp, but don't really know how it works
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 For_homework
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var LoopResult = new List<int>();
var LoopParameters = new List<int>();
LoopParameters = TakeInput();
LoopResult = Looping(LoopParameters);
Result(LoopResult);
FinalResult(LoopResult);
}
private List<int> TakeInput()
{
var Loop = new List<int>();
Loop.Add(int.Parse(textBox1.Text));
Loop.Add(int.Parse(textBox2.Text));
Loop.Add(int.Parse(textBox3.Text));
Loop.Add(int.Parse(textBox4.Text));
Loop.Add(int.Parse(textBox5.Text));
Loop.Add(int.Parse(textBox6.Text));
return Loop;
}//Take the input
private List<int> Looping(List<int> LoopParam)
{
var LoopSum = new List<int>();
LoopSum.Add(0);
LoopSum.Add(0);
for (int i = LoopParam[0]; i <= LoopParam[1]; i += LoopParam[2])
{
LoopSum[0]+=i;
}
for(int i = LoopParam[3]; i <= LoopParam[4]; i += LoopParam[5])
{
LoopSum[1]+=i;
}
return LoopSum;
}//Do the loop and summarize
void Result (List<int> LoopResult)
{
resultA.Text = ""+LoopResult[0];
resultB.Text = ""+LoopResult[1];
}//Give result
void FinalResult(List<int> LoopResult)
{
if(LoopResult[0]>LoopResult[1])
{
WhoWins.Text = "Player A wins";
}
else if(LoopResult[1]>LoopResult[0])
{
WhoWins.Text = "Player B wins";
}
else
{
WhoWins.Text = "It's a draw";
}
}//Give final result
private void textBox1_TextChanged(object sender, EventArgs e)
{
int i = int.Parse(textBox1.Text);
if (i < 1 || i > 6)
label3.Text = "Wrong number";
}//Texbox evaluation
private void textBox2_TextChanged(object sender, EventArgs e)
{
int i = int.Parse(textBox2.Text);
if (i < 7 || i > 18)
label4.Text = "Wrong number";
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
int i = int.Parse(textBox4.Text);
if (i < 1 || i > 6)
label7.Text = "Wrong number";
}//Textbox evaluation
private void textBox5_TextChanged(object sender, EventArgs e)
{
int i = int.Parse(textBox5.Text);
if (i < 7 || i > 18)
label8.Text = "Wrong number";
}
}
}
The form itself
It told me my post is mostly code so I need to add more details
There is LostFocus TextBox Event that might be useful for you in this case:
private void textBox1_LostFocus(object sender, System.EventArgs e)
{
//Do something
}
And don't forget to register event in your designer file as
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
My app is to test for prime number. First time I enter a prime number, the result is true and number is displayed to user. But second time, the prime number check is not functioning as expected. Here is my code:
private void button1_Click(object sender, EventArgs e)
{
label3.Text = textBox1.Text;
float a = (float)Convert.ToDouble(textBox1.Text);
if (check_Number(ref a) == true)
{
ResultCheck.Text = "Input Number is Prime";
}
else if (check_Number(ref a) == false)
{
ResultCheck.Text = "Input Number is not Prime";
}
}
here is an example program that uses trial division.
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;
namespace PrimeCheck
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = textBox1.Text;
double testthis = 0;
if (double.TryParse(textBox1.Text, out testthis))
{
if (CheckForPrime(testthis))
{
MessageBox.Show("prime time!!");
}
}
}
bool CheckForPrime(double number)
{//there are better ways but this is cheap and easy for an example
bool result = true;//assume we are prime until we can prove otherwise
for (double d = 2; d < number; d++)
{
if (number % d == 0)
{
result = false;
break;
}
}
return result;
}
}
}
I've been developing a program in C# that encodes characters into their values (e.g, A: 65). However, I'm getting a debug error in the decoding event that states that the input string was not in a correct format. Will somebody try to help me please? Thank you.
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;
namespace Decode_Project
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string message = encoderBox.Text;
decoderBox.Text = "";
for (int i = 0; i < message.Length; i++)
{
int code = message[i];
decoderBox.Text += String.Format("{0} ", code);
}
}
private void button2_Click(object sender, EventArgs e)
{
string[] messageCodes = decoderBox.Text.Split(' ');
int[] codes = new int[messageCodes.Length];
char[] letters = new char[codes.Length];
for (int i = 0; i < codes.Length; i++)
{
codes[i] = int.Parse(messageCodes[i]);
letters[i] = Convert.ToChar(codes[i]);
encoderBox.Text = "";
encoderBox.Text += letters[i];
}
}
}
}
Let's say your input is ABC, after clicking on button1, your encoded text will be "65 66 67 ". (Notice the extra space at the end of the string.)
So when you click on button2, you will get:
messageCodes[0]: "65"
messageCodes[1]: "66"
messageCodes[2]: "67"
messageCodes[3]: "" // <-- An extra empty string... Bad!
And when you do the int.Parse for the last item (the empty string), it will fail.
What you need to do is to trim the text before splitting it, as in:
string[] messageCodes = decoderBox.Text.Trim().Split(' ');
Also, as an aside, you should move the encoderBox.Text = ""; out of the for loop in order for it to work properly, as in:
encoderBox.Text = "";
for (int i = 0; i < codes.Length; i++)
{
codes[i] = int.Parse(messageCodes[i]);
letters[i] = Convert.ToChar(codes[i]);
encoderBox.Text += letters[i];
}