I`m a Win Forms newbie. How can I pass Control Object Values [Lets say Text Box Values] on a Windows forms to a Class File, and return values from the Class File to the main form.
This is what I`m doing - I have a main form, with a lot of text boxes, drop downs..etc and I want to pass values of them to a Class File [not another windows form] and do all my calculations using the methods there and return values back to the main form.
Different ways of doing this?
I have done this on web applications using query strings...and I just can`t figure out a way of doing this on windows forms...this is driving me mad.
Help will be greatly appreciated.
Thank You
San
Loading
Posted Dec 27, 2007, 2:22 PM
When you create a linked library, you add that library to the project and reference in the main project the namespace contained in the linked library that you wish to use. The real beauty of linked libraries that I have found is updating and ease of swapping out code.
Say you have an application, and this application is rock solid. However, later on the down the road you may want to make some changes to an important block of code or segment of the assembly. You can put the code for that assembly into a linked library, give it its own namespace, and reference that namespace from the main body of the code, and it will work just as if that code is an integral part of the main assembly.
The real beauty comes with changes. Say you want or need to make a change to the code contained in that linked library, all you need to do is open the linked library solution, make the changes, rebuild the .dll file, and then replace the old .dll file with the new one and there you go, instant code changes without having to reinstall or (usually) reconfigure the project!
sanPosted Dec 27, 2007, 12:26 PM
Posted Dec 26, 2007, 7:05 PM
using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
namespace MyProgram
{
public class MyForm : Form
{
private Calculation calculation = new Calcuation();
public MyForm()
{
InitializeComponent();
}
private void CalcButton_Click(object sender, EventArgs e)
{
ResultTextBox.Text =
AddNums(NumOneTextBox.Text, NumTwoTextBox.Text);
}
}
}
//--------------
//-NO MORE CODE-
//--------------
//Assume this is in a different source code file.
using System;
using System.Text;
//Notice its in the same namespace.
namespace MyProgram
{
public class Calcuation
{
public Calculation()
{
//TODO: Add constructor logic.
}
public double AddNums(string numOne, string numTwo)
{
return (Convert.ToDouble(numOne) + Convert.ToDouble(numTwo));
}
}
}
//--------------
//-NO MORE CODE-
//--------------
//This example has code that is not in the same namespace.
using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
//You must include the namespace of the
//code you wish to use.
using Calculations;
namespace MyProgram
{
public class MyForm : Form
{
private Calculation calculations = new Calcuation();
public MyForm()
{
InitializeComponent();
}
private void CalcButton_Click(object sender, EventArgs e)
{
ResultTextBox.Text = AddNums(NumOneTextBox.Text, NumTwoTextBox.Text);
}
}
}
//--------------
//-NO MORE CODE-
//--------------
//Assume this is in a different source code file.
using System;
using System.Text;
//Notice the different namespace.
namespace Calculations
{
public class Calcuation
{
public Calculation()
{
//TODO: Add constructor logic.
}
public double AddNums(string numOne, string numTwo)
{
return (Convert.ToDouble(numOne) + Convert.ToDouble(numTwo));
}
}
}
//--------------
//-NO MORE CODE-
//--------------
You will have to add a reference to the namespace if the namespace differs from the main one with the variables. But other than that, this example should work. Hope it helps!