Data reset after leaving a constructor - c#

I have probably a very beginner question,yet I can't figure out why is it like that. I am trying to pass in constructor a StringBuilder which via Debugger I nailed but as soon as my last step in Debugger leaves this constructor which is in different class, it gets back to null. I know that it is a reference type and therefore only a reference gets copied but even if I pass it by reference by "ref" result is the same...maybe I get it wrong or there is something else wrong...
class DifferentClass
{
public void Method()
{
StringBuilder[] PathtoFiles = new StringBuilder[numberOfImages];
for (int i = 0; i < numberOfImages; i++)
{
PathtoFiles[i] = new StringBuilder();
// string pattern for correct file naming/saving
string nameOfFile = string.Format("{0}{1}{2}", adresaPath, i, ".jpg");
PathtoFiles[i].Append(nameOfFile);
}
Pictures picture = new Pictures(ref PathtoFiles);
}
}
class Pictures
{
public StringBuilder[] sb;
public Pictures(ref StringBuilder[] sb)
{
this.sb = sb;
}
public Pictures()
{
}
public void LoadPictures(ImageList img)
{
for (int i = 0; i < sb.Count(); i++)
{
img.Images.Add(string.Format("pic{0}", i), Image.FromFile(sb[i].ToString()));
}
}
}
Upon request I am enclosing another bit of code this time in class where the method LoadPictures gets called:
class ThirdClass
{
DifferentClass diff = new DifferentClass();
Pictures picture = new Pictures();
private void btn_Download_Click(object sender, EventArgs e)
{
diff.Method();
//this is a control data is supposed to be saved in
picture.LoadPictures(imageList1);
}
}

Well there ya go:
class ThirdClass
{
DifferentClass diff = new DifferentClass();
Pictures picture = new Pictures();
private void btn_Download_Click(object sender, EventArgs e)
{
diff.Method();
//this is a control data is supposed to be saved in
picture.LoadPictures(imageList1);
}
}
You are using the default constructor. Which doesn't set StringBuilder[] Pictures.sb
This is assuming of course that code you re-edited is the code you are working with.
In that case you need to figure out how to instantiate DifferentClass, maybe in ThirdClass ctor. And perhaps you want DifferentClass.Method() to return a Picture object you can initialize also in ThirdClass constructor.
There are several ways you can do this. Up to you to choose the best method.

public void Method()
{
// the StringBuilder
// ...
Pictures picture = new Pictures(ref PathtoFiles);
}
initialization of picture is correct. but it is a local variable, and when Method() is completed, you can't access it anymore
you can modify your method to keep result in variable on client-side:
public Pictures Method()
{
// the StringBuilder
// ...
return new Pictures(ref PathtoFiles);
}
client-side
var pics = new DifferentClass().Method();
ImageList imgs = new ImageList();
pics.LoadPictures(imgs);

Related

Form doesn't animate properly

