C# Windows Forms App: Separate GUI from Business Logic - c#

I would like some advice on how to separate the UI and business logic in a simple C# Windows Forms Application.
Let's take this example:
The UI consists of a simple textbox and a button. The user enters a number between 0 and 9 and clicks the button. The program should add 10 to the number and update the text box with that value.
The business logic part should have no idea of the UI. How can this be accomplished?
Here's the empty Process class (Business Logic):
namespace addTen
{
class Process
{
public int AddTen(int num)
{
return num + 10;
}
}
}
The requirement is:
When the user clicks the button, somehow, the Process::AddTen gets invoked.
The Textbox must be updated with the return value of Process::AddTen.
I just don't know how to connect these two.

First, you need to change your class name. "Process" is name of a class in the Class Library and will likely cause confusion for anyone reading your code.
Let's assume, for the rest of this answer that you changed the class name to MyProcessor (still a bad name, but not a well-known, often-used class.)
Also, you're missing the code to check to ensure that the user input is, indeed, a number between 0 and 9. That's appropriate in the Form's code rather than the class code.
Assuming the TextBox is named textBox1 (The VS generated default for the first TextBox added to the form)
Further assuming the button's name is button1
In Visual Studio, double-click on the button to create the button click event handler, which will look like this:
protected void button1_Click(object sender, EventArgs e)
{
}
Within the event handler, add code so it looks like this:
protected void button1_Click(object sender, EventArgs e)
{
int safelyConvertedValue = -1;
if(!System.Int32.TryParse(textBox1.Text, out safelyConvertedValue))
{
// The input is not a valid Integer value at all.
MessageBox.Show("You need to enter a number between 1 an 9");
// Abort processing.
return;
}
// If you made it this far, the TryParse function should have set the value of the
// the variable named safelyConvertedValue to the value entered in the TextBox.
// However, it may still be out of the allowable range of 0-9)
if(safelyConvertedValue < 0 || safelyConvertedValue > 9)
{
// The input is not within the specified range.
MessageBox.Show("You need to enter a number between 1 an 9");
// Abort processing.
return;
}
MyProcessor p = new MyProcessor();
textBox1.Text = p.AddTen(safelyConvertedValue).ToString();
}
The class, with the access modifier set properly, should look like this:
namespace addTen
{
public class MyProcessor
{
public int AddTen(int num)
{
return num + 10;
}
}
}

You can create another class called "Process.cs" for example.
Methods that involve processing or data calculation you move there. In your case for example:
public class Process
{
public int AddTen(int num)
{
return num + 10;
}
}
Your UI click event will have a call to your "Process layer":
var myProcess = new Process();
//and then calculation
var initNumber = Convert.ToInt32(textBox.Text);
var calculatedValue = myProcess.AddTen(initNumber);
textBox.Text = calculatedValue.ToString();
This way your business logic, such as calculating is kept separately. If your UI changes you can still simply call myProcess.AddTen() method whether it's a web, Windows or a Mobile form.

