|
A much simpler way is to use the Process object that is returned from Process.Start and to use the Process.MainWindowHandle to the call for SetForegroundWindow :
Process p = Process.Start("gsview32.exe, openFileDialog.FileName);
if (p != null)
SetForegroundWindow(p.MainWindowHandle); Where SetForegroundWindow is defined as:
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for sssyed_in and Heath,
Finally I use Heath's method to solve my problem...
But then I have one new problem to ask:
How to set my own program back to ForegroundWindow...??
Thank you very much!!
|
|
|
|
|
Pass either your main Form 's Handle property (inherited from Control ) into SetForegroundWindow , or use Process.GetCurrentProcess().MainWindowHandle . The former is much quicker and recommended if possible.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Here's the issue :
I want to serialize some objects whose class contains a static attribute and I need to find this value again when I deserialize my objects.
For example :
[Serialize]<br />
public class MyObject<br />
{<br />
static int _NbInstances;<br />
string _Name;<br />
<br />
...<br />
}
How to make _NbInstances persistant ? Where should this value be saved ?
- Éric -
|
|
|
|
|
This is a conceptual no-no.
Static attributes should not be serialized with instances, because static attributes are not instance attributes. You can include these values by creating your custom serializer, but if you include these values in serialization, this will lead to inconsistent values, because every time you deserialize an instance, the static values will change.
A better design would be creating two methods, e.g. saveStatics() and readStatic() on each class that needs it.
BTW, a static "string _Name" smells bad design...
Perl combines all the worst aspects of C and Lisp: a billion different sublanguages in one monolithic executable. It combines the power of C with the readability of PostScript. -- Jamie Zawinski
|
|
|
|
|
Hello Daniel,
I know what you mean and I don't want to serialize the static value WITH each instance, but I was thinking of a kind of "meta-serialization" for class (just as static constructors exist).
In fact I'd like to know which design is the best for that. But it has to be done manually.
Note that I've writen these three lines of code quickly just as an illustration because the complexity of a better code is useless for the question.
Thank you,
- Éric -
|
|
|
|
|
Well, in that case, I think that one interesting possibility would be: group your static properties into a Singleton class. Then, serialize this Singleton. This way, you can only mark the singleton [Serializable].
If you think about it, you may be missing a class on your design that is simply a singleton...
Perl combines all the worst aspects of C and Lisp: a billion different sublanguages in one monolithic executable. It combines the power of C with the readability of PostScript. -- Jamie Zawinski
|
|
|
|
|
Hello,
Now I try to change default printer using C#.
After my investigation, I found it use WMI.
I used to WMI (Win32_Printer), but I couldn't.
The following is the source.
Please advice me.
best regards,
yu-yu
ManagementPath path = new ManagementPath();
ManagementBaseObject inParams = null;
ManagementBaseObject outParams = null;
string[] stemp = PrinterID.Split("\\".ToCharArray(), 8);
path.Server = stemp[2];
path.NamespacePath = @"root\CIMV2";
string relPath = string.Format("Win32_Printer.DeviceID='{0}'",PrinterID);
path.RelativePath = relPath;
try
{
ManagementObject mo = new ManagementObject(path);
outParams = mo.InvokeMethod("SetDefaultPrinter", inParams, null);
Console.WriteLine("Printer {0} is default now ", PrinterID);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
|
|
|
|
|
What didn't work about it? Give us a clue. Was an exception thrown and written out to the console in your catch block? Was the printer found? Make sure you're doing checks to see if an object is null, too. What if your printer object wasn't found. Since your try-catch block doesn't encompas everything, a NullReferenceException would've gone uncaught.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello,
>What didn't work about it? Give us a clue.
Yes, thank you.
when InvokeMethod() called, exception occuered.
>Was an exception thrown and written out to the console in your catch block?
Exception name is ManagementException: MethodNotImplemented System.Management.ManagementBaseObject
>Was the printer found?
Yes. I set 2 printers.
There are 2 printers in my network, and my pc too.
So I used them as network printer.
My environment is
Win2000Pro
.NET Framework1.1
VSNet2003
>Make sure you're doing checks to see if an object is null, too. What if your printer >object wasn't found. Since your try-catch block doesn't encompas everything, a >NullReferenceException would've gone uncaught.
I remade a source.
Hope your help.
regards,
yu-yu
------------------------------------------------------------------------------
ManagementPath path = new ManagementPath();
ManagementBaseObject inParams = null;
ManagementBaseObject outParams = null;
path.Server = "."; <- local PC setting
path.NamespacePath = @"root\CIMV2";
string relPath = string.Format("Win32_Printer.DeviceID='{0}'",PrinterID);
path.RelativePath = relPath;
try
{
ManagementObject mo = new ManagementObject(path);
outParams = mo.InvokeMethod("SetDefaultPrinter", inParams, null);
Console.WriteLine("Printer {0} is default now ", PrinterID);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
|
|
|
|
|
Instead of setting inParams to just null , try setting it to a valid instance like so:
ManagementBaseObject inParams = mo.GetMethodParameters("SetDefaultPrinter"); It won't contain any parameters, but it will be a valid instance with additional data. The following worked for me:
using System;
using System.Management;
public class SDP
{
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.Error.WriteLine("Invalid printer name.");
Environment.Exit(1);
}
ManagementObject mo = new ManagementObject(
string.Format("Win32_Printer='{0}'", args[0]));
ManagementBaseObject inParams =
mo.GetMethodParameters("SetDefaultPrinter");
ManagementBaseObject outParams =
mo.InvokeMethod("SetDefaultPrinter", inParams, null);
if ((uint)outParams["ReturnValue"] == 0)
Console.WriteLine("Success!");
}
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Mr. Stewart
Thanks,
May I ask a question.
Please tell me your Environment.
I checked SetDefaultPrinter(). so I found it need to XP higher.
Now the environment I think is Win2000 higher.
If this method can't use, how do you set default printer ?
regards,
yu-yu
|
|
|
|
|
I'm on WinXP, but the WMI providers are pretty much universal (there's some that aren't supported in Win9X, but 2000 and above supports about the same functionality). You should go to MSDN[^] and download the WMI SDK to use the management application to see what's available. There's also a Server Explorer extension for management classes and events you can download for both VS.NET 2002 and 2003. Just search for "Server Explorer Management" on MSDN and you should find the links.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks,
I try to do.
regards,
yu-yu
|
|
|
|
|
How do I give a Textbox input focus?
I use the following code, but nothing happens:
if (txtTextbox.CanFocus)
txtTextbox.Focus();
|
|
|
|
|
Set the Taborder of the Textbox as 0 or first focus control of the form.
|
|
|
|
|
Hi,
I want to save Armenian UniCode Characters in my tables of SqlServer any body knows which collation should I choose for my DataBase ?!!
or Some Place I can find the right collation I searched the microsoft collation list but I find nothing.
I post this message in the Part Sql/ADO but no one answer !!!
Thanks in advance .
|
|
|
|
|
This forum is for C# questions. See the SQL forum instead.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
thanks for telling me ! but I don't know if you Read my message !!!!
"I post this message in the Part Sql/ADO but no one answer !!! "
C# Forum have seen a lot more by developers !
|
|
|
|
|
Ok, here's the trick. I need to be able to compile code at run-time and then save the compiled code for later use so I don't have to compile it again. What's the trick to this? WHat should I be looking up? Should I be making an assembly, or is there a better way to do it?
NATHAN RIDLEY
Web Application Developer
email: nathan @ netlab.com.au
[remove the spaces before and after the @ symbol]
|
|
|
|
|
Never mind, I missed the parameter that specifies the DLL to create :P
NATHAN RIDLEY
Web Application Developer
email: nathan @ netlab.com.au
[remove the spaces before and after the @ symbol]
|
|
|
|
|
Is it possible to use C# controls in a MFC Dialog based application. I have a C# control of a fancy button, and want to add this button on the MFC Dialog. Any help in this context will be much appreciated.
Regards
|
|
|
|
|
|
im trying to multiply some numbers num1 is a normal integer,(1,2,3....) and num2 is a decimal number(0.23,1.00,3.00....) im having problems multiplying them, i've done it different ways sometimes i get a 0 in the total textbox or this error : Input String was not in a correct format. Can somebody help me out to rectify this problem.
<br />
if(Pharma.Rows.Count != 0)<br />
{<br />
string Tablet = (string) Pharma.Rows[0][0];<br />
<br />
decimal num2 = Convert.ToDecimal(Tablet);<br />
<br />
MessageBox.Show(num2.ToString());
<br />
int num3 = Convert.ToInt32(num2);<br />
<br />
int num1 = Int32.Parse(txtQuantity.Text);<br />
<br />
MessageBox.Show(num1.ToString());
<br />
total = num1 * num3;<br />
<br />
decimal total1 = Convert.ToDecimal(total);<br />
<br />
MessageBox.Show(total.ToString());
<br />
txtMedTotal.Text = total.ToString();<br />
}<br />
else<br />
{<br />
MessageBox.Show("The Patient has yet to be processed","Information");<br />
}<br />
<br />
<br />
this code is givin me back a result of 0...when it should be 2.76
im three times confused now!
|
|
|
|
|
Hi
Where you Put
int num3 = Convert.ToInt32(num2);
Num2 turns to 0.because it has 0 as integer part.you better convert Num1 to Decimal !! or just Multiply Nme1 and Num2
|
|
|
|