|
I'm aware of how to use arrays similar to the following example.
static void Main()
{
ArrayList list = new ArrayList();
list.Add(45);
list.Add(87);
list.Add(12);
foreach(int num in list)
{
Console.WriteLine(num);
}
}
However, I'm trying to capture data strings from a log server and parse them in memory rather than FileStreaming them to a log.txt file and parsing later.
I can view the ArrayList activity with a listBox and it seems to me it only holds one (1) string at a time. I need a growing list in memory that I can sort and parse. Here's my failing setup so far. Help...... (C#)
str = System.Text.Encoding.ASCII.GetString(data); //data is a Byte[]
Al = new ArrayList(1024);
Al.Add(str);
foreach(string Line in Al)
{
listBox1.Items.Add(Line.ToString());
}
// Knowledge is a one way street, Wisdom is looking both ways before crossing
|
|
|
|
|
You've only added one string to the arrayList, so how many were you hoping would come out ? If there is a seperator in the byte[] that denotes the position of multiple strings, then use the Split function of the string class to turn str into a String[], which you can then foreach into the listbox. Assuming your delimiter is a slash or something, otherwise you may have to parse the byte array instead.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
just bind the arraylist's datasource and dont add/remove items to the listbox itself eg.
listBox1.DataSource = Al;
SHould be all you need. Look at the help too, if you want to customise the display of the listbox (something member (datamember?) property).
top secret xacc-ide 0.0.1
|
|
|
|
|
For those of you wanting to use file system type pattern matching on strings, here is some code for you. For example, you can use *, ?, and # wildcards in your pattern and test a string against it to see if it matches. This is similar to the VB LIKE function.
Ref: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxgrfwildcards.asp
*********Code follows************
///
/// Matches the pattern and string.
///
/// <param name="pText" />The Text string to be matched.
/// <param name="pPattern" />The pattern containing *, ?, and # wildcards.
/// <param name="pIgnoreCase" />True=Ignore Case.
/// <returns>True if the text and pattern match.
public static bool Like(string pText, string pPattern, bool pIgnoreCase)
{
bool retVal = false;
if(pText == null || pPattern == null)
{
return retVal;
}
if(pText.Length == 0 || pPattern.Length == 0)
{
return retVal;
}
if(pPattern == "*" || pPattern == "*.*")
{
retVal = true;
}
else
{ //Replace the pattern matching characters with temp values
//So all other characters can be escaped
pPattern = pPattern.Replace(@"*","\xfc");
pPattern = pPattern.Replace(@"?","\xfd");
pPattern = pPattern.Replace(@"#","\xfe");
//Escape all other characters
pPattern = Regex.Escape(pPattern);
//Replace the temp values with the required regular expressions
pPattern = pPattern.Replace("\xfc",@".*[^.]");
pPattern = pPattern.Replace("\xfd",@".");
pPattern = pPattern.Replace("\xfe",@"[0-9]");
if(pIgnoreCase == false)
{
retVal = Regex.IsMatch(pText, pPattern, RegexOptions.Compiled);
}
else
{
retVal = Regex.IsMatch(pText, pPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
}
return retVal;
}
private void TestTheLikeFunction ()
{
bool val;
// should return true
val = (Like(@"workbook.xls",@"*.xls",true));
val = (Like(@"workbook.xls",@"workbook*",true));
val = (Like(@"workbook.xls",@"workbook.*",true));
val = (Like(@"workbook.xls",@"work*.xls",true));
val = (Like(@"workbook.xls",@"work????.*",true));
val = (Like(@"workbook.xls",@"work*.*",true));
val = (Like(@"workbook.xls",@"*orkbook.xls",true));
val = (Like(@"workbook.xls",@"?orkbook.xls",true));
val = (Like(@"workbook.xls",@"?orkb??k.xls",true));
val = (Like(@"abcxls",@"*xls",true));
val = (Like(@"abcxls",@"???xls",true));
val = (Like(@"ab1xls",@"??#xls",true));
val = (Like(@"a12xls",@"?##xls",true));
// should return false
val = (Like(@"abcxls",@"#*",true));
val = (Like(@"abcxls",@"?#?xls",true));
val = (Like(@"abcxls",@"*.xls",true));
val = (Like(@"abcxls",@"*xls?",true));
val = (Like(@"abcxls",@"????xls",true));
val = (Like(@"workbook.xls",@"workbooke.*",true));
val = (Like(@"workbook.xls",@"work*xls",true));
val = (Like(@"workbook.xls",@"work?????.*",true));
val = (Like(@"workbook.xls",@"work???.*",true));
}
RobC
|
|
|
|
|
Rob,
I would recommend that you write up your findings as an article and submit it. The C# forum is to allow people to post questions and answers about the C# language. There are too many threads in this forum, yours will quickly disappear. Good luck.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hi all. Making my first "big" C# WinForms app. Basically, I have a WinForm (called MainForm) that has a menu, and a tab control docked to take up the full area. Eventually the number of tabs will be dynamic based on how many "workspaces" a user has open but for now is static. Within the first tab page, I am inserting a custom control -- called TypeAContentControl. There may be several of these on different tab pages (depending if the user called for multiple instances). That control actually contains, you guessed it, yet another control, and also contains a treeview and a panel which will house a future control. The control that is housed within TypeAContentControl is called SearchControl. It's pretty basic, contains a radio set, a text box, and a single button (which is at the heart of my question).
I can't seem to figure out how to make this the "default" button. I only need it default while the focus is within other fields of the SearchControl (ie, I want to type in my textbox, or be on my radio buttons and hit Enter to perform an action). The only time I seem to hit the code behind the button is if I tab enough times so that the button has the focus, and then hit spacebar or enter. If I'm on another field within SearchControl, I have to use the keyboard accelerator to hit my button.
I know on a standard WinForm with buttons, I can just set that Form's AcceptButton accordingly, but I do not see that as the solution for this problem.
Anyone have any clues as to what might help me out here? I've been Googling and searching the MSDN for a while now trying to figure this out and just not finding anything helpful. Any help would be appreciated.
Thanks,
-John
|
|
|
|
|
Ok, clarification of some of the above: "I know on a standard WinForm with buttons, I can just set that Form's AcceptButton accordingly, but I do not see that as the solution for this problem." -- I can't even find an "AcceptButton" property except on my *Form*. It appears that the UserControls don't expose any of the behavior needed to make a default button work (someone please tell me I'm wrong =>).
So what I did for now is made a event handler for KeyDown on each of the fields within my SearchControl user control, and any time an Enter is detected, I send the button a PressButton command. This seems to work, however, I would like it to show the button as a default button (extra border basically). Am I going to have to owner draw the button? I'm not opposed to that, it just seems like this is an awful lot of monkeying for something I could do in ATL very easily. Or does someone have the "correct" solution to this whole default button & user control problem? Maybe the solution involves "Man you are totally designing/coding that wrong, go grab this book on WinForm development."
Thanks,
-John
|
|
|
|
|
There's a couple different things you could do. One would be to define those properties on your UserControl derivative, and then when you add that to a Form (it's going to happen eventually) assign Form.AcceptButton to MyUserControl.AcceptButton . You could even use a designer to do this automatically.
Another one - though I'm not sure it'll work - is to extend the Button class and override the CreateParams property like so:
using CP = System.Windows.Forms.CreateParams;
const int BS_DEFPUSHBUTTON = 0x01;
protected override CP CreateParams
{
get
{
CP cp = base.CreateParams;
cp.Style |= BS_DEFPUSHBUTTON;
return cp;
}
} If you wanted, you could encapsulate this behavior into a property that conditionally sets the BS_DEFPUSHBUTTON window style.
The questionable part is whether the Windows Management APIs will really care if this button is not in a Form (a dialog, as it knows it). Good luck!
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you for the reply. The first solution sounds extremely elegant, but I'll need to make sure on entry of any UserControl that I reset Form.AcceptButton to be the valid default button for whatever control I'm on. Not a big deal but seems like the maintenance of the field could get buggy in complex situations.
The second solution seems like it will be what I need to try, and the ability to set the style conditionally, although I can't think of an instance where I'll need to use this now, seems like something I should try to add. Either way, thank you for your suggestions, much better than checking for Enter on each keypress of each field.
|
|
|
|
|
Hello!
I want to create an application that can play mp3-files. What's the best way of going?
Is there any class in the framework that i can use?
Thanks!
/Erik
|
|
|
|
|
Your best bet would be to go with DirectX, it has inherent support to play both audio and video.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Hi Erik,
If what you want is basic control over mp3 files (play, stop, pause etc), your best bet would be to use the AudioVideoPlayback component of DirectX.
The DirectX SDK comes with some examples and you should be able to code a simple app to play mp3 files in a few minutes' time.
|
|
|
|
|
Hi all,
as already described in an other thread, I am currently working on drawing PieCharts like this:
http://mitglied.lycos.de/axbycz/charts/chart.jpg
I have a little testapp that writes a bitmap of a PieChart to harddisk, when I press a button.
This works fine for the first time, but if I press the button a second time, I get a System.ArgumentException:
exception.Message is "Invalid parameter used."
HResult -2147024809
COMPlusExceptionCode -532459699
Here's my little application:
http://mitglied.lycos.de/axbycz/charts/Charting.zip
Would be great if someone could have a look at it. I'm think I'm going insane, because I can't find, what the problem is
|
|
|
|
|
And as I mentioned, make sure you're disposing all Bitmap (or Image ) and Graphics objects when done. Not disposing of them can cause memory problems, though the exact exception is ambiguous. Try my suggestions I posted before about using the using statement (or just code the try-finally blocks yourself; it's all compiled to the same IL) and see if that fixed anything.
Disposing of the Bitmap on which you're drawing will also make sure the file handle to which it's saved is disposed (at least it's supposed to; to be sure, write to a FileStream instead of using a filename String and dispose of it when finished saving).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I already tried your suggestions. I added using statement for all Graphics and Bitmap objects, but this didn't solve the problem.
I also commented out the line where the bitmap is saved to disk, to make sure this is no filehandle problem, but this also didn't help.
Is there a way to explicitly dispose all objects that aren't disposed after the first drawing?
Best Regards
Bernd R.
-------------------------------------------
The light at the end of the tunnel has been switched off temporarily due to budget problems...
|
|
|
|
|
Don't use it for the Bitmap you're going to return. If you do, the object will be disposed and anything you do with it will throw an ObjectDisposedException . Just dispose of everything else that implements IDisposable .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I noticed that, when I tried to save the bitmap that was returned by the DrawChart method
I think I have to try out a memory profiler like this:
http://www.scitech.se/memprofiler/
Maybe this leads me to the problem.
-------------------------------------------
The light at the end of the tunnel has been switched off temporarily due to budget problems...
|
|
|
|
|
Brush blackBrush = Brushes.Black;
for(int i = 0; i < m_DataSource.Count; i++)
{
SolidBrush sliceBrush = new SolidBrush(m_DataSource[i].SliceColor);
graphics.FillRectangle(sliceBrush, origin.X + 5, origin.Y + 5 + (m_FontLegend.Height * i), 10, 10);
string strText = m_DataSource[i].Name +": "+ m_DataSource[i].Value.ToString();
int iStartX = origin.X + 20;
int iStartY = origin.Y + 1 + (m_FontLegend.Height * i);
graphics.DrawString(strText, m_FontLegend, blackBrush, iStartX, iStartY);
sliceBrush.Dispose();
}
blackBrush.Dispose();
See what the problem was? I disposed the system's black brush
-------------------------------------------
The light at the end of the tunnel has been switched off temporarily due to budget problems...
|
|
|
|
|
I've noticed that sometimes, specific parts of my application will run on my machine differently than the machine that it is actually intended to be run on. For instance, my computer, the one I am using to develop this application, there are MANY instances where the program has run flawlessly. However, when I move the application to the computers where it will be run, some things will either respond differently, or cause a crash. Examples of this include objects being disposed too early on the live-system machines, causing a crash. These same objects are alive and working fine at the same stages on the development machine. Another example is I want to simply position a button inside of a textbox (this is embedded in a datagrid column). Works fine on the development machine, and the button always shows up properly, and in the correct placement. However, on the live-system, the button seems to have a mind of its own, sometimes showing up, sometimes not, and usually always positioned in the wrong place.
So what is the deal with this type of behavior? Is this something that programmers will have to constantly deal with if using the .NET environment? The development and live-system computers all are running the same versions of the .NET Framework. I just don't get it. It is difficult to fix a problem when I can't duplicate the cause. Is this an inherent result of C#, or .NET, programs compiling as they are being run? I like the idea that they are actually being optimized for the specific machine they are being run on, but is the optimizer causing my programs to run differently on different machines? If that isn't causing the problem, then what in the world could it possibly be? I'm open to a lot of suggestions here!
Thanks, I hope somebody (Heath...hint hint) can help me out here.
|
|
|
|
|
First off, please don't address me personally. I'm flattered, but it's not very nice to others with good answers like Nick and Dave and others that show up from time to time.
Second, keep in mind that the .NET Framework - while the same on all Windows platforms (save the differences between Windows and Windows NT, like the FileSystemWatcher component that doesn't work on Windows), they rely on underlying system components like Windows Common Controls. Differences in the system there can make a little difference, though not as drastic as what you described with the positioned Button .
When it comes to positioning, the most common culprit is the DPI (dots per inch, sometimes refered to as PPI or pixels per inch). The difference in DPI affects how elements are positioned. As much as I hate VB (version 6 and below), it did get this right; it used twips, which is a logical unit (i.e., inches, points, centimeters, etc.). .NET uses pixels.
If you search the C# forum, you'll find several threads where I've posted more information about this including a snippet of code I use when necessary.
Other differences in behavior could be because you're using a debug build where another machine is using the release build. Debug builds are typically slower (not always true, but most often true). It could also be because you have some APIs (native functions, COM libraries, etc.) that you're P/Invoking or RCW'ing (it's a verb now, I say! ) that others don't have. Keep in mind when you deploy RCWs (interop assemblies), the corresponding COM libraries must be present as well (not necessarily the same version, but a version of the typelib and implementing COM server that uses the same CLSIDs and IIDs, assuming the COM server is written correctly).
For all these reasons, that's why companies test in large test bays with a multitude of different hardware and software configurations.
Also, before some *nix zealot jumps in and says "switch to linux", the same is true for linux and other *nix flavors as well. *nix solves DLL hell by using symbolic links but still requires that a library to which an executable was bound be installed on the platform.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks, I'll look into these causes and hopefully find the reasons.
I didn't mean to exclude anybody else, I just couldn't think of any other VIP names right offhand. I wanted a straightforward, and knowledgeable answer on this question, and I knew I'd get it if you answered. I appreciate the help!
- D
|
|
|
|
|
Hello people, I know that System.Security.Cryptography is a good way to protect data but now I have another problem. How to protect my private key? I can't leave may key on EXE or DLL's becaus C# is an easy way for reverse engeneering... I don't just said easy but... SO VERY MUCH EASY A LOT (lol)... What can I do to protect this?
Wender Oliveira
.NET Programmer
|
|
|
|
|
You wouldn't distribute your private key anyway. That's always bad no matter what you're doing.
If you're looking for a way to verify data, you'll want to use digital signatures. This allows you to distribute your public key (it's public, so it doesn't really matter*) and verify the signature of the signed data (it's a digest of the signed data encrypted with your private key; RSA and DSA implement this differently).
While my article probably isn't what you're looking for, read Using XML Digital Signatures for Application Licensing[^] for a good conceptual overview.
If you need to encrypt data, it's often best you use services provided by the OS. If you'll always run on Windows, read How To Create a DPAPI Library[^] on MSDN. This is a handy API for storing encrypted credentials, and is used by Passport (at least the local implementation that MSN and Windows Messengers use as well as IE for web sites). It's also very customizable and does most of the work for you.
If you want to implement your own encryption, generate a key pair on the machine after the app's installed. Store that in a key container. This method is fool proof, but not cryptoanalyst proof.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
i was under the impression that ANY time a control is accessed by a thread that didn't create the control, the call has to be marshaled to the creating thread, and .NET provides this by returning TRUE from Invoke.Required, and then using Invoke() passing it a delegate. many times i find myself forgetting to do this, and am reminded with mysterious crashes when the control is accessed from another thread. other times i forget, i am not reminded because it seems to work fine, which doesn't make sense to me. i stepped through code and confirmed that another (non-creating) thread was writing to a RichTextBox without a problem. my question is, why is this? is it just the richTExtBox that can be accessed from another thread?
|
|
|
|
|
You can call Control.Invoke, .BeginInvoke, .EndInvoke, .CreateGraphics on any thread you please.
The RTB will blow up on you eventually, though, I've seen it several times.
#include "witty_sig.h"
|
|
|
|
|