Make your 'Process' class public (and as #DavidStratton says, change the name):
public class MyProcess
I would say you should parse your string value from TextBox.Text to an int:
private void button1_Click(object sender, EventArgs e)
{
MyProcess myProcess = new MyProcess();
string result = textBox1.Text;
int number;
if(int.TryParse(textBox1.Text, out number))
{
result = myProcess.AddTen(number).ToString();
}
textBox1.Text = result;
}

To separate the logic completely you can declare a base class that can contain the button and manage handlers. Your specific process can inherit the base class, and the logic can be set. Finally the form can declares an instance of the class and pass in the button.
It looks something like this:
class BaseProcessor
{
System.Windows.Forms.Button myButton;
public System.Windows.Forms.Button MyButton
{
get
{
return myButton;
}
set
{
myButton = value;
myButton.Click += new System.EventHandler(this.MyButton_Click);
}
}
public BaseProcessor()
{
}
public virtual void MyButton_Click(object sender, EventArgs e)
{
}
Then declare the process:
class MyProcess : BaseProcessor
{
public override void MyButton_Click(object sender, EventArgs e)
{
MessageBox.Show("This is my process");
}
}
Then inside the form, declare an instance of the process and attach the button:
public partial class Form1 : Form
{
MyProcess myProcess = null;
public Form1()
{
InitializeComponent();
myProcess = new MyProcess
{
MyButton = button1
};
}
}
Using this method, there is no business logic code in the form. The parent class is useful because events like clicking buttons are pretty common so its easier to declare them centrally, in my opinion.

Related

How to retain the value from an numericupdown after closing a form

I have the following question. I am using C# .NET, and I want to save a value in numericupdown box after I close my form. In my aplication I have in total 2 forms, so I want to save the value I enter in the second one, and after I open it again I want to see the last value. In my case the numericupdown value is empty after I open the second form again.
I was thinking about something like this:
namespace Project2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
decimal a = numericUpDown1.Value;
label2.Text = "N: " + a;
}
}
}
But is still empty after I open it again.
You can create a class which provides set/get for a NumericUpDown control in this case using the following.
public sealed class Setting
{
private static readonly Lazy<Setting> Lazy =
new Lazy<Setting>(() => new Setting());
public static Setting Instance => Lazy.Value;
public decimal NumericUpDownValue { get; set; }
}
In the child form, OnShown set Value property to Settings.NumericUpDownValue then OnClosing remember the value.
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
Shown += (sender, args) =>
numericUpDown1.DataBindings.Add("Value", Setting.Instance,
nameof(Setting.NumericUpDownValue));
Closing += (sender, args) =>
Setting.Instance.NumericUpDownValue = numericUpDown1.Value;
}
}
The code above, specifically Settings class is known as the Singleton Pattern which you can learn more about in Implementing the Singleton Pattern in C#.
You can use static variable to store last updated value and with the reference of class name you can use it where ever you want.
From MSDN: Two common uses of static fields are to keep a count of the number of
objects that have been instantiated, or to store a value that must
be shared among all instances.
Like,
namespace Project2
{
public partial class Form2 : Form
{
public static decimal lastNumericUpDownValue = 0;
public Form2()
{
//For example: thiw will print lastest saved Numeric updown value.
//For the first time, it will print 0
Console.WriteLine(Form2.lastNumericUpDownValue);
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Assign value to lastNumericUpDownValue variable. Look at how it is used.
Form2.lastNumericUpDownValue = numericUpDown1.Value;
}
}
}

C# WPF Window with 'out' for one of the parameters

My problem is I have a variable that I need the window to send back, so I am using out to accomplish this. Here is an example of the constructor for the WPF window.
public CustomYesNo(out bool FormFilled)
{
InitializeComponent();
FormFilled = false;
}
The problem i'm having is I want it so one of the other methods in the class will be able to modify the FormFilled variable that gets sent back to the calling class like below.
private void Button_Yes_Click(object sender, RoutedEventArgs e)
{
FormFilled = true;
Close();
}
Obviously the Button_Yes_Click method does not have access to the FormFilled variable, and I am trying to figure out how I could possible change the value of the FormFilled variable from this method since this variable is only in the constructor's scope. Is what I am trying to do possible using 'out' or do I need to go another route?
Try this pattern
Calling Method:
class foo
{
public void bar()
{
DialogForm myDialogForm = new DialogForm();
myDialogForm.ShowDialog();
if (myDialogForm.DialogResult)
{
//Its true
}
}
}
Form window:
public partial class DialogForm : Window
{
public DialogForm()
{
InitializeComponent();
}
void submitButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
}
The calling method in WPF is slightly different for checking the result:
bool? result = myDialogForm.ShowDialog();
if (result.HasValue && result.Value)

Different parents for new Winform with a single constructor

