Click here to Skip to main content
14,837,225 members

Comments by Luc Pattyn (Top 163 by date)

Luc Pattyn 14-Feb-21 10:20am View
You're still using DateDiff where there is no need for it, you can calculate MinDOB and MaxDOB in VB.NET as I showed you, then just use SQL to compare Age to those limits.

Your query has multiple problems (missing space, incorrect use of BETWEEN).
I suggest you build your query string into a variable and have a look at it before you feed it to the database.

Also, you should put all of it inside a try=catch and look at the exception when you get one.

Luc Pattyn 13-Feb-21 15:35pm View
I don't know any object that has both a num_rows property and a fetch_assoc() method. Yet you have $sonuc do both?????????????
Luc Pattyn 13-Feb-21 15:30pm View
The change seems necessary and good, but insufficient.

I have some trouble understanding your code, it seems more complex than is needed. I mostly distrust your expression yielding the children. I'm not convinced rootitem is needed at all.

I'm used to enumerate files in a hierarchical file system, and that basically
goes like so (in a C#-like pseudo-code), starting with an empty result:

public static void AddAllFiles(List result, string folder) {
var entries=Directory.GetDirectoryEntries(folder); // both files and folders
foreach(DirectoryEntry de in entries) {
if (de.IsFile) result.Add(de);
else AddAllFiles(result, de.Name); // if is folder

So there is no special treatment of the first entry, no rootitem.

Suggestion: increase observability, do whatever it takes to see what is actually going on. There are basically two ways to do that:
1. use logging (add print statements documenting the steps (i.e. adding to result, recursing, ...)
2. use the debugger.

For a lot of problems, including this one, I prefer logging, as that results in an organized report of what went on, whereas debugging shows only snapshots (like a frame-by-frame movie).
Luc Pattyn 13-Feb-21 14:10pm View
Count me out. It sounds much like a new virus vector...
There is a reason why autorun was turned off for flash drives and the like!
Luc Pattyn 13-Feb-21 13:03pm View
I tend to make people think, and hopefully learn...

Luc Pattyn 13-Feb-21 8:06am View
You haven't told us what the data types are (in database and in code).
My best guess would be OrderNo is a numeric field, intended to hold an order number; so
cmd.Parameters.AddWithValue("@OrderNo", txtOrder_No.Text);

is not OK, as you are providing a string.
If so, use int.TryParse() to convert your textbox(?) content to a number.

Luc Pattyn 11-Feb-21 21:25pm View
So overlap is allowed; how could it result in a lower minimum??? I don't see a need for an overlap.

Some test cases fail? Then give us one of those!
Luc Pattyn 11-Feb-21 8:10am View
I'm a duct tape expert after all. :D
Luc Pattyn 9-Feb-21 13:13pm View
All whitespace gets accumulated and replaced by a single space, that is a feature of HTML. There are four ways to avoid it:
1. use PRE tags as Richard pointed out
2. use   instead of a space (i.e. modify the source)
3. don't use HTML
4. don't ever put two or more spaces in a filename (I tend to avoid any space in filenames!)

BTW: why would your tool need to use the result of HTML; the HTML source itself contains the double spaces, so parse that, using the HTMLutility pack or something similar.

Luc Pattyn 9-Feb-21 12:19pm View
You can't loop HTML code like that, put all the loop stuff inside PHP, like so (using period to concatenate strings, and semicolon to terminate PHP instructions):

foreach ($files as $file) {
echo "<tr>";
echo "<td>".$file['id']."</td>";
echo "<td>".$file['name']."</td>";
echo "</tr>";

Luc Pattyn 8-Feb-21 22:00pm View
Your specs are incomplete:
- if your program is ran twice, should it generate the same series of results, or not?
- if your program would have a second for loop, identical to the one already present, should the second loop generate the same series of results, or not?

And is there some upper bound to the number of values involved, i.e. what is max(to-from) ?

Luc Pattyn 8-Feb-21 20:53pm View
No need to repeat your question. If you want to expand or correct your question here, simply click "Improve question" and edit...

Luc Pattyn 8-Feb-21 20:42pm View
Maybe the matrix could be organized as a collection/array of columns, rather than a collection/array of rows?

You might get a more tailored answer if you were to provide more context: what is the overall application about, what libraries or operations are involved, why would you want to move columns around, etc...

Luc Pattyn 7-Feb-21 21:54pm View
When the available methods don't satisfy your requirements, you'll have to create your own. I would suggest you use Directory.EnumerateFiles, and for each file returned, you check its existence in the destination, and copy when appropriate using File.Copy.

Beware, whatever I/O methods you use, you should always set up a try-catch construct and handle possible Exceptions (at least log or display the entire Exception.ToString() result). Doing so would also have indicated why FileSystem.CopyDirectory didn't perform what you expected.

Finally, file path operations are best handled by the Path class; e.g. its Path.Combine method is quite useful.

PS: the FileSystem class is rather VB.NET specific, the classes and methods mentioned above are more general, they are the normal way to do file I/O in C#, but work equally well in VB.NET; anyway, you may have to read up some more...
Luc Pattyn 7-Feb-21 21:06pm View
The third parameter is a boolean flag named overwrite; you pass True and complain files get overwritten. Have you tried setting it False?

Luc Pattyn 7-Feb-21 17:33pm View
Sunflower seeds? Did you run out of kopi luwak? And they didn't notice?
Luc Pattyn 7-Feb-21 17:18pm View
There's only a few episodes per series, and they keep importing ex-wives, forgotten siblings, tourists, fresh nobility, strange cultists, and more potential victims, so Midsomer is doing well actually.

Top Expert? that is hard to believe, I only answer a few questions, I tend to pick the ones that I know something about AND show some potential. Which doesn't add up much. So I was quite surprised a few weeks ago when I received an email announcing another MVPship... Things must go really badly.

But you Sir, you're a rock.

Luc Pattyn 7-Feb-21 16:44pm View
If you're not sure it can be done, how can you even begin to hope you could do it???
Luc Pattyn 7-Feb-21 16:18pm View
That's Tom Barnaby's twin brother, isn't he?
Luc Pattyn 7-Feb-21 15:50pm View
I dunno.
The one that encourages reflection?
The one that interrupts and spoon feeds?
The one that provokes a non-stock reply?
Luc Pattyn 7-Feb-21 14:49pm View
+5 good cop bad cop routine often works wonders...

Luc Pattyn 7-Feb-21 12:29pm View
May I suggest more facts and less think. :D
Luc Pattyn 7-Feb-21 12:09pm View
If by "picture element" you mean a PictureBox, I would advise against that silly Control; just use a Panel and draw its content yourself.

I hope you measured the time your library takes to analyse a single picture.

And as long as you work frame by frame, this is a situation where you absolutely must pipeline the whole process; which means using threads and proper synchronisation.

Finally, there are lots of nice articles on CodeProject, maybe you'll like some of these.

Luc Pattyn 7-Feb-21 11:56am View
IMO the deletion query should use the same WHERE clause as the discovery query, hence selector, no email.

Your second $stmt->execute() belongs inside the if($stmt = $pdo->prepare($sql)) block.

Your fix is not good. The problem may have disappeared, the code is not OK.
As Richard pointed out, datetime values in a database should use the appropriate type. Strings are for arbitrary text, a datetime is a very structured thing; a database knows how to compare datetimes; whereas string comparison does not understand e.g. month names.

With a datetime field, you could replace most all of your code with a single query, saying delete all records where code and selector match AND expires is less than NOW.

Luc Pattyn 3-Feb-21 10:05am View
You don't need the value of 99999999! to determine how many digits it would have; all you need is its logarithm, say in base 10. And taking the logarithm of a product boils down to summing the logarithms of its factors, whether you apply it to the definition of factorial, or to the Stirling formula...

Something like this
public int calcNumDigits(int max) {
double numDigits = 0;
for (int i = 0; i < max; i++) {
numDigits += Math.Log10(i+1);
log("" + max + "! has " + numDigits + " digits");
return (int)Math.Ceiling(numDigits);
suffices to get to the result: 756570548 decimal digits.
Luc Pattyn 3-Feb-21 10:01am View
I'm glad your calculator is happy.
BTW the logarithm of a product equals the sum of the logarithms of its factors.

Luc Pattyn 3-Feb-21 9:59am View
Indeed 99,999,999! has some 756570548 decimal digits.
You could store it as a string, consuming 756MB (C) or 1512MB (C#) of memory; or as an int32 array with 6 digits per int, requiring some 500MB of memory.
I don't see a problem.

Luc Pattyn 2-Feb-21 21:00pm View
Some kind of BigInteger scheme is being used, built as an array of ints where 4 (actually 6) digits of the big number are stored in each element. So it works like a base 10,000 (actually base 1,000,000) representation of integers with arbitrary range, assuming MAX is chosen appropriately.

Luc Pattyn 17-Jan-21 8:20am View
Luc Pattyn 14-Jan-21 22:09pm View
Did you check what is inside your query string?
Is Year a numeric or a textual field? and Section?
Why do you have a ";" in the one query and not in the other?

suggestion: as long as it doesn't work, add a textbox (or label) that permanently displays your query string and set it before you execute the query. Make sure to use a monospaced font such as Courier New.
Luc Pattyn 14-Jan-21 6:04am View
Luc Pattyn 10-Jan-21 16:49pm View
Excellent answer, the only one mentioning stack.

How about a compiler option to automatically nullify all references that go out of scope?

Luc Pattyn 10-Jan-21 9:08am View
When all objects are different and have a predetermined destination, the situation is completely different. Whatever their number I would keep a current position Pi and a destination position Di for each of them, and then minimize the function SUM((Pi-Di)*(Pi-Di)).

Remark: in general you can choose many different improvement or cost functions, and many of them will work well assuming they indeed yield an extreme value (minimum or maximum) on the ideal solution AND hopefully tend to monotonically evolve towards that extreme as your system nears the state you're after. A sum of squares is known to be pretty good in most cases.

Luc Pattyn 6-Jan-21 17:56pm View
That looks like a wandering bracket; be careful, they tend to be dangerous.
Luc Pattyn 6-Jan-21 12:11pm View
Your instance of big class holds an array of type normal, whoch gets dimensioned to 10 (i.e. 10 times a null pointer) but not initialized to real objects as you did not call setup_big() at all.

You must either call setup_big() inside your Main method, or better call it inside a constructor of the big class.

Luc Pattyn 6-Jan-21 12:06pm View
If you created, positioned, and supplied properties to the form and the ZComboBoxes by the Visual Designer, then it is a mistake to immediately replace those ZComboBoxes with new ones, which will have default properties (such as Location zero,zero) independent of what you did while using Visual Designer.

Luc Pattyn 27-Sep-20 20:11pm View
It is rather hard to create a triangle having two but not three sides with zero length...
Luc Pattyn 4-Sep-20 19:21pm View
That is horrible code.

1) whoever stores passwords as plain text should be fired right away.

2) for a single login, you think it is wise to read all known users from the database?

3) You don't need DataAdapters and DataTables for a simple read.

4) Objects that have a Dispose() method should be disposed of when done e.g. SqlConnection.

5) why would the user attempting to log in happen to be in the first row???
Luc Pattyn 4-Sep-20 19:08pm View
Thanks for your code. Do you have a question?

I do: If you happen to get such an exception, what is it your program is doing at that point? Are you killing a thread, closing the port, exiting the program, or what?
Luc Pattyn 14-Aug-20 5:54am View

ignoring exceptions like that is a horrible idea. They offer the first clue when things go terribly wrong, so always make sure you see them.
Luc Pattyn 12-Aug-20 18:26pm View
I disagree, he only wants 10 different results from the random function, the array holds 10 cells and 11 walls surrounding those 10 cells.

It is his comment that is confusing, should have been
return (rand() % 10) * 2 + 1;   // return an odd random number in [1,19]
Luc Pattyn 12-Aug-20 18:02pm View
IMHO it would be easier to keep the logic and the display of the game separate.

In the logic, you have an array of cells, each having only a few possible states; you could model that with an array of integers.

When displaying, you would then have to iterate that array and build a string from substrings (" | " and "*").

And if you use (a) some constants/defines and (b) bracket-style array accesses, everything becomes much more readable. as in:

#define EMPTY 0#define STAR 1#define BAR 2#define CELLCOUNT 10int array[2*CELLCOUNT+1];char mystring[4*CELLCOUNT];for(int i=0; i<=CELLCOUNT ; i++) array[2*i]=BAR;
for(int i=0; i<CELLCOUNT ; i++) array[2*i+1]=EMPTY;array[1]=STAR;mystring[0]=0;for(int i=0; i<2*CELLCOUNT+1; i++){    switch(array[i]) {        case BAR:            strcat(mystring, " | ");            break;        case STAR:            strcat(mystring, "*");            break;        case EMPTY:            strcat(mystring, " ");            break;    }}
Luc Pattyn 11-Aug-20 8:05am View
Your formulation of the problem is quite unclear.

1. You display a number of items, please explain what they are. You work with checked items, what are those? and while removing, you remove the items form the display. What does that mean?

2. It is unclear which items get shown, and which gets checked. Especially how that differs between your first and second run of the program.

3. It would help if you described an actual scenario, such as: initial list contains A,B,C,D with B and C checked, then I ... etc.

4. You show a single method, which first adds items to the database and then removes the same items. What purpose would that serve? and how would that support your statment about deletion in a second run, as there is no code shown that only tries to delete...

5. It would help if you were to show real code.

6. As Garth already mentioned, you are ignoring the return value of ExecuteNonQuery. I would suggest you:
- count and display the number of checked items
- accumulate the return values of ExecuteNonQuery while adding, and display that
- accumulate the return values of ExecuteNonQuery while attempting to delete, and display that number.
- and NOT display every single item you try to delete.
That way you should see only a few numbers, and they should all match; when they don't, you get a first hint as to what is wrong.

7. If adding such observability isn't sufficient, now is the time to start using the debugger.

8. Finally I have several comments on your coding; these probably won't affect the program behavior but improvement is welcome:
- there is no need to open and close the connection for each iteration in a foreach loop;
- code looks much nicer if you apply a using statement for objects that need being disposed of when done, such as your connection.
- I don't like your OfType<string>().ToArray() trick allowing you to remove items while still enumerating the checked items. A simple .ToList() should be sufficient; personally I do this with an extra foreach and an intermediate list, think things-to-remove-after-foreach-is-done. That takes a bit more code but is clearer.
Luc Pattyn 11-Aug-20 5:16am View
Brutally honest? you're way too kind, another career path would be advisable.
Luc Pattyn 29-Jul-20 7:35am View
Thanks, you improved your solution so much I had to five it

Luc Pattyn 28-Jul-20 17:27pm View
Yeah, I avoid this QA section as much as I can, i.e. I read stuff here but I avoid getting in myself; that is, until something pops up that really interests me or puzzles me. And then I try to gain some insight in what is being discussed, which often is hard...

Multiple screens triggers a number of questions (last time I did that is maybe 20 years ago):
1. how do you get your app to automatically start on the screen of your choice? (if you have to move it around manually the shown handler code wouldn't be much help, would it?)
2. if, in the code you've shown, the Form ("this") initially isn't flush left, wouldn't setting its width to screen.width make it extend beyond its screen, flowing into the next screen if any? unless you also do Left=screen.Left; of course...
3. and if you somehow took precautions to get it flush left from the start, couldn't you apply the same trick to also get the width correct immediately, rather than fixing it once shown?

Luc Pattyn 28-Jul-20 13:55pm View
Hi OG, I'm a bit surprised you do this in the Shown event handler, as this would result in a second layout (recalculating docks, anchors, etc) and a visible size change.

I tend to do such things in the Forms constructor, right after calling InitializeComponents; and typically based on SystemInformation.WorkingArea

This has served me well, BTW my systems are single screen, maybe you are handling it differently in order to support multiple screens?

Luc Pattyn 24-Jul-20 10:21am View
A square root inside a simple comparison typically can be replaced by a single multiplication. Yes, this results in many more multiplications but these are very cheap, I've never seen the float/double approach be faster than the pure integer approach, but feel free to prove me wrong...

PS: most numbers aren't prime; the inner loop will most often end with a small divisor (2, 3, 5 or 7), long before the square root has been amortized...

PS2: and IF one needs sequential square roots, their values are known to be rising monotonically, more specifically each square root equals the previous one plus either zero or one, no need to calculate them from scratch each time...

Luc Pattyn 8-Jul-20 12:26pm View
Yes I know, I also see comments and replies come and go all the time. I'm giving up on this Q&A department. I didn't like it in the past, decided to give it another go now, and arrived at the same conclusion.

Goodbye to you all.
Luc Pattyn 8-Jul-20 12:12pm View
As I said before, look at the RTF specification and experiment.
Google brought me this showing there are many possibilities (as always with anything Microsoft) and the story never ends.
Luc Pattyn 8-Jul-20 11:14am View
OK I suggest you start reading this.

And then you will have to manipulate the rtf string (Text property); you either need to locate, read and fully understand the RTF specification document, or experiment with a few RTF documents you create yourself using WordPad (avoid Word if you can, it always generates a ton of code! Unless you have to support that too...).

I once wrote code that adds an image to an RTF document; it used a Bitmap and some native code to get that converted to MetaFile. I don't have the code for the way back.
Luc Pattyn 8-Jul-20 10:17am View
What do you mean by "YouTube"?
Oh, is that the teen version of "TikTok"?

Actually a parrot clip would fit well on the page you referred to...
Luc Pattyn 8-Jul-20 10:05am View
I suggest you run your code on a screen that shows a large (background) image, so you can see which (wrong) part of the screen you are actually capturing; that would make it a lot easier to figure out in what way your coordinate manipulations are failing...
Luc Pattyn 8-Jul-20 10:00am View
great link
Luc Pattyn 7-Jul-20 22:38pm View

I don't speak Python, but my Google foo is OK.
I was able to find this example which looks rather similar to your code.

There are some differences in parameter handling: they don't have any **kwargs (I don't understand what that is doing in your code), and the __init__ of derived classes calls super().__init__(...) which seems quite logical.

Hope this helps.
Luc Pattyn 7-Jul-20 19:21pm View
if you want an unmodified opencv, then pre-built libraries seem to be the way to go: see here.
Luc Pattyn 7-Jul-20 19:07pm View
I suggest you read the articles I referred, and if they don't help (I didn't check them) apply some more google foo.

There must be ready-to-use libraries for Windows, have a look here.

The alternative is to create a new project yourself, inside VS, and then add every source file to the project, by right-clicking the project in the "Solution Explorer" and choosing "Add"/probably "Existing Item". That could be hard if you have more than a few files though.
Luc Pattyn 7-Jul-20 18:50pm View
You should try File/Add/Existing Project
That would allow you to add an existing Visual Studio project.
If the new project stems from an older VS, it will ask you if it can update the project; if the new project is newer than VS, it will probably refuse to do it.

If your project isn't built with Visual Studio, it would probably not work in general. However for opencv you should look around, maybe this article or this CodeProject article helps.
Luc Pattyn 7-Jul-20 15:48pm View
Maybe not everything can or should be solved in a single line of code.

This article will show you a possible way: JSON APIs.

Luc Pattyn 7-Jul-20 11:13am View
Why do you insist on calling it an array, you said yourself it did not start with a bracket.
Do you know at all what JSON is? did you read the documentation on json_decode?
do some research, till you do I will not answer anymore.
Luc Pattyn 7-Jul-20 11:02am View
This was your fifth question.
Each and every time "what I have tried" was answered by something along the line of "Searched in google but didn't find a solution to the problem"

You show no code, no error messages, nothing. No sign of you really trying anything.

And now, you tell me the code I gave you, or some variant thereof, "works". What I want to hear is you did some research and now understand why and how it works. Then you would have made some progress, and be better prepared to answer your own questions...

If you are new to programming, or new to a language or some specific aspect of software, then start by reading some chapters in a book on the subject. Make an effort, try to learn. We are not here to spoon feed you.

Good luck.
Luc Pattyn 6-Jul-20 17:53pm View
I agree, when I encounter a "smart" input field, I run away full speed.

I don't want someone/something changing what I type, and certainly not while I'm typing.

And how about I try to correct a typo and hit backspace/delete? Do I have to take into account spaces or other delimiters I never entered? It is all horrible.

Format AFTER input not during.
Luc Pattyn 4-Jul-20 12:57pm View
Well, the let-us-do-your-homework service is making good use of CodeProject!
So we do the work, they cash in.

And I'm not sure I'll be here much longer, the average level of question content, clarity, and effort are more disconcerting than ever...

BTW: I think I have a code gem that solves Jane's phone number problem; I will wait a month or so before publishing it here.
Luc Pattyn 4-Jul-20 12:25pm View
At least he is honest about it...
Luc Pattyn 4-Jul-20 12:01pm View
you're welcome
Luc Pattyn 3-Jul-20 0:03am View
Did you run your code? with different inputs?
did you carefully read the assignment? did you fulfill all requirements?
Luc Pattyn 2-Jul-20 22:15pm View
Yep. I'm still somewhat active here, more on C# forum though.
Luc Pattyn 2-Jul-20 20:34pm View
FYI: I recently learned Windows 10 does it differently: the taskbar icon does not depend on the application icon, instead it matches the one in the currently active form.
Luc Pattyn 1-Jul-20 16:06pm View
You could start by getting hold of the document that specifies the requirements in the country of interest... Each specification in such document will require you to perform some operations on the image and then get a go/nogo result.
Luc Pattyn 30-Jun-20 14:47pm View
I suggest you read what MSDN says about Enum.Parse, the official documentation page includes an example. Learn something new every day...
Luc Pattyn 30-Jun-20 11:43am View

That is quite original. Not sure what your intentions are, I am sure there is a better way of getting there.
Luc Pattyn 29-Jun-20 17:31pm View
1. I don't understand your code. There is a rather early
and a dozen lines below that I see
receiptds = new ReportDataSource("DataSet1", dc.Tables["dtSold"]);

2. I strongly recommend you apply nested using statements for everything disposable (conn, cmd, ds, whatever) so everything gets scoped properly and gets disposed of automatically at the right moment.

Luc Pattyn 28-Jun-20 14:44pm View
You're welcome.

Performing an operation on all controls of a certain type is doable with a foreach loop on Controls, something like:
foreach (Control c in Controls) {
    Button b=c as Button;
    if (b!=null) b.SomeProperty=SomeValue;

If your form is somewhat hierarchical, e.g. some Buttons are located inside a container (such as a Panel) then in general you need recursion to handle that.

Luc Pattyn 28-Jun-20 13:16pm View
Indeed, something seems wrong.
The BackColor properties are fine, it is only the way the "modified" buttons are painted that deviates. Also the hover color is way off.
And it misbehaves with or without VisualStyles enabled. ???
And it is the same with good old VS2008...
And also on Win7

One way to get rid of it is to select FlatStyle.System (by default buttons are FlatStyle.Standard) whatever all that might mean.
Luc Pattyn 23-Jun-20 9:48am View
so you changed something, did not tell us what, and got a new error.
Maybe you 'fixed' what was correct?
Do you actually know PHP at all?
Be precise, show code (with linenumbers; your original post got edited so you can see how that is done) and tell us its behavior.
Luc Pattyn 22-Jun-20 20:32pm View
That would work for any form, as long as you put a single line in its constructor (or its Load handler):

Icon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location)

which basically says: set this form's icon to the icon associated with the file whose path I'm giving you, which is actually your own EXE path. Extracting an icon that way is rather expensive.

Two ways to improve:

1. a simpler line would be

Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath)

which avoids reflection but still scrutinizes the file looking for the app icon.

2. If you set both app icon and mainform icon to the icon you want then it is easy and inexpensive to copy that icon from mainform to any other form within the app.

And that is what I usually do by defining a "BaseForm" that holds all my form preferences (sizing behavior, icon, styles, ...) from which I inherit all other forms.


PS: I hope you found in one of the older replies my confirmation on older Windows versions always showing the app icon in the taskbar.
Luc Pattyn 22-Jun-20 17:20pm View
I think so; I used to store my icons and the like in the top folder of the project; a few years ago, I switched to putting such things in the Resources folder, even when I don't always use them as real resources, in the sense of

Assembly assembly=Assembly.GetEntryAssembly();
using (Stream myStream=assembly.GetManifestResourceStream(resourceName)) {
Luc Pattyn 22-Jun-20 17:09pm View
I'm getting a feeling Windows behavior has changed, probably with Win10 (but could be earlier, I never noticed it because I always use the same icon everywhere in an app).

It now seems the taskbar will show the icon it finds in the active form (Main Form, or an open Modal Dialog), which surely makes sense, as it will depend on which form/dialog is open.

The app's icon remains relevant e.g. when you create a desktop shortcut to your EXE.


I created a small test app with a main form and a dialog; app, mainform and dialog all using different icons. On Win7, desktop and taskbar always show app icon. On Win10, desktop shows app icon, taskbar shows icon of active form, i.e. first mainform, then when I open the dialog taskbar shows the dialog icon.

Luc Pattyn 22-Jun-20 16:58pm View
The icon I'm using today can currently be found on my website:

You can download it using "Save image" or whatever it is called in your browser.
Luc Pattyn 22-Jun-20 16:46pm View
Don't ask me why Microsoft did what they did, I don't know, I'm not sure they do either. :)

The way I go about that is: use Visual Studio, open a Form in View Designer, then choose properties, and one by one toggle everything boolean and watch what happens.

One of the strangest things in Windows land is they provide a lot of documentation in textual form but most often refuse to provide images that would make things much clearer. Well, in the end you learn to live with the way it is...

Luc Pattyn 22-Jun-20 16:42pm View
Icons have always been a bit tricky. They evolved from a single image to a set of different sizes and color depths. The one used by the taskbar button will be different from the one used by the desktop Explorer (which itself depends on some choices about icon sizes on the desktop) etc.

I tend to provide an icon file that only holds a 32*32 pixel icon and I use it for both the forms (in WinForms) and the apps, normally that works well for me. If you feel a need to stuff multiple sizes/depths in one ico, make very sure they all look pretty much the same, or you will be in for surprises. What you certainly should not do is take an existing ico and replace one of its images but not all of them.

AFAIK the folder the ico file is in does not really matter; what may be relevant is the properties you give that ico file in Solution Explorer; I tend to use either "content" or "embedded resource" (Not sure what it all means for icons), and "do not copy".

Suggestion: try a few things, if all else fails try again with an icon file from a different source.

Luc Pattyn 20-Jun-20 16:58pm View
And what is the problem? Does your code compile without errors? Does it run at all? Be specific if you want something. If necessary, show us relevant code.

Yes making it much faster will require some rewriting, there is no magic bullet...

The official documentation in MSDN gives you everything you may need, such as

And this site offers some articles that explain it more, and make good use of it. This may interest you:

Luc Pattyn 12-Feb-20 14:44pm View
Sorry, that code can not be shared, it is not entirely mine to give away.

Luc Pattyn 27-Oct-19 18:07pm View
Actually lakh is an Indian word meaning "one hundred thousand".

Luc Pattyn 25-Oct-19 12:55pm View
Improve the obervability of your code and run it for 1 file only.

Step1 making sure you get the lines as expected (as opposed to all in 1 line e.g.):

Dim lines() As String = File.ReadAllLines(backup)
MessageBox.Show("read " & lines.Length & " lines")      ' Add this line

Step 2

Add counters counting number of starts recognized, stops recognized, and lines copied; and display those when done generating the output file.
Luc Pattyn 24-Oct-19 10:50am View
I ran your first code block (for a single file, without the allFiles loop) and it worked.
I set filename="abc.txt" which contained some text and your markers;
and I found the stripped result in "abc.txt.bak"

Calling something a backup and then modifying it on purpose makes no sense, but it works.
Luc Pattyn 22-Oct-19 17:19pm View
Hmm. I don't like your formatting much (spacing, indentation), it just makes it all very hard to read. I did notice a <php{ where IMO a space would be required in between. You also have an if(count without a $. I doubt these would generate the error you're getting, but then the problem may well be in the included file. A missing } is what I suspect.
Luc Pattyn 12-Oct-19 17:37pm View
My approach is correct.

I have the following page:

$arg = NULL;
if (isset($_GET["arg"])) $arg = $_GET["arg"];
if ($arg===null) echo "arg is absent";
else echo "arg=$arg";
echo "<br>";
if ($arg) echo "arg is non-zero (this will show only for non-null non-zero)";


which you can try at

It does what it must do. My guess is you are performing bad comparisons on your variable. Read the PHP doc on how null, numbers (zero and non-zero) and other data are converted into true/false!
Luc Pattyn 12-Oct-19 12:09pm View
Ah, so I'm glad to see you are getting the output you think you should be getting...

Luc Pattyn 12-Oct-19 11:23am View
2 ton of bricks? What happened to your 3D printer plans?
Luc Pattyn 3-Oct-19 11:59am View
we now can close down Q&A!
Luc Pattyn 1-Oct-19 23:01pm View

your code won't compile, there is no SerialPort.Write() overload that accepts a numeric value. What you probably want is sp.WriteLine("290200");.

Luc Pattyn 24-Nov-17 20:52pm View
Hi OG, your solution is fine for a specific and existing file ("a text file" in the title), not for files with ".txt" extension ("text files" in the question's body). Then PInvoking AssocQueryString() would probably be the right way to go.

Luc Pattyn 10-Apr-17 13:32pm View
use Directory.CreateDirectory(pathName) and Image.Save(fullPathToFile) methods; and use a try-catch block with error reporting!

Alternatively, create the folder manually, and just Save.

If you can't get it to work, show exact code and provide details about what happens.

Luc Pattyn 6-Jun-16 20:22pm View
Hi Griff,

My C# desktop applications just started having trouble sending emails using gmail; seems Google has enabled OAuth 2.0 now, and a simple credential isn't sufficient any more.

I'm eager to read a nice article on how to do OAuth 2.0, still googling though... Any suggestions? Or spare time?

BTW: of course one can change the gmail account to "Allow less secure apps" but that doesn't sound like the right way as it most likely disables OAuth 2.0 all together.


Luc Pattyn 3-Mar-12 9:35am View
Reason for my vote of 3
Hmm. I had to read the code several times before I could guess what this was about. Some more explanation would be welcome.

I do have two comments:

1. a case-insensitive string compare is better handled by string.Compare, the code shown creates two new objects nobody really wants.

2. Some file systems are case-sensitive (typically UNIX based ones), so if you mount one of them and map them to a drive letter, a copy operation such as COPY abc.txt ABC.txt will silently be swallowed by your code??

Luc Pattyn 28-Jan-12 7:46am View
plagiarism + bizarre title + image not showing + wrong tag + ...
Luc Pattyn 11-Jan-12 3:34am View
Actually I'm not surprised other alternatives are faster; it is all about how often the string is scanned, and my compact approach scans thrice: Length, Replace(=copy), Length.

Your original code is bound to be the fastest as it scans only once, and does so at the lowest level, i.e. inside IndexOf, which I trust is doing a good job at it.

Luc Pattyn 4-Jan-12 15:58pm View

Luc Pattyn 28-Nov-11 4:54am View
if (testSubject is UltraTabPageControl) {
aPage = testSubject as UltraTabPageControl;

such is-as pattern, while very readable, is wasting CPU cycles as the type checking is executed twice. The better apporach is:

aPage = testSubject as UltraTabPageControl;
if (aPage!=null) {


Luc Pattyn 5-Nov-11 19:22pm View
Slightly different, taking advantage of some binary operator, thus eliminating the need for divisions or modulo's:

public static string SwapCharPairs(string input) {
int length=input.Length;
int lengthEven=length&-2; // -2 has all bits set except bit 0
StringBuilder sb=new StringBuilder(length);
for (int i=0; i<lengthEven; i++) sb.Append(input[i^1]); // ^1 toggles bit 0
if (lengthEven!=length) sb.Append(input[lengthEven]);
return sb.ToString();

Luc Pattyn 10-Oct-11 21:55pm View
IMO if (textBox!=null) would be adequate, no need for object.ReferenceEquals()

Luc Pattyn 29-Sep-11 8:08am View
Reason for my vote of 3
Controls can form a hierarchy as in a TextBox inside a GroupBox inside a Panel inside a SplitContainer. So you need recursion to do something to all (or some) of them.
Luc Pattyn 24-Sep-11 23:04pm View
Not quite. Two examples:
1) If the textbox contains 214748364, then typing 7 at the RHS is OK (results in 0x7FFFFFFF), however typing it at the LHS clearly gives an overflow which your code is not detecting.
2) If the textbox contains digits only, then typing a minus sign at the LHS is OK, whereas your code will reject it.
Luc Pattyn 21-Sep-11 12:55pm View
That is incorrect, your code is assuming the caret is at the RHS, which isn't necessarily true. It's better to wait for the TextChanged event, and check there. Otherwise, you have to compute the new string under all circumstances, including paste and cut-paste.
Luc Pattyn 14-Sep-11 16:36pm View
The cross-thread limitations are not specific to CF or a particular version thereof, they are present (and basically always the same) for all versions of the Framework. I described the whole shebang here.
Luc Pattyn 3-Sep-11 11:05am View
Hi Phil,

version 2 is OK IMO, and I like it!

there is just a minor mistake: "The call to KeepAlive simply tells the Garbage Collector..." is not completely accurate; it really instructs the compiler to keep a reference (and hence the object alive), as it will be used as a parameter (to an empty method, which hasn't been executed yet at the point where you were risking of seeing the object killed). Any which way you choose to keep a reference around is fine; calling Dispose() is, however if that is not what you want, GC.KeepAlive() is a nice way without any side effects (while it is an empty method, I assume it has some attributes to make sure it doesn't get optimized away).

And finally, I think "Treat the execution of a finalizer as a bug" is too strong a statement; finalizers can be put to good use (I don't have a simple example around though, I typically seem not to need them); however if all they do is call Dispose, then yes, you should try and call Dispose directly if at all possible.

Luc Pattyn 31-Aug-11 18:02pm View
Thanks Walt. I do like compact code, unless readability would suffer.
Luc Pattyn 31-Aug-11 12:00pm View
Luc Pattyn 29-Aug-11 14:38pm View
Me again, I now read one of the MS pages your article referred to. And I fully agree with what MS says there: GC.KeepAlive is one way to make sure the object's reference has to be kept around (and hence the object remains alive) until after your native call returns. They did not name some alternatives, such as: return b;, or Console.WriteLine(b.ToString()) or b.Dispose(). And they certainly did not say b.Dispose() is not sufficient!
Luc Pattyn 29-Aug-11 14:24pm View
Hi Phil,

0. yes I am familiar with the internals of compilers and garbage collectors, I have been involved in several projects creating such, albeit some time ago and not .NET specific.
1.I hadn't read the article until now; now I have, and I don't trust it. It doesn't give me any proof of what is claimed.
2. Your statement "The compilers analyze the code and determine that a particular object is collectable beyond a certain point" is completely false; a compiler analyzes source code and generates either IL/MSIL or native code. It doesn't worry about object survival (except for scope checking while accepting your code), and in particular it does not instruct the run-time about killing or keeping alive objects. That is handled entirely by the garbage collector; and then the GC isn't interested in your code at all, it only looks at the data (static classes, stacks, CPU registers, and the like); and when in doubt, it has to take the conservative approach and not drop objects when in doubt.
3. This is what neither the article nor you have mentioned: objects may be MOVED in memory by the GC (a GC pass will "compact" the regular heap, by moving all surviving objects to one end, reducing the possible fragmentation risks). Managed code doesn't care about that, it copes automatically. However, pointers passed to native code may become invalid. There are some remedies, one is automatic pinning, which may or may not work reliably, I just don't know, I have never really trusted it. I most always use explicit pinning through the GCHandle class. I don't know how you could pass a pointer without automatic or explicit pinning, the only way I can see things go wrong for sure is when your native code would continue to use the pointer after the function returned, which cannot possibly be remedied automatically. That is where GCHandle is necessary. I wrote two articles that touch the subject, which you can find here and here
Note: if my #3 is relevant to your situation, then GC.KeepAlive() helping out would puzzle me, all it does is offer a way to refer to a reference you don't want to die; the method is actually empty, you can check with a disassembler tool such as Reflector! Obviously, being empty (and not having been called yet) doesn't contribute to pinning.

Hope this helps.

I also hope the above is somewhat clear; if it isn't I strongly suggest you move the discussion to a real forum where one can use an almost decent edit page, rather than the silly 4-line edit box (extensible to a whopping 5-line!) the TT comment subsystem provides.
Luc Pattyn 29-Aug-11 8:34am View
"the use of using or an explicit invocation of Dispose reduces the probability of premature disposal ... but does NOT eliminate it."
I fail to see how that could be possible. When you have:

Foobar b=new Foobar();

(or a using construct which the compiler turns into a try-finally-dispose) then obviously b has to remain alive till its Dispose method is called, there is no way the GC could collect b in the mean time (barring a .NET bug, of which I'm unaware).
I'm afraid your observations haven't been completely accurate about this; alternatively I would be interested in an actual example that does fail in keeping such b alive when it should.

Luc Pattyn 28-Aug-11 5:16am View
I worked on some pretty complex managed/unmanaged applications (mostly without COM though), and had a similar experience once, however I never used GC.KeepAlive() to remedy it. In my experience either one of the following should suffice:
- make Foobar b a class member instead of a local variable;
- make Foobar implement IDisposable and use using (Foobar b=new Foobar()), which essentially adds a b.Dispose() statement and hence a reference that keeps the Foobar instance alive until it gets executed.

Obviously I prefer the latter as it is safer and matches the situation at hand; your Foobar objects being half native deserve to be disposable.

Luc Pattyn 22-Aug-11 23:00pm View
i = i + 4; //Do this to make it faster.

that comment made me laugh aloud, which doesn't happen very often.
If you want multiples of 5, then step by 5, not by 1, nor by 1 then by 4.
Just get the initial value corrected, like so:
for (int i=(from+4)/5*5; i<to; i+=5) ...

There is no need for a modulo operation here!

Luc Pattyn 21-Aug-11 5:57am View
VS2008 has done that for years...
Luc Pattyn 15-Aug-11 20:57pm View
WOW64 is NOT an x86 emulator; there is no need for such emulation: the 64-bit Intel or AMD chip includes one or more 32-bit as well as 64-bit CPUs, there is no need to emulate the 32-bit instruction set, registers, or programmer's model. What WOW64 does is emulate a 32-bit version of Windows, as if an actual 32-bit Windows XP/Vista/7 were present.
Luc Pattyn 26-Jun-11 19:11pm View
Reason for my vote of 2
slightly better. A few examples would be welcome, and an explanation how it works, and what external resources are being used, if any.
Luc Pattyn 25-Jun-11 19:20pm View
Reason for my vote of 1
No description, no explanation, no text whatsoever.
Luc Pattyn 14-Jun-11 7:20am View
Excellent. And the crypticism gets balanced very well by the comment lines.

Luc Pattyn 12-Jun-11 20:59pm View
Nice little function, a bit cryptic though, and probably not quite correct. 22 gives 22th.
I guess there is a bug in k = (int)(v % 100);
BTW: you don't need the variable w at all. Start setting res to v.ToString, then reduce v to v%100, later do k = (int)(v % 10), and all is well.

Luc Pattyn 12-Jun-11 20:39pm View
yep. problem solved.
IIRC you also changed the way positives get returned. Having had trouble with a leading NULL? I had been wondering about SET @minus = '' too but didn't mention it (it wouldn't compile in the languages I'm used to, I don't know how SQL treats it, my guess now is it turns it into a NULL character, i.e. a '\0')
Luc Pattyn 12-Jun-11 15:05pm View
I'm also puzzled about SET @minus = '-1', shouldn't that be SET @minus = '-'

Luc Pattyn 12-Jun-11 15:01pm View
That is the typical way to do these things, however it is the first time I see it done in SQL.

Unfortunately, it is bound to fail for the smallest possible input value. Have you tested with -9,223,372,036,854,775,808?

As you are relying on the system's int-to-string conversion anyway, why not avoid the bug and convert the original number rather than the attempted ABS value, and then deal with the possible '-' character from there?

Luc Pattyn 8-Jun-11 10:15am View
I don't want to change the functionality. All I said is: if all replacements are single chars (as in the example I gave), then, for that instance, use a string, not a string array.
Luc Pattyn 8-Jun-11 10:00am View
you seem to have missed part of the suggestion, you could still simplify: when the replacement is a single character, use a string, not a string array.
Like so:
private static string[] ReplacingCharDegree33And48 = ReplacingCharDegree16And32.Concat(new string[] { "$", "$", "£", "£", "(", "(", "¥", "¥", "µ", "µ", "Ð", "Ð" }).ToArray();
private static string ReplacingCharDegree33And48 = "$$££((¥¥µµÐÐ";
and then a simple indexing gives the one character you want.

Luc Pattyn 8-Jun-11 7:27am View
I don't know what the expected action would be.
Personally, I don't like drastic changes as they make it hard to understand what all the existing comments are about, so I would add an alternate.

Luc Pattyn 7-Jun-11 19:58pm View
Great tip.
Right-clicking a folder while holding SHIFT down adds "Open command window here" on Vista too.

Luc Pattyn 1-Jun-11 6:28am View
Reason for my vote of 1
Philippe is right.
Luc Pattyn 11-May-11 21:17pm View
I agree with your approach.
The sad thing is for any given type T compositeValues is going to be calculated over and over again, always yielding the same value. The enum type really should offer a solution to avoid that.

I will add an alternative that may remedy that to some extent.

Luc Pattyn 25-Apr-11 15:45pm View
When in doubt, go for an article I'd say. Things tend to grow anyway, some people never can get enough.
Maybe add some nice pictures, a few use cases, actual SQL code, and come up with a demo app or web site.

Luc Pattyn 25-Apr-11 10:39am View
Great tip!

BTW: assuming the username is case-insensitive, you may choose to add robustness by making the hashing case-insensitive too.

Of course not storing the password in one way or other also makes it impossible to return the password in case it gets lost. The suggestion would then be to have a mechanism for the user to provide a new password, probably by re-instating the original password, which was provided by the system, and has to be replaced by the user right away.

FWIW: As hashing is a kind of compression, an infinite number of strings may result in the same hash value; therefore if both your hashing code and someone's hash value are known, you can obtain a working password for it (a 2^32 brute-force effort) and hence impersonate him on the system. On the plus side, this isn't going to be the original password, so it won't work for other sites, assuming they use a different hashing code snippet. I recommend adding not only the account name but also some string literal that is unique for the site.

Luc Pattyn 20-Apr-11 9:09am View
OK, we're half done now. I'll use my original numbers again:
1. what I meant to say is this tip/trick should only show the conversion method; having a test bed (Form, Button, PictureBox) is nice, but should be in a separate class (and probably not shown here).
3. calling MeasureString was fine, however you should capture its return value and store it as a Size, then use its Width and Height properties. The way you had it, the code was calling MeasureString twice, so the expense of measuring the text got doubled.
5. The brushes and fonts you create need being disposed; that too can be handled by using statements.

Luc Pattyn 19-Apr-11 18:24pm View
Your code needs some cleaning up. Here are some comments:
1. why would there be a need for a Form? the Convert_Text_to_Image() method belongs in a static class (and its name should follow standard C# naming conventions)
2. why do you create two bitmaps? and two graphics, one for each bitmap?
3. why do you execute MeasureString twice?
4. you should dispose of the Graphics you create. The "using" pattern would work well here.
5. you probably want the brush color to be a parameter too; note: if you were to create a brush, you'd have to dispose it too.

Luc Pattyn 14-Apr-11 15:29pm View
"lotion programmer" doesn't sound right, does it? :)
Luc Pattyn 12-Apr-11 11:56am View
Thanks. Searching a bit you could stumble on this one :)
Luc Pattyn 10-Apr-11 4:52am View
you're welcome. :)
Luc Pattyn 14-Mar-11 17:10pm View
I fixed the < mistake, thanks. And no, I'm no longer trying to quote the exact text as it changes all the time. :)
Luc Pattyn 28-Feb-11 12:55pm View
Reason for my vote of 2
no text, no tip.
Luc Pattyn 24-Feb-11 10:12am View
I would vote that up if I knew how to do so...
And I am in favor of PRE-tag support everywhere.

Luc Pattyn 13-Jan-11 16:49pm View
I agree on the performance aspect. When error reporting comes into play, I prefer all problems reported at once, not one at a time, so I don't need multiple user iterations; nested IFs wouldn't do that for me.
Luc Pattyn 13-Jan-11 14:36pm View
I agree, however I wouldn't use nested ifs, the beaty of TryParse is you can simply AND a lot of them together, without investing lots of spaces, tabs, and curlies. :)
Luc Pattyn 13-Jan-11 12:43pm View
Reason for my vote of 5
Hi Dave. Well done. Except for the stack issue of course. :laugh:
Luc Pattyn 13-Jan-11 12:42pm View
I wonder what really happened here. The parameter is part of the Win32 API, it is not really .NET related. And it is optional, i.e. NULL is good. So I'm puzzled why it wouldn't fail on all Windows versions and .NET versions, or work on all of them. Unless .NET 4.0 here really came together with a 64-bit Winodws version, which would pass parameters differently and get confused by the value it found instead of the missing template pointer.
Luc Pattyn 10-Jan-11 8:23am View
Visual Studio does east-coasting without any problem. There are dozens of formatting options, apparently your choices and mine are different.
Luc Pattyn 9-Jan-11 14:46pm View
Wrong? The compiler is happy with it. I do that all the time, and call it east-coast formatting.
Luc Pattyn 4-Jan-11 13:19pm View
I don't see it (maybe because it really is missing??)
Luc Pattyn 2-Jan-11 12:58pm View
If you were to show only a fraction of the code, explaining it, telling us how it works and how it is special; and then provide a download application that demonstates it somehow; it could make a good article. But never a tip/trick.

PS: I added this as a separate comment because the voting tool only gives a minute edit box.
Luc Pattyn 2-Jan-11 12:56pm View
Reason for my vote of 1
Too much code.
Luc Pattyn 1-Jan-11 11:26am View
I did in the line len--;
Luc Pattyn 29-Nov-10 16:03pm View
for positive values of Days both (7+Days) Mod 7 and Days Mod 7 are equivalent, but unequal to just Days (think of Days values >=7)
Luc Pattyn 18-Sep-10 22:03pm View
Reason for my vote of 1
I'm afraid I don't agree. The tip was about the bitness of the OS, not the process.

If you build native code for 32-bit, it will always say 32-bit. If you build it for 64-bit, it will fail on Win32.

If you build managed code for AnyCPU, it will adapt itself by default; but I guess you could force it to run in 32-bit mode, hence lying about the OS bitness.

My conclusion is you cannot reliably get the OS bitness without looking outside the process.
Luc Pattyn 12-Sep-10 12:46pm View
Reason for my vote of 3
fixing vote
Luc Pattyn 24-Aug-10 20:01pm View
I object. It's unacceptable. Roman numbers use upper-case. always. no exceptions. none whatsoever.
I need a completely-disgusted icon here. In fact an entire line of them.
Luc Pattyn 19-Aug-10 8:54am View
I'd like to see some unit tests, your method seems a bit error prone. Otherwise excellent.
Luc Pattyn 20-Jul-10 13:48pm View
Reason for my vote of 2
Luc Pattyn 20-Jul-10 13:47pm View
QuarterOfDate seems quite wrong, it numbers the quarters as 4, 1, 2, 3?
all it takes IMO is return (curDate.Month+2)/3; if one wants quarters 1, 2, 3, 4.
Luc Pattyn 19-Jun-10 7:53am View
Reason for my vote of 1
The best way? it isn't even readable! It starts and ends with a failing PRE tag, not sure if this is all one snippet, or some text is embedded, explaining what this is about.
Luc Pattyn 8-May-10 14:17pm View
not going to delete this
Luc Pattyn 8-May-10 14:16pm View
going to delete this
Har! Har! I have made it available again. - Henry. :-)
And I have deleted the one you were going to leave.
Am I able to make these changes because of my site status or can anybody do this?
Luc Pattyn 8-May-10 14:16pm View
Correction: yes I can edit them, however the edit box gets preloaded with the content of an arbitrary comment, not necessarily the one I want to edit.
Luc Pattyn 4-May-10 10:23am View
Luc Pattyn 4-May-10 10:01am View
Hi John,
I'm testing the new comment facilities. Can you reply to this? If so, will I be able to react on your reply? is it hierarchical again, as forums are, or did it just go from a one-level to a two-level structure?
BTW: your first code snippet has a little HTML problem around the LT/GT signs.
Luc Pattyn 4-May-10 9:56am View
Hi Henry
just testing comment feature