private void rateTextBox_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(pieceTextBox.Text))
{
}
else if (string.IsNullOrEmpty(rateReturnTextBox.Text))
{
}
else
{
int piece = Convert.ToInt32(pieceTextBox.Text);
int rate = Convert.ToInt32(rateTextBox.Text);
int netTotal = piece * rate;
netTotalBillTextBox.Text = netTotal.ToString();
}
}
//why dose not show the multiplication answer....where is the mistake?
I want this answer in netTotalBillTextBox .
Apparently, in the second condition shouldn't compare rateTextBox?
I assume it will always return string.Empty in rateReturnTextBox.
I am working on a quiz project which also allows user to add new questions.
I have an array list in which questions are stored and retrieved for displaying. Each object saved in the array list contains 5 strings.
Question
Right answer
Wrong answer 1
Wrong answer 2
Wrong answer 3
How can I randomly select objects from the array list to be displayed on the screen? And how can I shuffle the 4 answers (as radio buttons) so that the right answer appears at different positions each time?
namespace quiz
{
public partial class Quiz : Form
{
private ArrayList Questionslist;
public Quiz(ArrayList list)
{
InitializeComponent();
Questionslist = list;
}
int index = 0;
private void Quiz_Load(object sender, EventArgs e)
{
//creating an object of class Question and copying the object at index1 from arraylist into it
Question q = (Question)Questionslist[index];
//to display the contents
lblQs.Text = q.Quest;
radioButtonA1.Text = q.RightAnswer;
radioButtonA2.Text = q.WrongAnswer1;
radioButtonA3.Text = q.WrongAnswer2;
radioButtonA4.Text = q.WrongAnswer3;
}
private int Score = 0;
private void radioButtonA1_CheckedChanged(object sender, EventArgs e)
{
//if checkbox is checked
//displaying text in separate two lines on messagebox
if (radioButtonA1.Checked == true)
{
MessageBox.Show("Well Done" + Environment.NewLine + "You Are Right");
Score++;
index++;
if (index < Questionslist.Count)
{
radioButtonA1.Checked = false;
radioButtonA2.Checked = false;
radioButtonA3.Checked = false;
radioButtonA4.Checked = false;
Quiz_Load(sender, e);
}
else
{
index--;
MessageBox.Show("Quiz Finished" + Environment.NewLine + "your Score is" + Score);
Close();
}
}
}
private void radioButtonA2_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonA2.Checked == true)
{
MessageBox.Show("Sorry" + Environment.NewLine + "You Are Wrong");
Close();
}
}
private void radioButtonA3_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonA3.Checked == true)
{
MessageBox.Show("Sorry" + Environment.NewLine + "You Are Wrong");
Close();
}
}
private void radioButtonA4_CheckedChanged(object sender, EventArgs e)
{
if (radioButtonA4.Checked == true)
{
MessageBox.Show("Sorry" + Environment.NewLine + "You Are Wrong");
Close();
}
}
}
}
this is the code for class question
namespace quiz
{
[Serializable()]
public class Question : ISerializable
{
public String Quest;
public String RightAnswer;
public String WrongAnswer1;
public String WrongAnswer2;
public String WrongAnswer3;
public Question()
{
Quest = null;
RightAnswer=null;
WrongAnswer1=null;
WrongAnswer2=null;
WrongAnswer3=null;
}
//serialization function
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Question", Quest);
info.AddValue("Right Answer", RightAnswer);
info.AddValue("WrongAnswer1",WrongAnswer1);
info.AddValue("WrongAnswer2",WrongAnswer2);
info.AddValue("WrongAnswer3",WrongAnswer3);
}
//deserializaton constructor
public Question(SerializationInfo info, StreamingContext context)
{
Quest = (String)info.GetValue("Question", typeof(String));
RightAnswer=(String)info.GetValue("Right Answer",typeof(String));
WrongAnswer1=(String)info.GetValue("WrongAnswer1",typeof(String));
WrongAnswer2=(String)info.GetValue("WrongAnswer2",typeof(String));
WrongAnswer3=(String)info.GetValue("WrongAnswer3",typeof(String));
}
}
}
You know which one is the 'right' answer based on the Text property. One approach would be to store the answers in an array and shuffle the array before assigning it to the radio buttons:
// You're going to make questionList a List<Question> because if you like
// Right answers you know that ArrayList is Wrong.
Question q = questionList[index];
// You should move this next bit to the Question class
string[] answers = new string[]
{
q.RightAnswer,
q.WrongAnswer1,
q.WrongAnswer2,
q.WrongAnswer3
};
// Using the Fisher-Yates shuffle from:
// http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp
Shuffle(answers);
// Ideally: question.GetShuffledAnswers()
radioButtonA1.Text = answers[0];
radioButtonA2.Text = answers[1];
radioButtonA3.Text = answers[2];
radioButtonA4.Text = answers[3];
Then later, all you need is a single radio button event handler that they all share:
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = (RadioButton)sender;
Question q = questionList[index];
//if checkbox is checked
//displaying text in separate two lines on messagebox
if (rb.Checked && q.RightAnswer == rb.Text)
{
// Move your code to another method
// MessageBox.Show("Well Done" + Environment.NewLine + "You Are Right");
UserSelectedCorrectAnswer();
}
else if (rb.Checked)
{
// They checked the radio button, but were wrong!
// MessageBox.Show("Sorry" + Environment.NewLine + "You Are Wrong");
UserSelectedWrongAnswer();
}
}
Pick a random string from ArrayList:
string randomPick(ArrayList strings)
{
return strings[random.Next(strings.Length)];
}
You might consider having a Question class. That would contain a string Text member for the question (because I think Question.Question is silly, Question.Text makes more sense when read). Since you mentioned an ArrayList (not because I think it's the best solution), you can have one of those as a member as well which would contain all the potential answers.
When you create a question, you can display Question.Text, then use the function above to randomly pick an answer. Then use RemoveAt(index) on the answers to ensure you don't duplicate answers.
I'd make a Question class containing the question, the right answer and a list of possible answers, and have methods that return the possible answers in randomized order and compares a guess to the correct answer. It could look something like this:
class Question
{
String question;
String rightAnswer;
List<String> possibleAnswers = new ArrayList<String>();
public Question(String question, String answer, List<String> possibleAnswers)
{
this.question = question;
this.rightAnswer = answer;
this.possibleAnswers.addAll(possibleAnswers);
this.possibleAnswers.add(this.rightAnswer);
}
public List<String> getAnswers()
{
Collections.shuffle(possibleAnswers);
return possibleAnswers;
}
public boolean isCorrect(String answer)
{
return answer.equals(correctAnswer);
}
}
You should encapsulate all the questions in a class. For example, create a class named Question. This class can contain 5 variables: String question, String answer1, String answer2, String answer3 and String answer4.
Save all your questions in a database or read them from a file and load them on the start of the program.
Use the Random class to randomly select a question and to 'shuffle' the 4 questions.
Here's a method that will work:
public List<string> Randomize(string[] numbers)
{
List<string> randomized = new List<string>();
List<string> original = new List<string>(numbers);
Random r = new Random();
while (original.Count > 0) {
int index = r.Next(original.Count);
randomized.Add(original[index]);
original.RemoveAt(index);
}
return randomized;
}
just adapt it to string array instead of int array
The shuffle is probably a standard question, but I guess this will work:
// Warning: Not a thread-safe type.
// Will be corrupted if application is multi-threaded.
static readonly Random randomNumberGenerator = new Random();
// Returns a new sequence whose elements are
// the elements of 'inputListOrArray' in random order
public static IEnumerable<T> Shuffle<T>(IReadOnlyList<T> inputListOrArray)
{
return GetPermutation(inputListOrArray.Count).Select(x => inputListOrArray[x]);
}
static IEnumerable<int> GetPermutation(int n)
{
var list = Enumerable.Range(0, n).ToArray();
for (int idx = 0; idx < n; ++idx)
{
int swapWith = randomNumberGenerator.Next(idx, n);
yield return list[swapWith];
list[swapWith] = list[idx];
}
}
If you don't have IReadOnlyList<T> (.NET 4.5), just use IList<T>. The incoming object is not mutated.
thanks to all of u who answered my question.i did it this way.
private void Quiz_Load(object sender, EventArgs e)
{
displayQs();
}
private void displayQs()
{
Random _random = new Random();
int z = _random.Next(Questions.Count);
Question q = (Question)Questions[z];
Qslbl.Text = q.Quest;
DisplayAns(q, _random);
}
private void DisplayAns(Question q, Random _random)
{
int j = 0;
int[] array = new int[4];
for (int i = 0; j <= 3; i++)
{
int x = _random.Next(4);
x++;
if (array.Contains(x))
{
continue;
}
else
{
array[j] = x;
j++;
string answer = null;
if (j == 1)
answer = q.RightAnswer;
else if (j == 2)
answer = q.WrongAnswer1;
else if (j == 3)
answer = q.WrongAnswer2;
else if (j == 4)
answer = q.WrongAnswer3;
if (x == 1)
radioButton1.Text = answer;
else if (x == 2)
radioButton2.Text = answer;
else if (x == 3)
radioButton3.Text = answer;
else if (x == 4)
radioButton4.Text = answer;
}
}
}
I have this program that I am working on for class, I think the problem is in my if statements. When I run the program and make my selection and click the total button and I get this as a display "for your appetizer, for your entree, and for dessert" in the order label and the price for the steak dinner in the order total label. I think I may have to use switch statements, but I'm not sure any suggestions would be of great help, thanks.
namespace Restaurant
{
public partial class frmRestaurant : Form
{
decimal AppetizerPrice = 0.0m;
decimal EntreePrice = 0.0m;
decimal DessertPrice = 0.0m;
decimal total = 0.0m;
string AppetizerOrder = "", EntreeOrder = "", DessertOrder = "", order = "";
public frmRestaurant()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnTotal_Click(object sender, EventArgs e)
{
CalculateTotal();
Order();
lblOrder.Text = order;
lblTotal.Text = total.ToString();
}
private void grpAppetizer_Enter(object sender, EventArgs e)
{
if (radCheeseSticks.Checked)
{
AppetizerPrice = 5.99m;
AppetizerOrder = "Cheese Sticks";
}
else if (radGarlicBread.Checked)
{
AppetizerPrice = 4.50m;
AppetizerOrder = "Garlic Bread";
}
else if (radChipsnSalsa.Checked)
{
AppetizerPrice = 3.50m;
AppetizerOrder = "Chips and Salsa";
}
}
private void grpEntree_Enter(object sender, EventArgs e)
{
if (radSteakDinner.Checked)
{
EntreePrice = 12.50m;
EntreeOrder = "Steak Dinner";
}
else if (radChickenParm.Checked)
{
EntreePrice = 10.99m;
EntreeOrder = "Chicken Parmigiana";
}
else if (radChipsnSalsa.Checked)
{
EntreePrice = 3.50m;
EntreeOrder = "Chips and Salsa";
}
}
private void grpDessert_Enter(object sender, EventArgs e)
{
if (radSteakDinner.Checked)
{
DessertPrice = 12.50m;
DessertOrder = "Steak Dinner";
}
else if (radChickenParm.Checked)
{
DessertPrice = 10.99m;
DessertOrder = "Chicken Parmigiana";
}
else if (radChipsnSalsa.Checked)
{
DessertPrice = 3.50m;
DessertOrder = "Chips and Salsa";
}
}
public decimal CalculateTotal()
{
total = AppetizerPrice + EntreePrice + DessertPrice;
return total;
}
public string Order()
{
order = AppetizerOrder + "for your appetizer," + EntreeOrder + "for your entree, and " + DessertOrder + "for dessert";
return order;
}
}
}
I think the GroupBox.Enter event is not useful for your use case. The enter event is invoked sometime during the control activation, but not when the value is changed.
One way to fix your problem is to set the appetizer/entree/dessert price and text only when the "Total" button is clicked. You do not need it before that in the current form right now.
Another way to fix it is to use the correct event: Just handle the RadioButton.CheckedChanged event for all of those radio buttons, for example:
private void radGarlicBread_CheckedChanged(object sender, EventArgs e)
{
if (radGarlicBread.Checked)
{
AppetizerPrice = 4.50m;
AppetizerOrder = "Garlic Bread";
}
}
First of all, you probably want to use String.Format(), because it'll look a bit cleaner than a bunch of concatenation--it'll also help you catch things like the lack of spaces here (your output seems like it would be, e.g. 'Chips and Salsafor your appetizer...')
It might also be better to just find whichever item is checked in the Order method rather than update it every time the user checks.
I'm not sure what is wrong, but maybe you can do a Debug.WriteLine() every time each of the Enter() methods is called to see what is going on.
For example:
Debug.WriteLine("grpDesert_Enter");
Debug.WriteLine(radSteakDinner.Checked);
Debug.WriteLine(radChickenPark.Checked);
Debug.WriteLine(radChipsnSalsa.Checked);
I'm new to C# so still finding my way around.
I have a button I want to enable only when a user enter text to textbox.
I get this error - "Object reference not set to an instance of an object".
Here is the related code (without the using and variables):
public MainWindow()
{
MessageBox.Show("Make sure to edit Settings tab.");
InitializeComponent();
if (startTextBox.Text == "0") // Checks to see if a textbox has some text other than zero. if no than the user cannot press button1 yet.
{
button1.IsEnabled = false;
}
else
{
button1.IsEnabled = true;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (radioButton1.IsChecked == false)
{
label17.Content = "No Hourly wage was set.";
}
}
private void add(object sender, RoutedEventArgs e) /// here is a very long method so I've removed its content.
}
public void printTime()
{
int Sum = (this.EndInt - this.StartInt);
int Money = (Sum * this.L1001);
label16.Content = Sum;
label17.Content = Money;
if ((textBox1.Text == "0") && ((textBox2.Text == "0") || (textBox3.Text == "0")))
{
label17.Content = "No Hourly wage was set.";
}
}
public void printTime2()
{
int Sum = (this.EndInt - this.StartInt);
MessageBox.Show("Is it possible that you've worked - " + Sum + " Hours?");
}
public void printTime3()
{
int Sum = (this.EndInt - this.StartInt);
int Money = (Sum * this.L1001);
label16.Content = Sum;
label17.Content = Money;
if (textBox1.Text == "0")
{
label17.Content = "No Hourly wage was set.";
}
}
public int Convert(String S)
{
int i = int.Parse(S);
return i;
}
// Input Validation For Excepting Integers Only!
private void input(object sender, TextCompositionEventArgs e)
{ CheckIsNumeric(e); }
private void CheckIsNumeric(TextCompositionEventArgs e)
{
int result; if (!(int.TryParse(e.Text, out result) || e.Text == "."))
{ e.Handled = true; MessageBox.Show("Numbers Only"); }
}
private void startTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
button1.IsEnabled = true;
}
}
}
It's the scope problem. You didn't show where button1 is defined. But inside your event handler startTextBox_TextChanged, button1 definition is nowhere to be found (actually it needs to be instantiated as well). Since you try to invoke a method on an object (button1) which has not been instantiated yet, that exception was thrown.
If you post more than just those snippets, either I or someone else might be able to further help you.
I've been creating a small application that allows a user to convert images to various sizes and formats. I've been struggling on getting a good solid design with this application. I have the application up and running, but it does integrate good Object-Oriented design. Since this is a personal project, I've been wanting to learn more about integrating interfaces, good class inheritance, object composition, and other elements of OO design.
However, I've been struggling to do so. Don't get me wrong, I know about OO design and what it is, I just don't know how to implement good OO design in projects. Of course its easy to look at class Examples that you read in books, or online. Examples may have simple scenarios such as the following.
Interface IPerson has member functions Walk(), Run() . Abstract Class Person uses IPerson Interface. Class Man and Class Female inherit from Abstract Class Person.
but when it comes to Real Projects I struggle to implement good design. I was hoping for some insight. Here is what I currently have.
Interface:
interface IPicture
{
Bitmap ReturnImage(string path, int width, int height);
}
Main Class that Holds Picture Information. This class basically stores information about the image passed, and information about the new values the user wants (i.e. new size, new file location, new pic format, etc.)
public class MyPictures : IPicture
{
//All Private variables below are properties. Property get/set's have been removed
//for the sake of space
private int _NewWidth;
private int _NewHeight;
private string _NewImgName;
private string _NewImgPath;
private string _NewImgFullPath;
private ImageFormat _NewImgFormat;
//Declare variables to hold values that have been determined
private int _OldWidth;
private int _OldHeight;
private string _OldImgName;
private string _OldImgPath;
//Old Image Format is in String format because of certain extension scenarios.
private string _OldImgFormat;
public MyPictures(Image img, string file)
{
ClearProperties();
//...set properties based on passed variables in constructor...
}
public void ClearProperties()
{
_NewWidth = 0;
_NewHeight = 0;
_NewImgName = "";
_NewImgPath = "";
_NewImgFullPath = "";
_NewImgFormat = null;
_OldWidth = 0;
_OldHeight = 0;
_OldImgName = "";
_OldImgPath = "";
_OldImgFormat = null;
}
public override string ToString()
{
return _OldImgPath;
}
public void ImageSave()
{
Bitmap tempBmp = new Bitmap(_OldImgPath);
Bitmap bmp = new Bitmap(tempBmp, _NewWidth, _NewHeight);
bmp.Save(_NewImgPath + #"\" + _NewImgName + "." + _NewImgFormat.ToString().ToLower(), _NewImgFormat);
}
public Bitmap ImageClone()
{
Bitmap bmp = new Bitmap(_OldImgPath);
return bmp;
}
Bitmap IPicture.ReturnImage(string path, int width, int height)
{
return new Bitmap(new Bitmap(path), width, height);
}
}
Main Class; Starting point of application. This definatly needs some work...
public partial class Form1 : Form
{
static bool hasThreadBeenStopped = false;
static bool imageProcessingComplete = false;
static bool imgConstrained = false;
//Default text when user selects 'All' checkbox for new image name
static string newNameDefault = "'Name' + #";
Utility.Validation.Validate valid = new Utility.Validation.Validate();
public Form1()
{
InitializeComponent();
//Populate Combo Box With Possible Image Formats...
//Conditionally show Image Properties...
ImgPropertiesEnabled();
//Set static progress bar properties...
progressBar1.Minimum = 0;
progressBar1.Step = 1;
}
private void Form1_Load(object sender, EventArgs e)
{
lblImgProcessed.Text = "";
lblFile.Text = "";
txtContentFolder.Text = "";
}
//Delegate declarations. Used for multi-thread processing
public delegate void PopulateTextboxDelegate(Label lbl, string text);
public delegate void ThreadWorkDelegate(Label lbl, string text);
public delegate void ImageDisplayDelegate(Image i);
public delegate void ProgressBarDelegate(ProgressBar p, int step, int value);
//Populate textbox fields with image processed, and image path being processed
public void PopulateTextbox(Label lbl, string text)
{
lbl.Text = "";
lbl.Text = text;
}
public void ThreadWork(Label lbl, string text)
{
this.Invoke(new PopulateTextboxDelegate(PopulateTextbox),
new object[] { lbl, text });
}
//Display Currently Processed Image
public void ImageDisplay(Image i)
{
pbMain.Image = null;
pbMain.Image = i;
}
public void ThreadWorkImg(Image i)
{
this.Invoke(new ImageDisplayDelegate(ImageDisplay),
new object[] {i});
}
//Increment Progress Bar
public void ProgressBarDisplay(ProgressBar pg, int max, int value)
{
//Dynamically set the Progress Bar properties
pg.Maximum = max;
pg.Value = value;
}
public void ThreadProgress(ProgressBar p, int max, int value)
{
this.Invoke(new ProgressBarDelegate(ProgressBarDisplay),
new object[] { p, max, value });
}
private void btnStart_Click(object sender, EventArgs e)
{
string IsValidResult = IsValid();
//If string is empty, Utility passed
if (IsValidResult == "")
{
Thread t = new Thread(new ThreadStart(ProcessFiles));
t.Start();
}
else
{
MessageBox.Show(IsValidResult);
}
}
public void ProcessFiles()
{
int count = 0;
ThreadWorkDelegate w = ThreadWork;
ImageDisplayDelegate im = ThreadWorkImg;
ProgressBarDelegate pb = ThreadProgress;
try
{
foreach (MyPictures mp in lstHold.Items)
{
try
{
if (hasThreadBeenStopped == false)
{
//Disable certain controls during process. We will use the generic
//MethodInvoker, which Represents a delegate that can execute any method
//in managed code that is declared void and takes no parameters.
//Using the MethodInvoker is good when simple delegates are needed. Ironically,
//this way of multi-thread delegation was used because the traditional way as used
//by the rest of the delegates in this method, was not working.
btnApply.Invoke(new MethodInvoker(delegate { btnApply.Enabled = false; }));
btnStart.Invoke(new MethodInvoker(delegate { btnStart.Enabled = false; }));
//Call delegate to show current picture being processed
im.BeginInvoke(mp.ImageClone(), null, null);
mp.ImageSave();
//Increment Count; Image has been processed
count++;
//Invoke Img Proceessed Output
w.BeginInvoke(lblImgProcessed, count.ToString() +
" of " + lstHold.Items.Count.ToString() + " processed",
null, null);
//Invoke File Process Output
w.BeginInvoke(lblFile, mp.NewImgPath, null, null);
//Invoke Progressbar output. Delegate is passed The count of images,
//which will be set as the progressbar max value. the 'count' variable is
//passed to determine the current value.
pb.BeginInvoke(progressBar1, lstHold.Items.Count, count, null, null);
}
else //Thread has been called to stop
{
MessageBox.Show("Image Processing Stopped: " + count + "of " +
lstHold.Items.Count + " processed");
//Enable controls after process
btnApply.Invoke(new MethodInvoker(delegate { btnApply.Enabled = true; }));
btnStart.Invoke(new MethodInvoker(delegate { btnStart.Enabled = true; }));
break;
}
}
catch (Exception ex)
{
MessageBox.Show("Error while processing pictures");
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error while attempting to execute pictures: " + ex.ToString());
}
finally
{
//Loop has ended:
//In finally statement, re-enable disabled controls
//Enable certain controls during process
btnApply.Invoke(new MethodInvoker(delegate { btnApply.Enabled = true; }));
btnStart.Invoke(new MethodInvoker(delegate { btnStart.Enabled = true; }));
//Reset class variables
hasThreadBeenStopped = false;
imageProcessingComplete = false;
}
}
private void btnContent_Click(object sender, EventArgs e)
{
string selection = null;
string[] files = null;
lstAll.Items.Clear();
contentBrowser.ShowDialog();
selection = contentBrowser.SelectedPath;
txtContentFolder.Text = selection;
if (selection != "" || selection != null)
{
try
{
files = System.IO.Directory.GetFiles(selection.Trim());
foreach (string file in files)
{
lstAll.Items.Add(file);
}
}
catch (Exception ex)
{
// MessageBox.Show(ex.ToString());
}
}
}
private void btnGo_Click(object sender, EventArgs e)
{
//Grab files from folder based on user input in the textbox.
string selection = txtContentFolder.Text.Trim();
string[] files = null;
lstAll.Items.Clear();
if (valid.IsNull(selection) == false || valid.IsEmpty(selection) == false)
{
try
{
files = System.IO.Directory.GetFiles(selection);
foreach (string file in files)
{
lstAll.Items.Add(file);
}
}
catch (Exception ex)
{
MessageBox.Show("Invalid Directory");
}
}
txtContentFolder.Text = selection;
}
private void btnDestination_Click(object sender, EventArgs e)
{
string selection = null;
destinationBrowser.ShowDialog();
selection = destinationBrowser.SelectedPath;
txtNewImgPath.Text = selection;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnStop_Click(object sender, EventArgs e)
{
//Flag variable that the stop button has been called. This variable is checked
//conditionally when looping over each picture.
hasThreadBeenStopped = true;
}
public string IsValid()
{
StringBuilder sb = new StringBuilder("");
if (lstHold.Items.Count <= 0)
{
return "No items exist to process";
}
//Validate that there is a value in each field for every object in lstHold. All the fields will be
//validated. Note: If there is one invalid field, the rest do not need to be considered.
foreach (MyPictures mp in lstHold.Items)
{
if (mp.NewImgName == "")
{
sb.Append(mp.OldImgPath + ", ");
}
else if (mp.NewImgPath == "")
{
sb.Append(mp.OldImgPath + ", ");
}
else if (mp.NewImgFormat == null)
{
sb.Append(mp.OldImgPath + ", ");
}
else if (mp.NewWidth == 0)
{
sb.Append(mp.OldImgPath + ", ");
}
else if (mp.NewHeight == 0)
{
sb.Append(mp.OldImgPath + ", ");
}
}
//If the returned string is empty, the image is valid. The check for the listbox's count
//will return a string immediatly if false. Because of this, we know that the returning
//string at this level will either be empty (validation passed) or filled with image paths
//of images missing required values. If image is not valid, return this concatenated string of image paths
//that are missing values, and insert a prefixed string literal to this list.
if (sb.ToString() != "")
{
sb.Insert(0, "The following images are missing required values: ");
return sb.ToString();
}
else //String is empty and has passed validation
{
return sb.ToString();
}
}
private void btnMoveOne_Click(object sender, EventArgs e)
{
//Loop through All strings in the lstAll list box. Then use each picture path to convert
//each picture into their own class
foreach (string file in lstAll.SelectedItems)
{
//isImgExistFlag is a flag indicating wheter the image coming from lstAll already exists
//in lstHold. By default, the variable is false. It is set to true if an image does exist
//This variable must be re-created within the scope of the main foreach loop to ensure a proper
//reset of the variable for each image comparison.
bool isImgExistFlag = false;
try
{
Image img;
img = Image.FromFile(file);
MyPictures mp = new MyPictures(img,file);
//If lstHold contains no items, add the item with no validation check.
if (lstHold.Items.Count == 0)
{
lstHold.Items.Add(mp);
}
else
{
//Run through each object in the lstHold to determine if the newly created object
//already exists in list box lstHold.
for (int i = 0; i < lstHold.Items.Count; i++)
{
MyPictures p = (MyPictures)lstHold.Items[i];
//Unique objects will be identified by their Original Image Path, because
//this value will be unique
if (p.OldImgPath == mp.OldImgPath)
{
isImgExistFlag = true;
}
}
//If isImgExistFlag is false, the current Image object doesnt currently exist
//in list box. Therefore, add it to the list.
if (isImgExistFlag == false)
{
lstHold.Items.Add(mp);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
private void btnMoveAll_Click(object sender, EventArgs e)
{
//This event has the same functionality as btnMoveOne_Click, except the main foreach loop
//is based on all of lstAll's items, rather than just the selected items.
foreach (string file in lstAll.Items)
{
bool isImgExistFlag = false;
try
{
Image img;
img = Image.FromFile(file);
MyPictures mp = new MyPictures(img, file);
if (lstHold.Items.Count == 0)
{
lstHold.Items.Add(mp);
}
else
{
for (int i = 0; i < lstHold.Items.Count; i++)
{
MyPictures p = (MyPictures)lstHold.Items[i];
if (p.OldImgPath == mp.OldImgPath)
{
isImgExistFlag = true;
}
}
if (isImgExistFlag == false)
{
lstHold.Items.Add(mp);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
private void btnRemoveOne_Click(object sender, EventArgs e)
{
/*
Create a seperate List to populate:
This is necessary because if you explicitly remove an item from the listbox
you will get the following error:
"List that this enumerator is bound to has been modified. An enumerator can
only be used if the list does not change."
*/
//This variable will keep track of the first index processed.
int first_index = 0;
int count = 0;
List<MyPictures> TempMp = new List<MyPictures>();
if (lstHold.Items.Count >= 1)
{
try
{
foreach (MyPictures mp in lstHold.SelectedItems)
{
if (count == 0)
{
first_index = lstHold.SelectedIndex;
}
//Add objects to be removed
TempMp.Add(mp);
}
foreach (MyPictures mp2 in TempMp)
{
lstHold.Items.Remove(mp2);
}
}
catch (Exception ex)
{
//Hide Error: MessageBox.Show(ex.ToString());
}
//Select new item in list if possible, as long as there is a item in the list
if (lstHold.Items.Count >= 1)
{
//If the first_index variable = the amount of items in the list, the new selected index
//should be the first index -1. This is because the variable first_index would be the
//index of the now deleted item in the list. Therefore we must subtract the variable by 1
//before assigning it to the selected value. Otherwise, we'll be assigning a selected index that
//no longer exists.
//There is also a check to make sure there is more than one item in the list. Otherwise, we could
//potentially assign a selected index of -1.
if (first_index == lstHold.Items.Count && lstHold.Items.Count != 1)
{
lstHold.SelectedIndex = first_index - 1;
}
else if (lstHold.Items.Count == 1)
{
lstHold.SelectedIndex = 0;
}
else
{
lstHold.SelectedIndex = first_index;
}
}
else
{
ClearTextBoxes();
}
}
}
private void btnRemoveAll_Click(object sender, EventArgs e)
{
lstHold.Items.Clear();
ClearTextBoxes();
ImgPropertiesEnabled();
}
private void lstHold_SelectedIndexChanged(object sender, EventArgs e)
{
//This prevents trying to access a negative index. This can happen when a item is removed.
if (lstHold.SelectedIndex >= 0)
{
try
{
MyPictures mp = (MyPictures)lstHold.Items[lstHold.SelectedIndex];
txtOldName.Text = mp.OldImgName;
txtOldImgPath.Text = mp.OldImgPath;
txtOldImgFormat.Text = mp.OldImgFormat.ToString();
txtOldWidth.Text = mp.OldWidth.ToString();
txtOldHeight.Text = mp.OldHeight.ToString();
txtNewName.Text = mp.NewImgName;
cbFormat.SelectedItem = mp.NewImgFormat;
txtNewWidth.Text = mp.NewWidth.ToString();
txtNewHeight.Text = mp.NewHeight.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//Call function to determine which controls should be enabled/disabled
ImgPropertiesEnabled();
}
private void btnApply_Click(object sender, EventArgs e)
{
//Reset color. It could be grey depending on if user changed default name.
txtNewName.ForeColor = Color.Black;
if (lstHold.SelectedIndex == -1)
{
MessageBox.Show("Picture not selected. Select picture to apply properties to.");
}
else if (lstHold.SelectedIndex >= 0)
{
MyPictures mp = (MyPictures)lstHold.Items[lstHold.SelectedIndex];
//User wants to apply a generated name to all pictures within the list
if (chkNewPicName.Checked == true)
{
int count = 0;
foreach (MyPictures pic in lstHold.Items)
{
pic.NewImgName = txtNewName.Text + count.ToString();
++count;
}
txtNewName.Text = mp.NewImgName;
}
//User wants to apply a custom name to this picture only
else
{
mp.NewImgName = txtNewName.Text;
}
//User wants to apply this path to all pictures within the list
if (chkNewPicPath.Checked == true)
{
foreach (MyPictures pic in lstHold.Items)
{
pic.NewImgPath = txtNewImgPath.Text;
}
txtNewImgPath.Text = mp.NewImgPath;
}
//User wants to apply this path to this picture only
else
{
mp.NewImgPath = txtNewImgPath.Text;
}
//User wants to apply this image format to all pictures within the list
if (chkNewPicFormat.Checked == true)
{
foreach (MyPictures pic in lstHold.Items)
{
pic.NewImgFormat = (ImageFormat)cbFormat.SelectedItem;
}
}
//User wants to apply this image format to this picture only
else
{
mp.NewImgFormat = (ImageFormat)cbFormat.SelectedItem;
}
//User wants to apply this size to all pictures
if (chkNewSize.Checked == true)
{
foreach (MyPictures pic in lstHold.Items)
{
pic.NewWidth = Convert.ToInt32(txtNewWidth.Text);
pic.NewHeight = Convert.ToInt32(txtNewHeight.Text);
}
txtNewWidth.Text = mp.NewWidth.ToString();
txtNewHeight.Text = mp.NewHeight.ToString();
}
//User wants to apply this size to this picture only
else
{
mp.NewWidth = Convert.ToInt32(txtNewWidth.Text);
mp.NewHeight = Convert.ToInt32(txtNewHeight.Text);
}
mp.NewImgName = txtNewName.Text;
mp.NewImgFormat = (ImageFormat)cbFormat.SelectedItem;
mp.NewWidth = Convert.ToInt32(txtNewWidth.Text);
mp.NewHeight = Convert.ToInt32(txtNewHeight.Text);
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (chkSelectAll.Checked)
{
chkNewPicName.Checked = true;
chkNewPicPath.Checked = true;
chkNewPicFormat.Checked = true;
chkNewSize.Checked = true;
}
else
{
chkNewPicName.Checked = false;
chkNewPicPath.Checked = false;
chkNewPicFormat.Checked = false;
chkNewSize.Checked = false;
}
}
private void previewToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("hi there!");
}
private void btnPreview_Click(object sender, EventArgs e)
{
try
{
if (lstHold.Items.Count <= 0)
{
MessageBox.Show("No pictures are available to preview");
}
else if (lstHold.SelectedItem == null)
{
MessageBox.Show("No picture is selected to preview");
}
else
{
MyPictures mp = (MyPictures)lstHold.SelectedItem;
//Bitmap bmp = new Bitmap(mp.OldImgPath);
Form2 frm = new Form2(mp);
frm.Show();
}
}
catch (Exception ex)
{
MessageBox.Show("An Error has occured:\n " + ex.ToString());
}
}
public void ImgPropertiesEnabled()
{
//Enable Image properties when an image is selected
if (lstHold.SelectedIndex >= 0)
{
gbCheckAll.Enabled = true;
gbImgProperties.Enabled = true;
}
else
{
//Disable Image properties when an image is not selected
gbCheckAll.Enabled = false;
gbImgProperties.Enabled = false;
}
//Preview buttons enablement will depend on the same conditions
btnPreview.Enabled = gbImgProperties.Enabled;
}
public void ClearTextBoxes()
{
txtNewImgPath.Text = "";
txtNewName.Text = "";
txtNewHeight.Text = Convert.ToString(0);
txtNewWidth.Text = Convert.ToString(0);
cbFormat.SelectedItem = null;
chkSelectAll.Checked = false;
}
}
Having scanned through the code, yes it is eleborate... maybe a little to much ;)
One thing that i noticed was your naming conventions. Even though it does not change anything in runtime it does make an API/code-maintenance easier.
So, instead of having an IPicture, i would make it something like `IResizableImage´ (reading your spec, thats what it is. Not just a picture, but a resizable one)
Instead of ´ReturnImage()´ i would use something like ´Scale()´. 'ImageSave()' to 'Save()'
Your code will start to read (Which added symantical information by naming convention)
IResizableImage myImg = new ResizableImage( orignalBitmap );
Image rescaledImg = myImg.Scale( "new path", 320,240 );
resccaledImg.Save();
instead of:
IPicture myImg = new MyPictures();
Image rescaled = myImg.ReturnImage( "newpath", 320, 240 );
rescaledImg.ImageSave();
So, Generally classes are nouns, methods are verbs, adjetives are properties/fields. Try to minimize duplication or redancy. "ImageSave" is a method on your Image. Isn't "Image.Save()" clearer than "Image.ImageSave()"?
Just some of my thoughts;
In coding guidelines there is no absolute right or wrong.
Think of being another person when USING the API versus WRITING the API. Jump out of the box of "i know what it does" and imagine being a user never having seen this API before. Does it feel natural and easy accesible?
Hope this helps,
Here are some improvements for you code and design. This tips are not all OO related but you should be aware that good design is not just OO design.
1.Avoid commenting what is obvious.
//Declare variables to hold values that have been determined
private int _OldWidth;
This comment is superfluous because any programmers will understand that is a declaration.
2.Avoid giving wrong name. For example the class "MyPictures" is not really correct because:
Is holds just one picture, while the name suggests many pictures.
It contains "My" which, in my opinion is not correct since if I read your code is not my class. It is yours ;)
3.Avoid concatenating strings. Use string.Format or, for paths, Path.Combine
bmp.Save(_NewImgPath + #"\" + _NewImgName + "." + _NewImgFormat.ToString().ToLower(), _NewImgFormat);
4.Keep methods short. It is hard to keep all methods to 5 lines of code but 30 lines (if my count is correct - without comments and empty lines) for ProcessFiles is a little bit too much.
5.Don't use design elements just because you want to have them. I see no reason to use the interface in your code. In your case it just increases the complexity of your code. Even more, you haven't used it(!!!). You just implemented it and that's all. Use interfaces when you have multiple types that share common functionality (the ones in interface) and you want to treat them all similar without being aware of the actual implementation.
interface IImage
{
void DrawLine(Point startPoint, Point endPoint);
}
class MonochromeImage:IImage
{
void DrawLine(Point startPoint, Point endPoint)
{
//Draw a monochrome line on images with one channel
}
}
class ColorImage:IImage
{
void DrawLine(Point startPoint, Point endPoint)
{
//Draw a red line on images with three channels
}
}
...
void DrawLineOnImage()
{
List<IImage> images = new List<IImage>();
images.Add(new ColorImage());
images.Add(new MonochromeImage());
//I am not aware of what kind of images actually have
//all it matters is to have a draw line method
foreach(IImage image in images)
{
image.DrawLine(p1,p2)
}
}
6.As others already mentioned, try to separate the presentation (graphical user interface - GUI) from the logic. Make it in such a way that you can replace the GUI without changing logic code.
7.Make single responsibility functions. btnMoveOne_Click has more than one responsibility: it checks if file exists and it handles elements on user interface.
8.You image class is coupled to the file system. What happens if I want to store images created in memory? What is the path then? Here is where you can improve the design of the class. Make it in such a way it doesn't matter if files are from disk (HINT: in a FileStream) or from memory (HINT: in a MemoryStream) or any other place.
That's all for now. Hope this information will help you.
To achieve good design you need to apply TDD (Test Driven Design).
You will soon find then testability requires separating the project to layers, such as presentation and business logic.
Start covering your project with tests, and you won't believe how fast you will find design inconsistences with it.
Things will just stand up and scream: "No way you will test me!"
The worst anemy nere is the code buried in the WinForms.
What you can do is making a view "humble". http://codebetter.com/blogs/jeremy.miller/archive/2007/05/23/build-your-own-cab-part-2-the-humble-dialog-box.aspx
As for the project samples, you have to look at architectural patterns, not the OOP samples.
The keywords you will be lookign for are MVC, MVP, MVVM.
Well, here's what I'd do. It's probably different than what many people would do, but I think it's a pretty good, flexible design.
public abstract class AbstractConverter : IImageHandler
{
public AbstractConverter(IImageHandler nextHandler)
{
output = nextHandler;
}
public void HandleImage(Bitmap b)
{
var outBitmap = Convert(b);
output.HandleImage(outBitmap);
}
protected abstract Bitmap Convert(Bitmap input);
private IImageHandler output;
}
public interface IImageHandler
{
void HandleImage(Bitmap b);
}
Now, the rest of your app is:
Creating implementations of AbstractConverter to handle the individual transformations you want
Creating something that can build and wire converters together
Getting the initial Bitmap, and writing the final result out.