Im running into a bit of an issue regarding Children and parents.
I have 2 forms which have the same dropdown menus, both of which have the ability to add additional options to them. When the "(add new)" option is selected in any of the combo boxes my third form is loaded which enables the addition of a new option.
This is the code for that third window (as it stands)
public partial class taskNewDropdownEntry : Form
{
taskWindow _owner;
applianceWindow _owner2;
int windowType;
int manufacturer_id;
sqlMod data = new sqlMod();
public int setManufacturerID {get { return manufacturer_id; } set { manufacturer_id = value; } }
public taskNewDropdownEntry(taskWindow owner, int type)
{
InitializeComponent();
this._owner = owner;
this.windowType = type;
}
public taskNewDropdownEntry(applianceWindow owner, int type)
{
InitializeComponent();
this._owner2 = owner;
this.windowType = type;
}
private void taskNewDropdownEntry_Load(object sender, EventArgs e)
{
if (windowType == 1)
{
instructionLabel.Text = "Input the new appliance type below";
}
else if (windowType == 2)
{
instructionLabel.Text = "Input the new manufacturer below";
}
else if (windowType == 3)
{
instructionLabel.Text = "Input the new model below";
}
}
private void btnOK_Click(object sender, EventArgs e)
{
if (windowType == 1)
{
data.insertApplianceType(textField.Text);
_owner.refreshTypeCombo();
}
else if (windowType == 2)
{
data.insertManufacturerSimple(textField.Text);
_owner.refreshManuCombo();
}
else if (windowType == 3)
{
data.insertModelSimple(manufacturer_id, textField.Text);
_owner.refreshModelCombo();
}
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
Now, my issue is that the 2 forms that call this third form are different - thus my only thought of how to solve this would be to duplicate some of the code and modify the methods (you can see the second constructor already added).
Instead of having multiple constructors, and duplicated methods (in this class, or in a seperate one) is there a way whereby I can use the same constructor but different owners depending on the form that calls it?
You have too much implementation in your child form. The way I would tackle this is to
Add a property to your child form:
public string InstructionLabel { get; set; }
This allows your parent forms to individually set the label text when instantiating the form, and also set up an event handler for when the form is closing. So your parent form would have code something like
var newItemForm = new taskNewDropdownEntry();
newItemForm.InstructionLabel = "Input the new appliance type below";
newItemForm.FormClosing += new FormClosingEventHandler(ChildFormClosing);
Then somewhere early in your child form's life cycle (FormLoading event) set
instructionLabel.Text = InstructionLabel;
Then also add a property in the child form for
public string NewItem { get; set; }
your child form should set this public property in the btnOK_Click event
private void btnOK_Click(object sender, EventArgs e)
{
this.NewItem =textField.Text;
}
Then your parent form listens for a FormClosing event, and when it hits that event it takes the NewItem text, adds it to the relevant combo and refreshes it. So in the parent form, the handler looks like
private void ChildFormClosing(object sender, FormClosingEventArgs e)
{
sqlMod data = new sqlMod();
data.insertApplianceType(textField.Text);
refreshTypeCombo();
}
Pretty hard to understand the question but code speaks for all.
There are 2 options, worse (because keeping the parent reference is not a good practice first of all):
create an interface that both classes taskWindow and applianceWindow (where is the naming convention for god's sake!) implement, ex
intrerface IRefreshable {
void refreshManuCombo();
}
then constructor and your poperty can have type of IRefreshable
IRefreshable _owner;
public taskNewDropdownEntry(IRefreshable owner, int type)
{
InitializeComponent();
this._owner = owner;
}
better option, use child form events like Closed to implement refreshing logic in parent. You just need to register event handler before showing the form and voila. Check examples here:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.closed(v=vs.110).aspx
You can also implement your own public form event for more custom usage (ex. DataChanged, ResultGenerated).

Make a radio button input from a form public in C#?

I've got code and i know I'm 99% of the way there. C# coding in MS VS2008.
Basically I have a form that has 4 radio buttons and a Continue button. the user clicks one of the radio buttons and clicks continue, and this all works fine.
However, I want to use the value entered by the user (i.e. if they click the first button, I want a variable equal to 1, 2nd button equals 2 and so on). I tried doing this in various points but the only place I can get it to run is in the private void btnOkClick line, which means I can use the values outside this void, which is what I really want.
I've tried playing around with setting some enums and such (commented out in the code below), but I can't quite get it. I know I must be close but my novice-ness is truly showing as I keep reading posts and can't quite grasp it.
In short, I want to be able to have other classes in my VS2008 project be able to reference whatever value the user selected in the initial form.
namespace AmortClient
{
public partial class frmLoadACTFCST : Form
{
public frmLoadACTFCST()
{
InitializeComponent();
//set the parent of the form to the container
//this.MdiParent = parent;
}
//public enum ACTFCST
//{
// ACT = 1,
// FCST = 2,
// PLAN = 3,
// FiveYearPlan2012=4
//}
//private ACTFCST _actfcst = ACTFCST.ACT;
//public ACTFCST actfcst
//{
// get { return _actfcst; }
// set { _actfcst = value; }
//}
private void frmLoadACTFCST_Load(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void btnActual_CheckedChanged(object sender, EventArgs e)
{
}
private void btnForecast_CheckedChanged(object sender, EventArgs e)
{
}
private void btnPlan_CheckedChanged(object sender, EventArgs e)
{
}
private void btn5YrPlan2012_CheckedChanged(object sender, EventArgs e)
{
}
private void btnContinue_Click(object sender, EventArgs e)
{
string ACTFCSTtext = "";
int dataTypeKey = 0;
if (btnActual.Checked)
{
ACTFCSTtext = btnActual.Text;
dataTypeKey = 1;
}
else if (btnForecast.Checked)
{
ACTFCSTtext = btnForecast.Text;
dataTypeKey = 2;
}
else if (btnPlan.Checked)
{
ACTFCSTtext = btnPlan.Text;
dataTypeKey = 3;
}
else if (btn5YrPlan2012.Checked)
{
ACTFCSTtext = btn5YrPlan2012.Text;
dataTypeKey = 4;
}
string msg = "";
msg = ACTFCSTtext + " " + dataTypeKey;
//btn5YrPlan2012
MessageBox.Show(msg);
Close();
}
}
}
Your dataTypeKey and ACTFCSTtext variables need to be declared as instance variables for your Form object if you want to access them from any other methods within your form. If you want to use them with some other form, you can pass them either as constructor arguments, or set some properties of said other form.
So you'd declare them just after the class declaration if you want them to be instance variables. They should still be private, meaning they can only be accessed from within your frmLoadACTFCST class.
public partial class frmLoadACTFCST : Form
{
private string ACTFCSTtext = "";
private int dataTypeKey = 0;
...
EDIT: if you want to access variables from one object in a different object (or static class), your options are as follows...
1) Declare your variables as public instance variables (same as shown above but public; these are known as Properties when you give them getter and setter methods). Your class that needs access to these variables would need to have a reference to the class that owns the variables.
Example:
FormA has a public property named SomeString.
FormB needs to access SomeString.
FormB needs a reference to FormA, and would access the variable as...
formAReference.SomeString
2) Pass the values of the variables as arguments to some method for the class that needs access.
Example:
FormA has a private instance variable named SomeString.
FormB needs access to SomeString.
If FormA instantiates FormB, it can pass the value of SomeString to FormB's constructor...
//From within FormA's code
FormB formB = new FormB(SomeString);
//FormB's constructor
public FormB(string someString)
{
this.someString = someString;
}
Maybe there is a smarter way to do it.
public partial class frmLoadACTFCST : Form
{
public frmLoadACTFCST()
{
InitializeComponent();
actfcst = ACTFCST.ACT;
btnActual.Tag = ACTFCST.ACT;
btnActual.Checked = true;
btnForecast.Tag = ACTFCST.FCST;
btnPlan.Tag = ACTFSCT.PLAN;
btn5YrPlan2012.Tag = ACTFCST.FiveYearPlan2012;
}
public enum ACTFCST
{
ACT = 1,
FCST = 2,
PLAN = 3,
FiveYearPlan2012=4
}
public static ACTFCST actfcst { get; private set; }
private void CheckedChanged(object sender, EventArgs e)
{
// All the buttons uses this Click-event.
actfcst = (sender as Button).Tag as ACTFCST;
}
private void btnContinue_Click(object sender, EventArgs e)
{
MessageBox.Show(actfcst.ToString());
Close();
}
}
The point is that all the buttons calls CheckedChanged when clicked.
Using a static means that others can access the value using something like this:
frmLoadACTFCST.ACTFCST value = frmLoadACTFCST.actfcst;
// Do something based on value.
I hope this helps you in yoyr quest.
If you select a control in design view, the properties window contains an item named "Modifiers". You can make the control public here.
A better way would be to create a new public property on your form that yields the value of the currently selected radio button.

Accessing Form's Controls from another class

I have a windows forms application with some controls added to the designer. When I want to change something (LIKE) enabling a text box from inside the Form1.cs, I simply use:
textBox1.Enabled = true;
but now I have a separated class called class1.cs.
How could I enable textBox1 from a static function class1.cs?
NOTE: I did not try any code because I am totally clueless about doing this.
EDIT: Lot of edit.
public partial class Form1 : Form
{
// Static form. Null if no form created yet.
private static Form1 form = null;
private delegate void EnableDelegate(bool enable);
public Form1()
{
InitializeComponent();
form = this;
}
// Static method, call the non-static version if the form exist.
public static void EnableStaticTextBox(bool enable)
{
if (form != null)
form.EnableTextBox(enable);
}
private void EnableTextBox(bool enable)
{
// If this returns true, it means it was called from an external thread.
if (InvokeRequired)
{
// Create a delegate of this method and let the form run it.
this.Invoke(new EnableDelegate(EnableTextBox), new object[] { enable });
return; // Important
}
// Set textBox
textBox1.Enabled = enable;
}
}
This is just another method:
TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
You shouldn't really change UI controls in your Form from your class1, but instead create a method or a property in class1 that would tell if the textbox should be enabled or not.
Example:
// I changed the name class1 to MySettings
public class MySettings
{
public bool ShouldTextBoxBeEnabled()
{
// Do some logic here.
return true;
}
// More generic
public static bool SetTextBoxState(TextBox textBox)
{
// Do some logic here.
textBox.Enabled = true;
}
// Or static property (method if you like)
public static StaticShouldTextBoxBeEnabled { get { return true; } }
}
Then in your form:
MySettings settings = new MySettings();
textBox1.Enabled = settings.ShouldTextBoxBeEnabled();
// Or static way
textBox1.Enabled = MySettings.StaticShouldTextBoxBeEnabled;
// Or this way you can send in all textboxes you want to do the logic on.
MySettings.SetTextBoxState(textBox1);
You can pass the instance of your Form to the class
MyForm frm = new MyForm();
MyClass c = new MyClass(frm);
Then your class can take that instance and access the textbox
public class MyClass
{
public MyClass(MyForm f)
{
f.TextBox1.Enabled = false;
}
}
The design does not look OK
It is better to call the class in your form and based on the value returned, manipulate the textbox
//MyForm Class
MyClass c = new MyClass();
c.DoSomethings();
if(c.getResult() == requiredValue)
textBox1.enabled = true;
else
textBox1.enabled = false;
//MyForm Class ends here
UPDATE
public class Class1
{
public static int SomeFunction()
{
int result = 1;
return result;
}
public static void SomeFunction(out int result)
{
result = 1;
}
}
Usage
if(Class1.SomeFunction() == 1)
textBox1.Enabled = true;
else
textBox1.Enabled = false;
OR
int result = 0;
Class1.SomeFunction(out result);
if(result == 1)
textBox1.Enabled = true;
else
textBox1.Enabled = false;
You could let your class1 have an event to enable the Textbox.
public class Class1
{
public event Action<object, EventArgs> subscribe ;
private void raiseEvent()
{
var handler = subscribe ;
if(handler!=null)
{
handler(this,EventArgs.Empty);//Raise the enable event.
}
}
}
Let the class containing the TextBox subscribe to it somehow. In TextBox wrapper class
public class TextBoxWrapper
public void EnablePropertyNotification(object sender, EventArgs args)
{
TextBox1.Enabled = true ; //Enables textbox when event is raised.
}
public TextBoxWrapper()
{
class1Instance.subscribe+=EnablePropertyNotification ;
}
To access/modify a Form Element property, just write this in your outside Class.
Form1.ActiveForm.Controls["textBox1"].Enabled = true;
Where textBox1 is the variable name of TextBox.
What this actually does: Gets the active Form object's control specified by the name in string.
WARNING: Active form means the form which is currently open and focused on. If you do something else on your computer, with your minimized WindowsForm application, the Form1.ActiveForm will not get the form, instead, it will give null, which can lead to errors later. Be careful!
based on the answer from #vr_driver you can do that to avoid problems with other containers like groupbox, panels...
TextBox t = Application.OpenForms["Form1"].Controls.Find("textBox1", true)[0] as TextBox;
In this example you have a form called Main.cs and a class called MyClass:
In MyClass (Note: the name of my Form Class = 'Main'):
Main ui = new Main();
ui.toolStripProgressBarStickers.PerformStep();
In (FormName).Designer.cs so in my case Main.designer.cs change the appropriate control from 'private' to 'public':
public System.Windows.Forms.ToolStripProgressBar toolStripProgressBarStickers;
This solved it for me.
Thanks, Ensai Tankado
I had to do this at work and didn't find that any of these answers matched what I ended up doing, so I'm showing how I made it work.
First, initialize a copy of your class in your load event.
NameOfClass newNameofClass;
Then you want to bind to your class (in the load event):
textBox1.DataBindings.Add(new Binding("Enabled", newNameofClass, "textBox1Enabled"));
In your class, do the following:
private bool textBox1Enabled = false;
public bool TextBox1Enabled
{
get
{
return textBox1Enabled;
}
set
{
textBox1Enabled = value;
}
}
The false setting will initialize your textbox to being disabled
Set textBox1Enabled to true if you want to enable by default.
If you have other logic to enable/disable the textbox, simply modify the value of textBox1Enabled accordingly.
Very easy:
Create an Instance of your Form Object where want to access the Elements from.
Form1 ui = new Form1();
and now change the Form Elements to "public" - like this in the Designer Code:
...
public System.Windows.Forms.TextBox textBox6;
...
Now you can access them like this in your Code:
ui.textBox6 ...
I had the same problem. I used windows forms & Visual Studio to generate a UI in a utility with textbox, checkbox, and button controls but ALL the code was in the same class.
I'm rewriting the utility now that I know "more" OOP concepts and created actual objects and separate classes. I too had problems getting the separate classes to be able to access the form controls and any shared methods that are in the form class. I tried the various suggestions in this thread as well as other threads but none of those solutions worked for me.
What worked for me (not sure if its the right thing to do or not) was I had each class that needed to access the controls and forms methods inherit from the Form.
Here is the relevant part of the Form.cs file:
namespace Utility
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void WriteNote(string noteText, bool asHeading = false)
{
//Writes Messages to the Message box in the UI
Font font1 = new Font(this.ResultsTB.Font, FontStyle.Bold);
Font font2 = new Font(this.ResultsTB.Font, FontStyle.Regular);
if (asHeading)
this.ResultsTB.Font = font1;
else
this.ResultsTB.Font = font2;
this.ResultsTB.AppendText(noteText + "\r\n");
}
My Form contains a textbox called DirTB and a method called "WriteNote" that writes info to another textbox called ResultsTB. Here is the class (at least as far down as the first successful call of the WriteNote method from the Form:
namespace Utility
{
public class AppServerDTO : Form1
{
#region App Server attributes
//attributes listed here
#endregion App Server attributes
#region AppServerDTO Constructor
public AppServerDTO()
{
//These methods verify and set all the attributes
VerifyInstallFolder();
}//end of constructor AppServer
#endregion AppServerDTO Constructor
#region AppServerDTO class methods
public void VerifyInstallFolder()
{
string keypath = string.Empty;
string locationVerification = DirTB.Text + #"\SomeText";
for (int i = 0; i < 4; i++) //allows 3 attempts to get the install folder right
{
if (Directory.Exists(locationVerification))
{
i = 4;//Kills the loop
}
else if (!Directory.Exists(locationVerification))
{
locationVerification = DirTB.Text + #"\SomeMoreText";
}
else if (!Directory.Exists(locationVerification))
{
WriteNote("The directory listed in the Install Directoy box is not reachable.");
WriteNote("Please select the correct directory.");
WriteNote("The correct directory is the folder that contains the ApplicationUpdates & UpdateManager folders.");
WriteNote(#"i.e. C:\Somewhere or D:\Someplace\Somewhere");
var folderpath = FolderPrompt(#"C:\");
DirTB.Text = folderpath; //updates the install folder textbox to the new location
keypath = folderpath;
i++;
}
}//end for loop
}//end VerifyInstallFolder
As long as you are very careful with what you mark as public vs private, it should be ok.
This is how you should do :
I wrote the code below in my form class :
public static Form1 form = null;
private delegate void SetImageDelegate(Image image);
public Form1()
{
InitializeComponent();
form = this;
}
public static void SetStaticImage(Image image)
{
if (form != null)
form.pic1.Image = image;
}
private void setImage(Image img)
{
// If this returns true, it means it was called from an external thread.
if (InvokeRequired)
{
// Create a delegate of this method and let the form run it.
this.Invoke(new SetImageDelegate(setImage), new object[] { img });
return; // Important
}
// Set textBox
pic1.Image = img;
}
and the code below should be in anouther class :
Form1 frm= Form1.form;
frm.pic1.Image = image;
Note that i changed private static Form1 form = null; to public static Form1 form = null;
Good Luck ... Written by Hassan Eskandari :)

Categories