|
This class shouldn't know anything about, nor care about, any Label control on any form. What it should be doing is using a delegate that the parent form can use to get notifications from this class and the form can decide whether or not to update its own label control.
|
|
|
|
|
Thanks, unfortunatley if I understood how to accomplish what you suggested I wouldn't of had to ask the question. I've tried vairous methods, including using a delegate. However I'm still stuck. Examples would much appriciated, thanks so much for your time.
|
|
|
|
|
Here's a quick example of how it could be aproached using an event. An event is a delegate in disguise!
using System;
using System.Windows.Forms;
namespace vdp
{
public partial class FormMain : Form
{
private Poker poker;
private int credits;
public FormMain()
{
InitializeComponent();
poker = new Poker();
labelWinnings.Text = "0";
poker.UpdateWinnings += new EventHandler<WinningsEventArgs>(poker_UpdateWinnings);
DoubleClick += new EventHandler(FormMain_DoubleClick);
}
void FormMain_DoubleClick(object sender, EventArgs e)
{
poker.PlayGame();
}
void poker_UpdateWinnings(object sender, WinningsEventArgs e)
{
AddToCredits(e.Winnings);
}
public void AddToCredits(int winnings)
{
credits += winnings;
labelWinnings.Text = credits.ToString();
}
}
public class Poker
{
public event EventHandler<WinningsEventArgs> UpdateWinnings;
public void PlayGame()
{
OnUpdateWinnings(new WinningsEventArgs(1000));
}
protected virtual void OnUpdateWinnings(WinningsEventArgs e)
{
EventHandler<WinningsEventArgs> eh = UpdateWinnings;
if (eh != null)
eh(this, e);
}
}
public class WinningsEventArgs : EventArgs
{
public WinningsEventArgs(int winnings)
{
Winnings = winnings;
}
public int Winnings
{
get;
private set;
}
}
}
|
|
|
|
|
Thanks, I typed it verbatium into my exisitng code, like before, it compiles and runs fine. But alas the label still doesn't update
|
|
|
|
|
Then you have something wrong with your event raising, subscription or handling. You need to learn/understand how these work to use them successfully.
Here is a brief code snippet with comments which should give you an overview. If this isn't enough, google or search here on CodeProject... there are lots of articles/blogs on it. I even have an article on here covering this subject in some depth - Events Made Simple[^]
public class TestClass
{
private ClassWithEvent classWithEvent;
public TestClass()
{
classWithEvent = new ClassWithEvent();
classWithEvent.Event += new EventHandler(classWithEvent_Event);
}
void classWithEvent_Event(object sender, EventArgs e)
{
Console.WriteLine("Event Rased!");
}
}
public class ClassWithEvent
{
public event EventHandler Event;
protected virtual void OnEvent(EventArgs e)
{
EventHandler eh = Event;
if (eh != null)
eh(this, e);
}
public void PerformEvent()
{
OnEvent(EventArgs.Empty);
}
}
|
|
|
|
|
ok, I got that part to work..I had the following line of code: Poker poker = new Poker(); before I called a method in my poker class from my frmMain class that had to be removed
|
|
|
|
|
Hello,
I was wondering if there is a way to perform a simple left mouse click anywhere on the screen?
Thanks.
|
|
|
|
|
The last time I tried this (about 4 years ago) it was complicated, assuming you want to move the mouse outside the bounds of the executing app. I had to hook the Windows Queue and intercept / process the mouse messages.
There is an article for something similar here
http://blogs.msdn.com/toub/archive/2006/05/03/589468.aspx[^]
I'm not sure this will work on OSs newer than XP, or even if this is the best way to go about it. Hopefully one of the opther CPians will have a better suggestion
CCC solved so far: 2 (including a Hard One!)
|
|
|
|
|
Hi,
yes you can click anywhere using P/Invoke and calling
[DllImport("user32.dll", CallingConvention=CallingConvention.StdCall, SetLastError=true)]
public static extern IntPtr SendInput(int count, ref INPUT input, int size);
I've built a little class that moves and clicks any way I want.
Of course clicking at (x,y) in screen coordinates assumes the thing you want to click really is there (the user or some code may move objects around) and is not covered by something else.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I create class library, its name "SharedObjects". This library contain one class, its name "Sample". Class "Sample" contain following codes;
private static readonly Sample m_Instance = new Sample();
private Sample()
{
}
public static Sample Instance
{
get { return m_Instance; }
}
public string Name { get; set; }
Sample class is singleton, and have one property which is "Name". I add two Windows Forms Application to my solution which are "Win1" and "Win2".
In Win1 contain following code;
Sample.Instance.Name = "ABC";
MessageBox.Show(Sample.Instance.Name);
In Win2 contain following code;
MessageBox.Show(Sample.Instance.Name);
My problem is, I run Win1 code its properly work but after that I run Win2 do not show "ABC", its show empty string.
I think my problem is related about application domain, but I can't solve my problem. I wrote CrossAppDomainSingleton class but I can't solve my problem.
Please help me...
|
|
|
|
|
Yes you have found expected behavior. The two applications are in separate AppDomains with separate memory spaces. Typically when sharing information between apps you would set up an intermediary source, like a database or a message queue. You could also create a WCF or Remoting service to share the data between apps
only two letters away from being an asset
|
|
|
|
|
Aoooo, actually my project already use WCF. Real senario like this, SharedObjects is inherit from generic list and this dll is shared dll, its mean using server-side and client-side. Run thread on application server when WCF service hosting and use "SharedObjects" dll.
Client use this dll, two times in one operation. First is using in client-side, second is server side via WCF. So add data to SharedObjects list. Its properly work. But another client want to use SharedObjects list in server side via WCF, SharedObjects list count is zero, main problem is this.
|
|
|
|
|
I don't understand your whole server side and client side breakdown. All you need is the WCF service, once instantiated the client has a proxy, an instance of the class, no need for a client copy also. What are you using with the WCF service?
only two letters away from being an asset
|
|
|
|
|
Sorry, I fix my question and ask again in the form.
I wrote class library (dll) and this library contains one singleton class. Like this;
public class MyList : List<string>
{
private static readonly MyList m_Instance = new MyList();
private MyList()
{
}
public static MyList Instance
{
get { return m_Instance; }
}
}
Client --- WCF --> Server; add item to MyList class, its properly work.
Another Client --- WCF --> Server; add item to MyList class, its properly work.
But;
I have a thread in server side. If this thread add to item to MyList, clients items not contain in the MyList. Actually main problem is I can't see MyList items which are aded by clients in server thread.
I think this problem related about AppDomain. How can I solve it?
Best Regards...
|
|
|
|
|
You still haven't answered the question. How have you constructed the WCF service? What InstanceMode are you using? What persistance mechanism are you using? How much do you really understand about WCF and using it?
only two letters away from being an asset
|
|
|
|
|
My WCF Service use TcpBinding and host on windows service. I am not use ServiceBehavior InstanceContextMode... Problem not related about WCF.
I wrote class which is MyClass1, this class in server side. This class have a Start() function.
public void Start()
{
Thread thread = new Thread(new ThreadStart(CheckTablesAreChanged));
thread.IsBackground = true;
thread.Start();
}
private void CheckTablesAreChanged()
{
while (true)
{
... ADD ITEM TO MyLIST CLASS ...
}
}
Start() function call when WCF is hoting like this;
host.Open();
MyClass1.Start();
So this side not related about WCF.
In Client side;
Client --> WCF --> Server --> ADD ITEM TO MyLIST CLASS
For example client add 3 ITEM TO MyLIST CLASS, after that I check MyLIST CLASS count property in "CheckTablesAreChanged()" function, MyLIST CLASS count is 0.
|
|
|
|
|
As Pete said in the prevous posts, each AppDomain is in its own memory space and is therefore isolated, marshalling objects between domains is not a facile task. In any case I think the architecture you describe is unsound:
- It breaks the principles of client / server architecture. If you want to access a common object you should only do this on the server via method calls. There are several ways of persisting the common object between calls / clients
- By attempting to call methods/set properties on an object instanced on the server from the client, you are opening up a security hole
CCC solved so far: 2 (including a Hard One!)
|
|
|
|
|
|
What is the easiest way to activate a specific excel worksheet within a workbook. I am using MS visual C#
|
|
|
|
|
Youre description is a bit vauge, but I'm guessing it would be something like Worksheets("someSheetName").Active ??
|
|
|
|
|
-David
I tried this method and it says
Error 2
The name 'Worksheets' does not exist in the current context
|
|
|
|
|
How about supplying the name you're using for the Excel Application object?
myExcelAppObject.Worksheets...
|
|
|
|
|
Actually.... Im not clear on excel worksheet within a workbook? Could you clarify on this?
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
Example you can have one Excel workbook with multiple tabs I am trying to figure out how to make the 3rd tab the active tab to manipulate the data.
|
|
|
|
|
antsims wrote: What is the easiest way to activate a specific excel worksheet within a workbook.
The easiest way is to search the MSDN documentation & samples[^].
|
|
|
|