In my application i have two Forms (that's my 1st quite big app)
After clicking start button in parent form i want loading panel to appear, and some logic to be done.
Loading panel (it is just another widowss form) contains bunifu loading circle animation (and some text).
Logic part is responsible for collecting names from directory tree, then replacing some text in Ms.Word files on the tree.
When i open loading panel without executing the logic, loading panel is animated properly and everything works fine.
private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
int x = this.Location.X+this.Width/2-75;
int y = this.Location.Y +this.Height/2-175;
Loader_panel LP = new Loader_panel();
LP.Left = x;
LP.Top = y;
LP.Show();
//System.Threading.Thread.Sleep(5000); \\this doesn't help animation to start
if (FormLogic._dataList.Count > 0) \\Here Logic part starts
{
for (int i = 0; i < FormLogic._dataList.Count; i++)
GetDir.GetTarget(FormLogic._dataList[i]);
/*foreach (var directory in FormLogic._dataList)
GetDir.GetTarget(directory);*/
LogList.Items.Add(DateTime.Now + "List isn't empty");// for testing
FormLogic.ClearData();
}
LP.Close();
}
After enabling logic loading panel appears (appearance isn't smooth), but animation doesn't work (it starts to work only when logic part did the work - i tested it by disabling LP.Close(). What can be reason of this problem ?
Additinal question. In .NET environment the code is compiled to work with multiple processor threads or i have to do it manually ?
EDIT 06/08/2018 7:21 CEST
I cant access LogList from GetDir Method (due to processing it by other thread).
I tried multiple Invoke construction, but none of it seemed to work ;/
I am just too rookie to figure it out. I specified more details in code below:
namespace Docr
{
public partial class DocrForm : Form
{
.....
private async void Button1_Click(object sender, EventArgs e)
{
int x = this.Location.X + this.Width / 2 - 75;
int y = this.Location.Y + this.Height / 2 - 175;
Loader_panel LP = new Loader_panel();
LP.Left = x;
LP.Top = y;
LP.Show(); //animation
int count = FormLogic._dataList.Count;
var list = FormLogic._dataList;
await Task.Run(() =>// processing logic during showing animation
{
if (count > 0)
{
for (int i = 0; i < count; i++)
{
GetDir.GetTarget(list[i],LogList); // Passing LogList as and argument
}
Invoke((Action)(() => { LogList.Items.Add(DateTime.Now + "Hi LogList"); }));\\ works fine
}
});
FormLogic.ClearData();
LP.Close();
}
....
}
namespace DocrLogic
{
class GetDir
{
.....
static public void GetTarget(string UserDirectory, ListBox List)// passing ListBox as an Argument
{
var path = UserDirectory;
var TargetDir = new DirectoryInfo(path);
var AllDocs1 = TargetDir.GetFiles("*.doc*", SearchOption.AllDirectories);
var ProperPrefixes = new List<string> { };
Invoke((Action)(() => { List.Items.Add(DateTime.Now + "Hi Log List, GetDir here"); })); // THIS DOESN'T WORK
....
}
.....
}
}
You need to make the method async, then use await to wait for the logic to complete. This way the LP form won't be interrupted by the heavy logic.
private async void bunifuFlatButton1_Click(object sender, EventArgs e)
{
int x = this.Location.X+this.Width/2-75;
int y = this.Location.Y +this.Height/2-175;
Loader_panel LP = new Loader_panel();
LP.Left = x;
LP.Top = y;
LP.Show();
int count = FormLogic._dataList.Count;
var list = FormLogic._dataList;
await Task.Run(()=>
{
if(count > 0)
{
for (int i = 0; i < count; i++)
{
GetDir.GetTarget(list[i]);
}
this.Invoke(() => { LogList.Items.Add(DateTime.Now + "List isn't empty"); });
}
});
FormLogic.ClearData();
LP.Close();
}
You have to make your code thread-safe by using Invoke to access not-thread-safe objects such as UI objects, otherwise, it will throw a System.Threading.ThreadAbortException or a System.InvalidOperationException.
Invoke syntax may differ based on your project but you may
see this post to understand the proper ways of using Invoke()
update
You must never try to access a UI object outside invoke. invoke is provided by System.Windows.Forms.Control and depends on the thread which originally created that control. therefore having Invoke on some other random class simply does not work.
In the second part, you need to change
public static void GetTarget(string UserDirectory, ListBox List)// passing ListBox as an Argument
{
...
Invoke((Action)(() => { List.Items.Add(DateTime.Now + "Hi Log List, GetDir here"); })); // THIS DOESN'T WORK
}
to
(you need to send the whole invoke line as the action parameter)
public static void GetTarget(string UserDirectory, Action action)// passing the action as an Argument
{
...
action();
}
or
(you need to set Dispatcher to LogList before starting the Task)
public static Control Dispather;
public static void GetTarget(string UserDirectory)// passing the action as an Argument
{
...
Dispather.Invoke((Action)(() => { List.Items.Add(DateTime.Now + "Hi Log List, GetDir here"); }));
}

Dynamic Checkbox from a txt file entry on a WinForm UI

Good Morning;
Actually have 2 questions. My first is what is this called? A Program? A Module?
WhatIsThisCalled()
{
//workToBeDone
}
I'm trying to create dynamic checkbox(s) from each entry in a text file. I'm trying to reuse the code so I have tried to create the module in a logic file. I feel like I've done this correctly, but I can't test it. I can not figure out how to reference
this.Controls.Add(chk[I]);
to the winForm I want to call it on. The error I get is about it being illegal in a static method. I'm only trying to clear the error (last one) so I can see if it will actually put the checkboxes onto the correct winForm Permissions.cs. Here is my Logic.cs module.
public static void getPermText()
{
Stream fileStream = File.Open(dataFolder + PermFile, FileMode.Open);
StreamReader reader = new StreamReader(fileStream);
string line = null;
do
{
line = reader.ReadLine();
if (line == null)
{
break;
}
string[] parts = line.Split('\n');
try
{
int userCount;
userCount = parts.Length;
CheckBox[] chk = new CheckBox[userCount];
int height = 1;
int padding = 10;
for (int i = 0; i <= userCount; i++)
{
chk[i] = new CheckBox();
chk[i].Name = parts.ToString();
chk[i].Text = parts.ToString();
chk[i].TabIndex = i;
chk[i].AutoCheck = true;
chk[i].Bounds = new Rectangle(15, 30 + padding + height, 150, 22);
this.Controls.Add(chk[i]);
height += 22;
}
}
catch
{
}
} while (true);
}
There is one global int userCount = 0; above that module. On Permissions.cs I have this code (with no errors).
public Permissions()
{
InitializeComponent();
}
private void Permissions_Load(object sender, EventArgs e)
{
WidgetLogic.getPermText();
}
Can anyone please direct me as to how, or if it is possible, to replace Permissions with this in a dynamic format?? I think??
Thank you very much in advance for all that look or help. I really appreciate it. Have a Great Day!! :)
I tired very hard to understand what you wanted to say. I think you just want to reference the form where the CheckBoxes should create.
So you should better pass the reference the of the form on which you want to create the controls:
public static void getPermText(System.Windows.Forms.Form targetForm)
{
//code
targetForm.Controls.Add(chk[i]); //changed "this" to "targetForm"
To call the method:
WidgetLogic.getPermText(this); //here "this" refers to the current form
Now where ever you will call this method it will create the controls on your form (the one you're passing as the parameter).
Notify me if I got your question wrong.

Looping through Textboxes to retrieve data, c#

I have a class called "Player" with a constructor that takes 2 strings and an int.
These are declared in Textboxes on a form, with each team (Home H / Away A) having a different name, and each name type (Last L / First F) adding to the textbox's name. therefor giving a unique name such as txtFHome1.
In a foreach loop I want to create a new Player with the details of the textboxes on a page.
This is what I have got so far.
List <Player> HomeTeam = new List<Player>
private void btnAccept_Click(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
for (int i = 0; i < 10; i++)
{
HomeTeam.Add (new Player(c.Name.EndsWith("FHome"+i),c.Name.EndsWith("LHome"+i),c.Name.EndsWith("upDownH"+i)));
}
}
}
any help?
From you post I understand that there are always 3 controls for 11 players, so there is no need to iterate trough all Controls in the form.
private void btnAccept_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
var player = new Player(((TextBox)this.Controls.FindControl("FHome" + i)).Text, ((TextBox)this.Controls.FindControl("LHome" + i)).Text, ((NumericUpDown)this.Controls.FindControl("upDownH" + i)).Value);
HomeTeam.Add(player);
}
}
The first way is to use dictionaries with all controls. It is the fastest and easiest way.
Dictionary<int, TextBox> FHome;
Dictionary<int, TextBox> LHome;
Dictionary<int, TextBox> upDownH;
You should add the controls like this:
FHome.Add(0, textBoxLHome0);
FHome.Add(1, textBoxLHome1);
Then in your code you can use
for (int i = 0; i < 10; i++)
{
HomeTeam.Add (new Player(FHome[i].Text, LHome[i].Text, upDownH[i].Text));
}
try this:
Controls.OfType<TextBox>().ToList().ForEach((textbox) =>
{
for (int i = 0; i < 10; i++)
{
textbox.Text = "your text";
}
});
remember to include using System.Linq
I advice you to at lease try an encapsulate the three textboxes into a single UserControl like so:
public partial class ControlPlayerParams : UserControl {
public string Param1 { get { return this.textBox1.Text; } }
public string Param2 { get { return this.textBox2.Text; } }
public string Param3 { get { return this.textBox3.Text; } }
public ControlPlayerParams() {
this.InitializeComponent();
}
}
That way, you could at least do what you wanted to do more fluently and more safely (plus you get to modify just one UserControl in case you need something changed (Validation ??)):
foreach (ControlPlayerParams cpp in this.Controls.OfType<ControlPlayerParams>())
HomeTeam.Add(new Player(cpp.Param1, cpp.Param2, cpp.Param3));
BUT you should rethink the architecture of the app a bit if you ask me.

List information disappearing

THIS IS HOMEWORK: I have a program that consists of two winforms and three classes. The program does the work it is supposed to do for the main form and displays it appropriately onto the textbox of the main form. In addition to displaying the information, it is also saved to a string list. The data consists of order information.
The string list passes that information along to a method in another class and stores it in a list as it is supposed to do. After this is done, I press a button which opens up another form. In this form I enter an order number. What is supposed to happen is that a method compares the order number entered with the row number of the list and return that information, whereupon it is displayed in the textbox of the second form.
That is what is supposed to happen. Instead, when it is time to compare the order number with the row number of the list, the list data is gone and I cannot figure out why. Here is my code that pertains:
private void btnPaymentButton_Click(object sender, EventArgs e)
{
amountPaid = double.Parse(this.txtAmountPaid.Text);
orderObject.GetChangeDue(orderObject.TotalAmountDue, amountPaid);
this.txtNumberOfPizzaOrdered.Clear();
this.txtNumberOfCokesOrdered.Clear();
this.txtAmountDue.Clear();
this.txtAmountPaid.Clear();
this.lblYourOrder.Visible = true;
this.rtxtYourOrder.Visible = true;
this.rtxtYourOrder.Text = orderObject.OrderSummary(amountPaid);
//storeOrderObject = new DailySummary(orderObject.OrderSummary(amountPaid));
storeOrderObject = new DailySummary(this.rtxtYourOrder.Text);
}
private void btnDailySummary_Click(object sender, EventArgs e)
{
DailySummaryForm form = new DailySummaryForm();
// this.Visible = false;
form.Show();
}
........
public class DailySummary
{
//declare variables
int numberOfCokes = 0,
numberOfPizzas = 0,
totalOfCokes = 0,
totalOfPizzas = 0,
orderNumberRequest = 0;
string orderFromForm1 = "",
getAllTheOrders = "",
getAnOrder = "";
List<string> pizzaOrderList = new List<string>();
public DailySummary(string orderForm)
{
orderFromForm1 = orderForm;
StoreOrder(orderFromForm1);
}
public DailySummary(int orderRequest)
{
orderNumberRequest = orderRequest;
GetOrder(OrderNumberRequest);
}
public int OrderNumberRequest
{
get
{
return this.orderNumberRequest;
}
}
//store order
public void StoreOrder(string orderFromForm1)
{
pizzaOrderList.Add(orderFromForm1);
}
//get the order
public string GetOrder(int OrderNumberRequest)
{
for (int row = 0; row < pizzaOrderList.Count; row++)
{
if (row == (OrderNumberRequest - 1))
{
getAnOrder = pizzaOrderList[row];
}
}
return getAnOrder;
}
........
public partial class DailySummaryForm : Form
{
int orderNumberRequest = 0;
//instantiate a from object
OrderForm formObject;
DailySummary summaryObject;
public DailySummaryForm()
{
InitializeComponent();
}
private void btnOrderNumberButton_Click(object sender, EventArgs e)
{
orderNumberRequest = int.Parse(this.txtOrderNumber.Text);
summaryObject = new DailySummary(orderNumberRequest);
this.rtxtDisplayOutput.Visible = true;
this.rtxtDisplayOutput.Text = summaryObject.GetAnOrder;
}
In your method btnPaymentButton_Click you create an instance of the DailySummary class, then later on when your in the DailySummaryForm you create a new instance of the DailySummary Class.
These instances are separate and therefore do not share the same values.
As your not persisting values to the DB you'll probably want to:
look into making your DailySummary Static (which will make it accessable throughout your
winform application). (It's not typically good practice to have too many global (static) variables hanging around, but without a persistence engine where you can store your Daily Summary you need a common place to look it up.
Pass the initialized instance of the DailySummary class to your DailySummaryForm. (The DailySummaryForm could expose a public property. The drawback of this method is if your switching between both forms and each form modified your summary class, your going to constantly have to pass it back and forth between the forms.
DailySummaryForm is creating a new instance of
DailySummary.
and that is why it is empty.

C# : Mini Application Structural Design (Classes/Interfaces/etc.)

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.

Categories

Resources