|
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
|
|
|
|
|
Hello everybuddy,
I have a public property of type pClass in my component class. When I have my component on a design surface selected, in the property panel the pClass property exists but is disabled (faded to gray) while I want it to have a plus mark on the left so when clicked extend and show cClass public properties.
Can you help me?!
- Den2fly
---
"Art happens when you least expect it."
|
|
|
|
|
You need to implement a custom TypeConverter . You then attribute pClass or the property of that type (better on the class except in rare cases or when it's not possible - like for types that already have TypeConverter s) using the TypeConverterAttribute .
You can find more information about the TypeConverter class at http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemComponentModelTypeConverterClassTopic.asp[^]. You should also read Enhancing Design-Time Support[^], also in the .NET Framework SDK.
Finally, it may be read-only because the property does not have a setter (the set accessor of a property). Make sure that's not the case. The TypeConverter , though, can help you do many things, such as converting to and from a string representation of your class (like it does for Point , Size , Enum s, Font , etc. You can also use it to display a property set that is shown when you expand your property, like the Font class.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi guys,
I'm developing a web application which requires me to embed windows media player 9. This player will be able to choose from a combo box which server it wants to display video from. Now, the combo box is a web control which is declared and can be manipulated in .cs file, but how do I set the URL of the windows media player 9, which is a windows control, since it is not declared in the .cs file. It's only declared as an object on .aspx file. I've tried JavaScript but didn't work as well, always gives an error on page. My questions are:
1. Is there a way to convert the windows media player 9 to web control?
2. Do I have to add a reference? is it to wmp.dll or wmppia.dll? tried 'em but didn't work.
3. Or is there any other way so that I can do what I'm supposed to do?
Desperate for ur help guys...
thankz...
|
|
|
|
|
You can make any tag an HTML control simply by adding runat="server" to its attribute list. Unfortunately, this won't expose the functionality you need (which is basically to add a PARAM element inside the OBJECT tag with the necessary NAME and VALUE attributes (using "URL" for the NAME ). So, there are several alternatives. You could always output this element directly to the page using the old ASP-style output. Inside your OBJECT tag, put:
<% Response.Write(UrlPropertyValue) %> , where UrlPropertyValue is a field or property that contains your URL to play. You could also output a script using either Page.RegisterClientScriptBlock or Page.RegisterStartupScript (see the documentation for these to methods in the .NET Framework SDK for more information). So long as your OBJECT tag has an ID attribute, you can call methods and get/set properties using the ID. This is pretty common and works well when you do it correctly.
There was a WebControl that did much of this here on CodeProject, but I'll be jiggered that I can't find it. You can try searching for it in the ASP.NET section. Really, though, it just does pretty much what I described above.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I get what you mean, but I need to set URL based on the client input. Therefore, I have to do the URL settings on the .cs file. Because the comboBox is a web control, which is used as the user input, I have to take that value, which only can be done in .cs file (or isn't it?) and The windows media player is a windows control, which is not declared at all at the .cs file, which makes me impossible to set the URL of it. Do you have any other suggestions? I don't really want to use JavaScript unless I have to.
Please help me guys...
|
|
|
|
|
It doesn't HAVE to be in the .cs file. You can mix code in both the code-behind files and in the .aspx files. The .aspx derives from the class in your .cs file, which derives from the Page class (or any other class you have choosen). A good OO design would let you do it either way.
You can still include an HTML control that lets you programmatically assign attributes. You could either write your own (not hard - add a property and override Render to output the HTML with the assign URL) or use a generic HtmlControl in place of the param, which should work:
<object ...>
<param name="URL" value="" id="URLParam" runat="server"/>
<!-- ... -->
</object> Then in the page, make sure that a HtmlGenericControl is defined as protected. In your code, simply add the attribute value:
protected HtmlGenericControl URLParam;
URLParam.Attributes["value"] = URLTextBox.Text;
There are many other ways you can accomplish the same thing, with or without a client script block (which also isn't hard). Just read the .NET Framework SDK documents.
Microsoft MVP, Visual C#
My Articles
|
|
|
|