|
Class
class Class1
{
public void ChangeTextctl ( string text , System.Windows.Forms.Control ctl )
{
ctl.Text = text;
}
}
Form
Class1 cls = new Class1();
cls.ChangeTextctl("Test", button1);
Non related to the Above
P.S : if you want to change the control text from another form
then set the control modifiers as Internal . and call it from the instant
I know nothing , I know nothing ...
|
|
|
|
|
I think he wants to do it in the other direction. To do it in the direction you suggest, makes no sense at all.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
Just a quick question:
When creating a new instance of the class by calling the constructor, wouldn't you re-initialize all of the controls again? InitializeComponent();
What if you don't want that to happen?
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
is the Calc class instanciated by a winForm class? If so, raise an event in your Calc instance (with a custom event args instance to pass the text), and subscribe to it in the winForm instance, and update the Label from the handler.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi Dave,
Thanks for the response.
Is there any way you could give an code example, because that is more or less what is happening.
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
This is a quickie example that should help to get you started. For more details on events, search this site, google... and have a look at my article Events Made Simple[^].
public partial class WinForm : Form
{
private Calc calc;
public WinForm()
{
InitializeComponent();
calc = new Calc();
calc.CalcResult += new EventHandler<CalcResultEventArgs>(calc_CalcResult);
calc.Add(3.14159265f);
}
void calc_CalcResult(object sender, CalcResultEventArgs e)
{
label1.Text = e.ResultAsString;
}
}
public class Calc
{
public event EventHandler<CalcResultEventArgs> CalcResult;
public float Value
{
get;
private set;
}
public void Add(float number)
{
Value += number;
OnCalcResult(new CalcResultEventArgs(Value.ToString()));
}
protected virtual void OnCalcResult(CalcResultEventArgs e)
{
EventHandler<CalcResultEventArgs> eh = CalcResult;
if (eh != null)
eh(this, e);
}
}
public class CalcResultEventArgs : EventArgs
{
public CalcResultEventArgs(string resultAsString)
{
ResultAsString = resultAsString;
}
public string ResultAsString
{
get;
private set;
}
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Excellent!! Thank you very much
Passing Data between Windows Forms[^] was very useful as well.
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
Just a quick point on this, setting a label on a form from a seperate class is probably not a particularly good idea. By doing that you've tightly coupled the class and form together which is bad for reuse. At the extreme you might as well put the calculation stuff in the form itself, as potentially the calc class can no longer operate without the form.
As Christian suggests, by exposing an event on your calculation class your form can get updated, but your class could be used elsewhere without that form.
Regards,
Rob Philpott.
|
|
|
|
|
Thanks for your comment Rob.
I was only trying to simplify my scenario as much as I can, the real application does nothing of this sort. I was only trying to explain in layman's terms.
Kind regards,
The only programmers that are better C# programmers, are those who look like this -> |
Programm3r
My Blog: ^_^
|
|
|
|
|
Hello,
Can anyone help me to solve below problem at my pc.
Method not found:'Int32 Microsoft.VisualStudio.Shell.Interop.IVsRunningDocumentTable2.QueryCloseRunningDocument(System.String,Int32 ByRef)'.
Thanks.
Jem_Jumper.
|
|
|
|
|
1. Have you added Microsoft.VisualStudio.Shell.Interop.8.0.dll as reference?
2. Second parameter is out parameter.
जय हिंद
|
|
|
|
|
Hi
can any one tell me how to install an application developed using .NET in Linux System
Thanks in advance
|
|
|
|
|
|
.net is not directly supported on Linux by Microsoft.
Take a look at the Mono project[^]. They are attempting to implement the CLR and large parts of the framework on Linux. Currently they have a lot of good support for .net 2.0, but some of the 3.5 stuff is still catching up. It depends on what your app uses.
Also see - http://en.wikipedia.org/wiki/Mono_(software)[^]
Simon
|
|
|
|
|
We have a software developed using DotNet 2.0. The problem we are facing is there are certain areas where customers require changes according to their business needs. These change in turn makes us to change the code and cater their requirements. Lets take an example of employee entrance at their office:
Client 1:
Activity 1. Employees swipe their card and enter.
Activity 2. Have tea/coffee.
Activity 3. Start work.
Client 2:
Activity 1. Employee enter their names and time-in manually.
Activity 2. Start Work.
Client 3:
Activity 1. Employees do not perform any check-in.
Activity 2. Have Breakfast.
Activity 3. Start Work.
My question is that if there is any approach where we can handle these changing requirements. The activities involved in these requirements like the example above do change for every customer. These activities can either increase/decrease in number or change entirely.
Thanks.
|
|
|
|
|
|
I'm somewhat hesitating about using nested try/catch blocks.
The situation which makes me think about is the following situation. Some sample code to illustrate:
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlTransaction trans = conn.BeginTransaction();
try
{
string query = "query goes here";
SqlCommand command = new SqlCommand(query, conn, trans);
command.ExecuteNonQuery();
trans.Commit();
}
catch
{
trans.Rollback();
}
finally
{
if(conn != null)
{
conn.Close();
conn.Dispose();
}
}
This block has 1 try/catch block but the problem resides in the "conn.Open();" line. When there is a network error the conn.Open() will throw an exception. Putting the conn.Open() in the try/catch block will invalidate the code which now resides in the catch/finally clauses.
So I can use 2 sequential try/catch blocks or a nexted construction. What do you people think?
The consumer isn't a moron; she is your wife.
|
|
|
|
|
It won't invalidate anything. You can just put an (if conn != null) type block at the start of the second try/catch, and then your finally block will work fine. Indeed, command.ExecuteNonQuery would just force the catch and finally, if conn was null. I'm also not sure why conn couldn't be in the existing try block, just declare trans outside it, and begin it, inside.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
Ah yes, you're right. That would make it a viable solution indeed.
*reminds himself not to start coding too early in the morning*
The consumer isn't a moron; she is your wife.
|
|
|
|
|
Helfdane wrote: reminds himself not to start coding too early in the morning
The best time is after but before
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
That would mean my evening diner as I'm sitting next to the coffee machine
The consumer isn't a moron; she is your wife.
|
|
|
|
|
Christian Graus wrote: I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
Now I've seen everything!
Cheers,
Vikram. Recent activities:
TV series: Friends, season 8
Books: Freakonomics, by Steven Levitt and Stephen J Dubner. Carpe Diem.
|
|
|
|
|
Sequencial try/catches would be better. Something like:
SqlConnection conn;
SqlTransaction trans;
try {
conn = new SqlConnection(connectionString);
conn.Open();
SqlTransaction trans = conn.BeginTransaction();
}
catch (SomeTypeOfException ex) {
}
finally {
if (conn != null) {
conn.Close();
conn.Dispose();
}
}
if (trans != null) {
try {
string query = "query goes here";
SqlCommand command = new SqlCommand(query, conn, trans);
command.ExecuteNonQuery();
trans.Commit();
}
catch (SomeOtherException ex) {
trans.Rollback();
}
finally {
if (conn != null) {
conn.Close();
conn.Dispose();
}
}
}
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
For the situation above, Christian Graus gave a good hint on a potential solution, and so do you. But in general, why is sequential better than nested?
The consumer isn't a moron; she is your wife.
|
|
|
|
|
You put a try catch around the possible/probable points of failure and catch SPECIFIC errors. Catch and ignore is, generally, not a good idea. Ignoring a specific case can be okay so long as you impliment a valid recovery.
ps. as pointed out the first finally is cr@p.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|