|
You have to either use an owner draw list box or a custom control[^] (shameless self-promotion).
|
|
|
|
|
Hallo
i get this Error when i try to load an Ax. has any one any idee?
Error:
Unable to get the window handle for the 'MyAx' control. Windowless ActiveX controls are not supported.
OS:
Windows 7 32Bit
Visual Studio 2010
modified 29-Apr-12 7:18am.
|
|
|
|
|
|
Hello,
I've a question about xmlSerializer in C#,
My question is how can you create an xmlfile with the xmlserializer from two or more different objects without creating a class contains all those objects.
For example this is the xml file wanted:
="1.0"="UTF-8"
<Root>
<Books>
<Book>
<Title>X</Title>
<Author>Mr x</Author>
</Book>
</Books>
<Movies>
<Movie> Movie Y</Movie>
</Movies>
</Root>
At the moment I'll do this with the XDocument class and create for each property an Xelement.
For example I create an XElement books which contains child elements and add them all to an XDocument and save this XDocument object.
public XElement getBooksElement(List<Book> books)
{
XElement ret = new XElement("Books");
foreach(Book b in books)
ret.Add(getBookElement(b));
return ret;
}
public XElement getBookElement(Book book)
{
XElement ret = new XElement("Book");
ret.Add(new XElement("Title", b.Title));
ret.Add(new XElement("Author", b.Author));
return ret;
}
How can be this achieved without creating a class containing all elements which should be in the object but serialize each object to an xml part and add those to the xml file.
I hope my question is clear enough,
Thank you in advance.
|
|
|
|
|
So you want to serialize each object individually, without creating a container class to hold all the objects? How about if you serialize each object to a MemoryStream, then create an XElement or XDocument to contain them all, and stick all the data you saved into the MemoryStreams inside it?
|
|
|
|
|
Yes that's sounds like a good idea but how do I create a XElement or XDocument out of the memorystream, and how can I manage the deserialization of this? Because should I read the XDocument and divide this XDocument in multie memorystream for dividing this up?
|
|
|
|
|
You can use the XDocument.Load static method to create an XDocument from the stream. (You might need to call Seek on the stream to reset it to the beginning!) As for creating multiple MemoryStreams to load the XDocument's elements, you can do this:
foreach (var element in myXDocument.Elements())
{
var ms = new MemoryStream(Encoding.Unicode.GetBytes(element.ToString()));
}
|
|
|
|
|
I don't really have a big issue with the powershell working because it works just fine.
The problem I'm having is the method exits before the command is actually completed on the Exchange server. I think it may have something to do with the way Exchange throttles powershell commands.
Is there a way to use powershell with Exchange but have it completely wait until the command is finish before refreshing my page?
Let me give you an example:
A user logs into the web interface and decides to enable their user for Exchange. So the user clicks Enable Exchange. This fires a powershell to Exchange running the Enable-Mailbox command. After it completes the command it retrieves updated information from Active Directory / Exchange about the users mailbox.
The problem I have is when it tries to retrieve the updated information it hasn't had time to populate in Exchange so it still thinks there isn't a mailbox. I know I could put a sleep in there but I rather do something cleaner.
Here is my base class:
public void RunPowershell(PSCommand pscmd = null, String script = null)
{
Runspace remoteRunspace = null;
PSDataCollection<ErrorRecord> errors = new PSDataCollection<ErrorRecord>();
OpenRunspace(_uri, _schema, _username, _password, ref remoteRunspace);
remoteRunspace.Open();
powershell.Runspace = remoteRunspace;
powershell.Streams.ClearStreams();
powershell.Commands.Clear();
if (pscmd != null)
{
String debugLog = "[RunPowershell] Executing powershell: ";
foreach (Command cmd in pscmd.Commands)
{
debugLog += cmd.CommandText;
foreach (CommandParameter cp in cmd.Parameters)
debugLog += String.Format("-{0} {1}", cp.Name, cp.Value.ToString());
}
Log.Debug(debugLog);
powershell.Commands = pscmd;
}
else if (script != null)
{
Log.Debug("[RunPowershell] Executing powershell: " + script);
powershell.Commands.AddScript(script);
}
powershell.Invoke();
ThrowIfError();
Log.Debug("[Exited] RunPowershell");
}
private void OpenRunspace(string uri, string schema, string username, string password, ref Runspace remoteRunspace)
{
SecureString securePass = new SecureString();
foreach (char c in password.ToCharArray())
securePass.AppendChar(c);
PSCredential psc = new PSCredential(username, securePass);
WSManConnectionInfo wmci = new WSManConnectionInfo(new Uri(uri), schema, psc);
wmci.AuthenticationMechanism = AuthenticationMechanism.Basic;
remoteRunspace = RunspaceFactory.CreateRunspace(wmci);
}
Here is the command running Enable-Mailbox:
public void Execute()
{
try
{
if (String.IsNullOrEmpty(Identity))
throw new Exception("Identity must be specified.");
if (String.IsNullOrEmpty(Alias))
throw new Exception("Alias must be specified.");
if (String.IsNullOrEmpty(DisplayName))
throw new Exception("Display Name must be specified.");
if (String.IsNullOrEmpty(PrimarySmtpAddress))
throw new Exception("The Primary Smtp Address must be specified.");
if (String.IsNullOrEmpty(CompanyCode))
throw new Exception("The Company Code must be specified.");
String enableMailboxCmd = String.Format(@"Enable-Mailbox -Identity '{0}' -AddressBookPolicy '{1} ABP' -Alias '{2}' -DisplayName '{3}' -PrimarySmtpAddress '{4}';",
Identity, CompanyCode, Alias, DisplayName, PrimarySmtpAddress);
String setMailbox = String.Format(@"Set-Mailbox -Identity '{0}' -OfflineAddressBook '{1} OAL' -CustomAttribute1 '{2}' -IssueWarningQuota:{3}KB -ProhibitSendQuota:{4}KB -ProhibitSendReceiveQuota:{5}KB -UseDatabaseQuotaDefaults $False;",
Identity, CompanyCode, CompanyCode, WarningQuota, ProhibitSendQuota, ProhibitSendReceiveQuota);
String setCasMailbox = String.Format(@"Set-CASMailbox -Identity '{0}' -ActiveSyncEnabled:${1} -ActiveSyncMailboxPolicy '{2}' -IMAPEnabled:${3} -POPEnabled:${4} -MAPIEnabled:${5} -OWAEnabled:${6} -OWAMailboxPolicy '{7}';",
Identity, ActiveSyncEnabled.ToString(), ActiveSyncPolicy, IMAPEnabled.ToString(), POPEnabled.ToString(), MAPIEnabled.ToString(), OWAEnabled.ToString(), OWAPolicy);
this.RunPowershell(null,
enableMailboxCmd +
setMailbox +
setCasMailbox);
}
catch (Exception ex)
{
throw ex;
}
finally
{
this.Close();
}
}
Thank you for your help
|
|
|
|
|
Hi,
I am working on a c# app that talks to an external device via a comm port.
The external device comes with a header file that defines the commands that can be sent and the possible return values, all as #define's.
Is there a way to get thease #define's in to my c# app, other than copying and pasting?
Thanks.
“If I had asked people what they wanted, they would have said faster horses.”
― Henry Ford
|
|
|
|
|
Ok, even if i get the header included some how, its still not going to work...
MSDN: The #define directive cannot be used to declare constant values as is typically done in C and C++.
What a crock.
“If I had asked people what they wanted, they would have said faster horses.”
― Henry Ford
|
|
|
|
|
What's a crock is that in the original C/C++ files, the original programmer made the mistake of depending on a compiler-specific feature to write code.
I know full well that everyone used to use #define to declare their constants in C/C++, even when they had a perfectly good "const" keyword, but in doing so, the made the code specific to the C/C++ compiler.
Sorry, but don't blame C# for the failings of some other programmer in a previoud language.
|
|
|
|
|
At one place where we did C, the "guru" specified that we should use define s (rather than const s etc.) as much as possible because they don't use memory.
|
|
|
|
|
Guess I have a lot code to change.
BDF
I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be.
-- BillWoodruff
|
|
|
|
|
|
Well... the quick and dirty way would be to pass it through a C pre-processor. However, you may want to limit the scope of what you do that way.
Without seeing what's actually in the header file and which parts you need, I'll show a simple example of what can be done:
Example.h
# define OPEN 1
# define CLOSE 2
# define READ 3
# define WRITE 4
Example.csi (choose an extension)
namespace Support
{
public static class Command
{
public const int Open = OPEN ;
public const int Close = CLOSE ;
public const int Read = READ ;
public const int Write = WRITE ;
}
// or
public enum Command
{
Open = OPEN
,
Close = CLOSE
,
Read = READ
,
Write = WRITE
}
}
Pass them through a C pre-processor, this uses the Visual C/C++ one:
cl /nologo /C /EP /P /FIC:Example.h /FiExample.cs Example.csi
Results in:
Example.cs
namespace Support
{
public static class Command
{
public const int Open = 1 ;
public const int Close = 2 ;
public const int Read = 3 ;
public const int Write = 4 ;
}
// or
public enum Command
{
Open = 1
,
Close = 2
,
Read = 3
,
Write = 4
}
}
You can then include this file in your project. If needed, you can have a pre-build step that performs the above pre-processing.
Notes:
0) The pre-C# file (csi) should not contain any directives
1) You should include a notice that it was generated
2) You could also set the file to read-only
3) Use a partial class if you want this to be part of a larger class so you don't pass the whole thing through the pre-processor
See also my Implanting Common Code in Unrelated Classes[^]
|
|
|
|
|
PIEBALDconsult wrote: the quick and dirty way would be to pass it through a C pre-processor.
That would seem like the ideal solution to me.
|
|
|
|
|
Hi,
I am currently creating a media player but as I am searching online for some help, no one seems to be able to provide the information. I am trying to have a media player which I find easy to build to have it playing videos but what I am having the issue with is creating a file on the computer and having files like mp3 which are stored there be automatically put into a library on the media player which then can be played. I want it so that the files in a folder will be automatically stored onto the media player which then I can search for on the application for the file name and click to play it. Please can people be specific because people can be very vague and state the obvious and then leave it at that. What will I need to create e.g. access database ?, and what will I need to do for it like create a class coding to list in a drop down box etc.
If anyone can help I will be highly greatful.
|
|
|
|
|
O.Calderbank wrote: I am currently creating a media player
O.Calderbank wrote: What will I need to create e.g. access database ?, and what will I need to do for it like create a class coding to list in a drop down box etc.
There seems to be some discrepancy between your statement and your question. Rather than trying to decide whether to use a dropdown or not, you need to think in more abstract terms about the design of your application. For example, what problem do you think will be solved by an Access database or by a dropdown box?
Have you looked into the basics of how to play videos[^] in your program?
Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness
|
|
|
|
|
Please do not cross post.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
Coming from a C++ background, I'm keen to learn the C# idiom. Picking up the language is simple enough, and I'm finding my way around the class library, but I'm not familiar with the idiom, or "how things are done".
I would like the code I write to follow the conventions well enough that an experienced C# developer reading the code can see what's going on without getting confused about alien idioms.
(One example I've noticed is the use of foreach rather than for loops over collections.)
Now, I have a document class for a desktop application, and I'd write its IO interface like this:
class Document
{
public Document()
{}
public Document(string path)
{}
public void Write()
{}
public void Write(string path)
{}
}
I don't think this is idiomatic C# though. Most examples seem to pass in streams instead of path names, for instance, and handle that aspect (eg opening and closing IO streams) outside the document class, in the application. Is that correct? Should I write a separate serializer?
Please let me know what the "proper" C# way of doing things like this is.
Cheers,
Orjan
|
|
|
|
|
I really wish I wasn't typing this on my phone because this is a fascinating question. Unfortunately I will have to give the shorthand version. The reason for using a stream rather than a path lies in OOP principles. Basically it's down to single responsibility The reasoning of it is this, your class shouldn't have to have different methods to serialise to different locations, so using a stream means that you can save to memory, or isolated storage or a standard file path, all with the same code.
Saying that, it is common to provide a method that accepts a path which instantiates a stream and then calls the relevant override.
|
|
|
|
|
I think what Pete alludes to is what I was going to say -- I would likely write several overloaded methods, depending on the situation:
0) One that takes a TextWriter and does the actual writing
1) One that takes a Stream, wraps it in a TextWriter, and passes it to 0
2) One that takes a FileInfo, opens a Stream for it, and passes it to 1
3) One that takes a string, wraps it in a FileInfo, and passes it to 2
A downside to this is that it adds a bunch of method calls and each method will likely perform its own parameter validation (null checking).
|
|
|
|
|
A stream is a wrapper around an array of bits. A file's location is a property of a file. Superfluous to mention; a MemoryStream doesn't have a filename, and neither does a NetworkStream.
The class that you're describing does not only contain the contents of a file, but also holds the most common operations on that file from your application. I'd go for one constructor (due to KISS) until you actually need a second one. How you read the contents of the file is up to you, and how you make it's contents available too.
..but yes, I'd expect those methods to work internally with streams and a reader.
Bastard Programmer from Hell
|
|
|
|
|
Yes, a stream would be more normal, although as the others have said, a file path option doesn't hurt.
One of the big strengths of passing a stream rather than a file is that the stream can be encapsulated in the containing class to form a compound document. If you think of your document as an excel sheet, then passing streams enables the excel file to contain a number of encapsulated sheets without your document being aware of it - you can't easily do that with files. Certainly, I would make the commitment to output to file at the highest level I can - if only so that I can easily reuse the lower level components.
foreach is a very, very useful construct: it can't replace a for loop, but it builds on it:
If you do not need to know the position of an element in a collection, using a foreach loop makes the code cleaner and easier to read - you aren't creating simple variables unnecessarily.If you do not want the collection to change while you go though it, the a foreach is much better - any attempt to add or remove elements will cause a run time error immediately, rather than waiting until something goes out of range.One of the hardest differences to get used to between C++ and C# is variable naming: gone is Hungarian notation, gone is m_blahblah, g_balhblah, and so forth! I've been using C# almost exclusively for a nuymber of years and I still call Buttons butOK and TextBoxes tbUserName!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Hello all,
I am making a project to finish my subject but I have some problem with the code of program. I am coding a code editor like Notepad++ for C#. I find any help in forum, but I have a problem that I can't find the help.
Well... Someone help me...
I want the code in a funtion or a region can expand and collapse like Nodepad++.
Can you guide me? Can you give the code demo?
Please give me the help to email: nguyenquangsang24@gmail.com
Thank very much.
|
|
|
|
|