|
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
|
|
|
|
|
Thanks for the info. I now have a working solution thanks to .NET
1. Write the base class in C# in a seperate DLL & namespace
2. Derive a new class in VB from the base class in another DLL - I can use Late Binding features of VB to automate MS Office (All Versions) without writing pages of code.
3. Use this new class in the top level program (C#)
All Thanks for .NET
Man I love .NET
Peter Tewkesbury
Developer
ibsl technologies
|
|
|
|
|
I am writing a custom Grid and can't figure how to get the scrolling to work.
I am trying to call ScrollWindow from CSharp and it doesn't seem to be doing anything. I am not getting any exceptions. Can anyone explain what the expected results should be or tell me what i am doing wrong.
Any help would be great!!!!
Code: [DllImport("User32.dll")] public extern static bool ScrollWindow(IntPtr hWnd, int nXAmount, int nYAmount, ref RECT rectScrollRegion, ref RECT rectClip);
public struct RECT {
public int bottom;
public int top;
public int left;
public int right;
public RECT(int left, int top, int right, int bottom) {
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
}
/***************/
RECT rect = new Rect(102, 2, 770, 381); bool b = NativeMethods.ScrollWindow(this.Handle, change, 0, ref rect, ref rect);
|
|
|
|
|
I think you're doing too much work on your own here. Windows Forms provides a ScrollableControl class (as well as Panel and ContainerControl).
If you have your control inherit from one of those the scrolling will be taken care of for you, you can set the current position with the AutoScrollPosition property (assuming you have autoscrolling enabled).
Now sure off hand what sets the bounds for scroll, my first guess would be something with the AutoScrollMinSize property.
HTH,
James
Sonork ID: 100.11138 - Hasaki
|
|
|
|
|
Here's a reply I got offline from the original poster, posted here for archival purposes
Thanks for the suggestion, but i determined what my
error was. I defined my RECT structure incorrectly so
that when it was being passed from managed to
unmanaged code, it was being marshalled incorrectly.
The correct definition of RECT:
[StructLayout(LayoutKind.Explicit)]
public struct RECT
{
[FieldOffset(0)] public int left;
[FieldOffset(4)] public int top;
[FieldOffset(8)] public int right;
[FieldOffset(12)] public int bottom;
public RECT(int left, int top, int right, int
bottom)
{
this.left = left;
this.top = top;
this.right = right;
this.bottom = bottom;
}
}
Sonork ID: 100.11138 - Hasaki
|
|
|
|
|
Is it possible to write a function that return 2 different data types?
e.g. if it's TRUE, it returns a string, and if it's FALSE, returns an array of string.
Thanks alot!
|
|
|
|
|
If C# supports a VARIANT you could do it that way, so long as both types were able to be stuffed into a VARIANT.
Christian
After all, there's nothing wrong with an elite as long as I'm allowed to be part of it!! - Mike Burston Oct 23, 2001
Sonork ID 100.10002:MeanManOzI live in Bob's HungOut now
|
|
|
|
|
You could also use the out keyword.
public bool OurFunction (out object value2)
{
bool value;
if (value)
value2 = "This object returned true!!!";
else
value2 = new string [] {"This object", "returned", "true", "!!!"};
return value;
}
Or you could simply just return an object and then check the type wheter it's true of false...
public object OurFunction2 ()
{
bool value;
object value2;
if (value)
value2 = "This object returned true!!!";
else
value2 = new string [] {"This object", "returned", "true", "!!!"};
return value2;
}
public void MainFunction ()
{
object obj = this.OurFunction2 ();
if (obj.GetType () == typeof (string))
Console.WriteLine ("TRUE!!!")
else
Console.WriteLine ("FALSE!!!");
}
Andreas Philipson
|
|
|
|
|
It could return object, which could either be a string or an array of string.
In general, however, I think that's a bad idea. Why not return an array with a single item for the first case?
|
|
|
|