|
Thanks for responding to my post. Unfortunately I can not post any code. I’ll try to be clearer about my question this time. I have a composite control, combination of standard control (say text box). Every time any onClick happens on this child(text box) control, I want to pass this event to parent(composite) control.
-Kasturi
|
|
|
|
|
There are a million articles online that deal with using delegates and events and these are the two topics you need to understand so you know what is going on. However, I found that most rticles are prtty poor when it comes to these topics. The article that I used to gain a good understnding i the following.
http://msdn.microsoft.com/msdnmag/issues/01/08/net/net0108.asp
I modified this code to be more generic, i.e. I have a class that works as a generic event "attacher" and "detacher". It's not that pretty though.
I don't have the code with me right now, so you'd have to wait for me to whip it up again if you have the patience. Otherwise you an read the article above and just dig in. It's well written nd ver clear at least to me. I'll still whip up that sample of mine though.
Regards
Senkwe
Just another wannabe code junky
|
|
|
|
|
Here's a sample EventsManger class. All it does is check to see if there are any listeners for registered events. As you can see it's very simple.
public class EventsManager
{
public delegate void EventHandler(Object sender);
public event EventHandler EventsHolder;
protected virtual void OnEventOccurred()
{
if(EventsHolder != null)
EventsHolder(this);
}
public void EventWasFired()
{
OnEventOccurred();
}
}
I prefer to have my entry point in a class called driver (I'm used to C++) so I declare the following...
public class driver
{
public static EventsManager em;
static void Main()
{
em = new EventsManager();
Application.Run(new Form1());
}
}
To test it, I created a user control with a button on it. i.e. a composite control. Now, I want that when I press the button, the user control back color turns red, ie, the button_click event gets bubbled up to the parent control so to speak. To make things easy, you can make your user control take an EventsManager as a constructor paramater.Then register a listener like so...
public UserControl1(EventsManager em)
{
InitializeComponent();
em.EventsHolder += new EventsManager.EventHandler(ButtonWasPressed);
}
private void ButtonWasPressed(Object sender)
{
this.BackColor = Color.Red;
}
You can then fire the event from the button's OnClick event like so
private void button1_Click(object sender, System.EventArgs e)
{
driver.em.EventWasFired();
}
You'll notice that I don't use EventArgs in my delegate declaration. I left that out because it's of no real use here. But it's advisable fro you to do this. (I typically derive a class from EventArgs and stuff it with mty own event specific code)
Well it's ugly but it works for me, hope it helps
Just another wannabe code junky
|
|
|
|
|
Does anybody know what the correct behaviour the proxy settings when using a HttpWebRequest/HttpWebResponse pairing should be?
Looking on the web I can see many articles (well a few) showing how to instruct a request to go through a proxy. The problem is, to me it already looks that way.... my programs, without abything extra, use the IE proxy settings.
I can change the proxy quite easily, but to bypass the proxies, the best I can come up with is something like this:
bool proxyDetected =
((WebProxy)GlobalProxySelection.Select).Address != null;
if ( proxyDetected && bypassProxy )
GlobalProxySelection.Select = GlobalProxySelection.GetEmptyWebProxy();
I'm confused! Everybody else seems to want to know how to use a proxy, I'm trying to find another example of someone trying to bypass a proxy! Does the above code look right?
Regards,
Ray
|
|
|
|
|
Hi folks,
what would be the best way to implement a page header (eg The Code Project's) on each page of a C# WebForm application?
Matt
------
Accept that some days you are the pigeon and some days the statue.
|
|
|
|
|
Guys,
I am having problems with the use of the CacheItemRemovedCallback event within an ASP.NET/C# website I am building. The below code is from my global.aspx file. The code loads XML/XSL objects into the Cache for later use. I also create a Cache dependency so should any files change which my XML/XSL objects use, the Cache is updated automatically. The code works only when something is removed from the Cache by another aspx file, since the code correctly identifies the HttpContext. However, when one of my files is changed and the RemovedCallback() function is fired, for some reason the Context and the HttpContext are both null. As such my code never has a chance to update the Cache again. Rather it exists "without an error" as soon as I try to use Cache.Insert. Does anyone know if this is a bug in Beta 2, or am I missing something, like the use of a special Context object?
Any help greatly appreciated..........
Cheers,
Tim.
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<%@ Import Namespace="System.Web.Caching" %>
<%@ Import Namespace="CommerceEngine.Common" %>
private static CacheItemRemovedCallback onRemove = null;
private XPathDocument CommerceListXmlObj;
private XslTransform CommerceListXslObj;
public void Application_OnStart()
{
ApplicationConfiguration.OnApplicationStart(Context.Server.MapPath( Context.Request.ApplicationPath ));
onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
CommerceListXmlObj = new XPathDocument(CommerceEngineConfiguration.CommerceListXmlFilePath);
HttpContext.Current.Cache.Insert("CommerceListXmlObj", CommerceListXmlObj, new CacheDependency (CommerceEngineConfiguration.CommerceListXmlFilePath), DateTime.Now.AddHours(1), TimeSpan.Zero, CacheItemPriority.Default, CacheItemPriorityDecay.Slow, onRemove );
CommerceListXslObj = new XslTransform();
CommerceListXslObj.Load(CommerceEngineConfiguration.CommerceListXslFilePath);
HttpContext.Current.Cache.Insert("CommerceListXslObj", CommerceListXslObj, new CacheDependency(CommerceEngineConfiguration.CommerceListXslFilePath), DateTime.Now.AddHours(1), TimeSpan.Zero, CacheItemPriority.Default, CacheItemPriorityDecay.Slow, onRemove );
}
public void RemovedCallback(String k, Object v, CacheItemRemovedReason r)
{
switch(k)
{
case "CommerceListXmlObj" :
CommerceListXmlObj = new XPathDocument(CommerceEngineConfiguration.CommerceListXmlFilePath);
HttpContext.Current.Cache.Insert("CommerceListXmlObj", CommerceListXmlObj, new CacheDependency(CommerceEngineConfiguration.CommerceListXmlFilePath), DateTime.Now.AddHours(1), TimeSpan.Zero, CacheItemPriority.Default, CacheItemPriorityDecay.Slow, onRemove );
break;
}
}
|
|
|
|
|
I know this is beginner-level, but how can I write a console app with Visual Studio, run the app and have the output redirected to the Visual Studio output window? If I can't do that, is there another means of pausing the application without calling a getchar() type function?
bye bye,
KLC
|
|
|
|
|
You can use the trace functions to get your text to the output window.
System.Diagnostics.Trace
I don't think there is one-line method of changing all System.Console.WriteLine's to use the output window.
But you could write a class that implements/extends System.IO.TextWriter (using the Trace class as your backend) then use System.Console.SetOut, passing in an instance of your new class.
HTH,
James
Sonork ID: 100.11138 - Hasaki
|
|
|
|
|
Thanks James, but both methods requires a lot of changing of code that doesn't need to be changed. I was hoping for at least a way in Visual Studio to route all Writelines to the output window. Oh well.
Thanks anyway.
bye bye,
KLC
|
|
|
|
|
I didn't look into it too much when I replied (was busy putting the finishing touches on an article).
Here's a class that'll do what you want
using System;
using System.Diagnostics;
namespace ConsoleRerouter
{
public class OutputToTrace : System.IO.TextWriter
{
public OutputToTrace()
{
}
public override void Write(char c)
{
Trace.Write(c, "Console");
}
public override void WriteLine(string line)
{
Trace.WriteLine(line, "Console");
}
private System.Text.Encoding enc = new System.Text.ASCIIEncoding();
public override System.Text.Encoding Encoding
{
get
{
return enc;
}
}
}
}
If you do anything really vigorous you'll have to add more calls to Trace.Write/WriteLine, but it'll do the deed as it is right now.
Use it like
System.Console.SetOut(new ConsoleRerouter.OutputToTrace());
It worked great in my simple test (output everything that was typed in)
Enjoy,
James
Sonork ID: 100.11138 - Hasaki
|
|
|
|
|
Run the program by pressing CTRL+F5. The command-line window will request you to press a key before terminating.
|
|
|
|
|
Hi guys...here's my problem. I have created a viual control called a TilePlaceControl. I have added many of these to a panel control on a form.
Now, I can access any control containedby the panel in this way...
myPanel.Controls[x] where x is the 0 based index of the control previously added. The problem is, that snippet of code as it stands returns (and rightly so) a generic Sytem.Forms.Control object.
My problem is that I need to change an aspect of the TilePlaceControl actually contained within the panel...for example, I need to do something like this...
myPanel.Controls[x].MyMethod(...)
So then, why can I NOT do this?
(TilePlaceControl)myPanel.Controls[x].MyMethod(...)
The compiler complains saying that MyMethod is not a member of System.Forms.Control.
I wouldn't mind so much if I could simply do the following...
TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x];
tpc.MyMethod(...)
myPanel.Controls[x] = tpc;
But I can't because Controls[x] is a READ ONLY indexer.
Please guys give me a way around this, the other alternative is just way too ugly to even contemplate
Regards,
Senkwe
Just another wannabe code junky
|
|
|
|
|
Senkwe Chanda wrote:
(TilePlaceControl)myPanel.Controls[x].MyMethod(...)
Almost have it!
((TilePlaceControl)myPanel.Controls[x]).MyMethod(...)
That will give you what you want
[Note the extra set of parenthesis]
Senkwe Chanda wrote:
TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x];
tpc.MyMethod(...)
myPanel.Controls[x] = tpc;
So close, yet again You shouldn't have to assign back into the collection unless you actually want to change the instance in the collection in which case you should do a Controls.Remove and Controls.Add.
The code below should work, and give you the results you expect, as will the code at the top.
TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x];<br />
tpc.MyMethod(...)
James
Sonork ID: 100.11138 - Hasaki
|
|
|
|
|
Thanks alot James, your first code snippet works great and is even clear now that I see it in black and white.
As for your last piece of code
TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x];
tpc.MyMethod(...)
I thought of that but then thought that
TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x];
creates a seperate instance of the object and hence, tpc.MyMethod(...) would still not be called on the instance contained by the panel.
But I'll explore that again.
Thanks alot!!
Regards
Senkwe
Just another wannabe code junky
|
|
|
|
|
Senkwe Chanda wrote:
I thought of that but then thought that
TilePlaceControl tpc = (TilePlaceControl)myPanel.Controls[x];
creates a seperate instance of the object and hence, tpc.MyMethod(...) would still not be called on the instance contained by the panel.
Nope, tpc is only a reference to the object. A reference is similar to a pointer in C/C++ and ByRef in VB; anything you do to the reference (except assignment) will carry through to the actual instance.
Assignment on a reference only makes the reference refer/point to something else, leaving the original intact.
The only way to create a new instance is via the new keyword or through reflection's Activator.CreateInstance, neither of which you show here
James
Sonork ID: 100.11138 - Hasaki
|
|
|
|
|
What is the equivalent of VB's "InStr" function in C#?
|
|
|
|
|
use the IndexOf method on your string object.
int instr = myString.IndexOf("moo", 0);
There are 5 other overloads that you can use as well, 3 to find a string, 3 to find a char.
James
Sonork ID: 100.11138 - Hasaki
|
|
|
|
|
Hello,
I need some information on where to find and obtain a drives attributes, e.g, ROM_DRIVE or FLOPPY_DRIVE etc and other attributes pertaining to the drive. I've searched the MSDN library and other places to no avail. I've tried using Environment but can't get anything. Any insight would greatly be appreciated.
Thanks
|
|
|
|
|
Any sample on using CreateWindow API from c# ? I’m aware of “MessageBox” sample on MSDN but I’ve no idea on how to retrieve HWND of Form and Instance handle. I’m trying to put together code to use virtual listview. virtual listview is available in SDK not with .Net. Any idea would be truly appreciated.
Thanks
|
|
|
|
|
kasturirawat wrote:
...I’ve no idea on how to retrieve HWND of Form and Instance handle.
For the HWND use the Forms Handle property. The HINSTANCE is a bit harder to get, but still possible.
untested code, but from the docs this should work; though the GetHINSTANCE method confuses me a bit as i'll explain later.
<br />
IntPtr hInstance = System.Runtime.InteropServices.Marshal.GetHINSTANCE(<br />
typeof(ClassThatHasYourHInstance).Module <br />
);<br />
Here's the confusing part about this method
Parameters
m
The module whose HInstance is desired.
Return Value
The HInstance for m;-1 if the module does not have an HInstance.
Remarks
Whether dynamic or in-memory, modules do not have an HInstance.
It looks like this method will work until you read the remarks. We pass in the module whose hinstance we wish to retreive, but then the remarks tells us that modules don't have one. Thus we will always get -1.
I seem to vaguely remember there being some win32 function that would give you the hinstance if you had the hwnd. Since you have the hwnd you could call this function and get the hinstance (call via p/invoke).
HTH,
James
Sonork ID: 100.11138 - Hasaki
|
|
|
|
|
I’ve created sample for virtual listview using SDK. Since dotnet does not support virtual list view, is there any way I can use my sdK sample with dotnet app. Using PINVOKE is very time consuming
|
|
|
|
|
I believe you could wrap your code with MC++ providing an interface for your dotnet app to work with it. Thats about the extent of my knowledge on MC++ though. :p
Perhaps someone with a better grasp could fill you in more?
James
Sonork ID: 100.11138 - Hasaki
|
|
|
|
|
Do Anyone know how to use TAPI in C# or any component in .Net that enable Voice over IP application???????
|
|
|
|
|
Hi,
Can any one tell me how I can Automate MS Office 97/2000/XP using the same C# Code. Using VB I could use Late Binding to do this, but I can not figure out how to do it using C#. I wish to have one C# Program which will work on all versions of MS Office.
Thanks for your time
Peter Tewkesbury
Peter Tewkesbury
|
|
|
|
|
Just off the top of my head, so i have no idea how much work this would be...
Abstract the functionality you need from office into an interface or abstract class. Then write a class for each version of office. At runtime detect which version of office is installed and use the proper class.
You might have to break each class into a separate assembly so it won't try loading office 97 typelib info when you want xp info. My knowledge of how the references get loaded is minimal though, so this may be an extraneous step.
Hope this at least gives you some ideas,
James
Sonork ID: 100.11138 - Hasaki
|
|
|
|