Spurious/Incorrect .NET Form.Activated() Event 2017

Notice: Using the "fix" below can result in the form's Activated event not being fired when it is restored from the taskbar. I guess this isn't a proper solution, after-all. Adding checks for the form being minimised/restored may be a solution but the Form class is firing those events are weird times, so.,,

Notice 2: I've added another version below.

A couple of new categories for this blog: Development and .NET. I'm surprised I haven't written more on programming in general, to be honest. I think I'll have to port across my post from an older blog on the whole BadImageException problem which drove me mad for far too long.

But, let's continue with the subject of this post: the Form.Activated() event incorrectly firing on a Form.

Quite a few years ago I created my own Metro-style form based on the idea of Microsoft's flat-style interface. Metro was approximately a couple of years away from release and there wasn't really any screenshots of what it would look like. I ploughed on with my version - including a break of a year because it took a long time to get it working correctly as working with forms that have their FormBorderStyle set to None in .NET is just outright annoying. This changed in WPF, but we're using WinForms here. #winforms4lyfe

The biggest issue was getting Windows to treat the borderless form as a true window; it just didn't give a crap about it. A quick way to tell if Windows regards a form as a true window is to minimise it to the taskbar. Does it immediately disappear from view? It's not a true window. Does it animate down into the taskbar? It's a true window.

There were two issues remaining (that I remember, anyway): restoring the window from its minimised state causes the window to gain an extra X number of pixels on its height. That's still an issue and seems random as to which form it affects. The fix I put in-place seems to work most of the time, but regardless, the child controls gain extra height with/without the form growing in size. Clearly more work to be done, there.

The second issue which I'm going to talk about here is the form's Activated() event firing when the form hasn't actually been activated. I don't know why this occurs, but it's annoying and obviously unexpected.

It seems somewhat random when it happens, but here's how it generally goes:

While the form is activated, deactivate the form by clicking outside of it and then move the mouse cursor over the form without clicking; chances are the form will receive the Activated() event. Despite this event firing, the form isn't actually activated.

This causes a problem in my particular case because I handle and expose the activation event via a WindowActivityChanged() event, and I also change the form's visual style depending on its active state.

For example, here's a (currently work-in-progress) form that's active:

Active Form

And here's that same form when it has become inactive:

Inactive Form

As you can see, having the form become active when it actually isn't is confusing to the user. And extremely irritating to me.

My first quick go at fixing this seems to work; I'll have to keep testing it over the long term but it seems like it may have solved it. Not a fan that I have to do this in the first place, but what can ya do?

Have this at the first line(s) within the Form_Activated() event [VB]:

' Filter out any spurious activations.
If Not ActiveForm Is Me Then Exit Sub

Here's another version (see notice at top of this post):

If Me.RectangleToScreen(Me.DisplayRectangle).Contains(Cursor.Position) AndAlso Not ActiveForm Is Me Then Exit Sub

ActiveForm is a Read-Only property on the base Form class and contains a reference to the form that is currently active (within our application; it's not system-wide or anything!), so we're checking to see if the active form is actually me (or this in C#) rather than just blindly accepting the fact.

It appears ActiveForm is set before Form_Activated() is fired, so it does what we want.

I'm not sure if it's worth putting a check into the Form_Deactivated() event to ensure that the form is truly deactivated, but I don't recall ever running into that particular problem. I guess time will tell.


Server Going Offline 2017

Well, I'm not surprised in all honesty.

The server this and my other sites are hosted on has started to go offline for 10 - 240 minutes per day, at (seemingly?) random times.

About 15 years ago (christ, really!?) UK2 moved me from my original server when I upgraded my hosting and since then it's been going to shit. I - finally - got them to move me about within the last year to another server after constant down times, except within the last week it's all gone to shit again.

The difference is that before this server change, the sites were going offline for (typically) a few minutes a time numerous times per day around two days out of the week, but on this new server the sites are going offline for extended periods of time once or twice a day every other day.

Compared to other hosting providers, UK2 isn't actually that cheap; I haven't switched providers because there's two decades worth of infrastructure I've built-up with UK2 that I really can't be arsed sorting out.


No, I Haven't Binned This Off 2017

Still here.

I haven't posted in a couple of days because I didn't have anything to write about. While I begin working on a new program yesterday and got a bulk of it done today, I'm not going to write anything on it until it's near being released. It's more of a "tool" than a fully-feature application, but it's still useful for those wanting, er, it.


In the works: Voting 2017

A new feature is coming and that is...


Each post will have a thumbs up and down buttons. This'll allow posts to be voted on and will give me an idea of the types of posts to make more of in future - or fewer of.

It would've gone live today but I'm having a bit of a pisser of a time when it comes to actually displaying the voting results to myself. For some reason, a value of 1 is being returned for each and every post, regardless of the number of actual votes cast. Although I did receive a value of 2 at some point but that was lost when I re-seeded the database.

I can only imagine it is something to do with the count being returned for each actual post rather than the number of votes made for the posts. This reminds me, I'm going to need to do more than just return a number as I'd rather have the sum of the positive and negative posts and display that; displaying positive and negative posts separately is just a waste of space, I think.

Once this is fixed, then I'll upload the changes.


Formatting Fix for Mobiles 2017

I was just about to go to bed and then thought it would be a great idea to jump right into PHP and sort out the mobile-issue with the quote above. D'oh.

Alright, the blue quote at the top of this page seems to be a bit too long for mobile browsers and overflows out of its header bar. The solution? Don't show it on mobile browsers - sorted!

Did a quick search to see if I could just detect mobile within the User-Agent and sort it that way. Instead, I found some library that kept getting referenced and decided to just go ahead and make use of it: the Agent library, with Laravel support - hurrah.

This somewhat goes against my own lightweight way of doing things, but presumably it's not quite as easy as detecting a mobile sub-string as otherwise why would there be a dedicated library for it? In fact, don't answer that.

Integrated it and all that and then the site stopped working due to an exception. Deleted the vendors folder from the server - which took minutes - and then re-uploaded my local version as no doubt something was missing. A few minutes later - it all works. Phew. Oh; as a bonus, a load of libraries also got updated with a quick composer update, so that's nice.

A quick addition of

if (!Agent::isMobile()) {

along with a change to remove the appropriate <span> and such, and it was all done.

Does it work? No idea. Haven't actually tried it on a mobile device yet (although inversing the logic works so I'm hopeful it's all fiiine) as my phone is elsewhere in the house. But, there are no errors and so worst case is that it makes no difference.


Documentation - is it worth it? 2017

It feels like I should be writing another post as it's been a few days. While that's technically true, there's been posts every day now but they were actually written earlier in the week and published once per day with the new timed publish update.

So I'll write a quick post about documentation. Specifically, the FileSieve documentation.

I haven't done any programming for the past couple of days and so thought I should at least do something, and that something was some FileSieve docs. I remembered a thing: a couple of days ago I looked at the Analytics for the FS documentation and it appears hardly anyone even looks at it.

So, really; am I just wasting my time with it? Honestly... yes, I think I am. I started reworking the single Automation page into an entire section before this realisation and now I'm starting to wonder if there's any point? Unlike a lot of the rest of the application, automation requires documentation.

Maybe I should just shove out the required info (and not bother polishing it all up) and be done with it and spend any other FileSieve time on the application itself.


Timed Publishing? In! 2017

Woke up yesterday morning and had a thought while I was on my way to the bathroom: timed publishing doesn't need to be Cron-based. Just set a date on the post and display that post if the current date/time is past it.

It seems I got somewhat fixated on the whole Cron job method of allowing posts to go live based on a set date and time that the obvious non-scheduled way of doing things just passed me by.

I spent yesterday getting the feature in and made it live. Posts are now displayed based in their (newly added) published_at column rather than created_at. Of course I had huge amounts of fun with timezones. sigh

I gave the site a quick test on mobile after someone mentioned it doesn't work. Seems it's just a minor formatting issue, at least on Android Chrome, involving the quote in the header at the top of the page. I'll probably just not display it on mobile browsers; presumably there's a mobile substring in the User-Agent header.

Didn't quite get around to sorting out the categories display (click on a category at the top of this post) and so viewing a category right now doesn't work quite how you'd expect. I probably should've gone with keywords rather than rigid main and sub categories.

Finally, I added a set of badges to posts so I can see at-a-glance if any specific flags are set rather than having to rely on remembering what their background colours denote.

Post Flags


FileSieve Documentation 2017

I seem to be on a major productivity roll lately. I realised why, too: this new Corsair K95 RGB Platinum keyboard (US version can be seen here). It might seem like I'm babbling on about mechanical keyboards like a zealot, but I had to plug in my previous rubber-domed Logitech G19 keyboard recently and holy crap. The holiest of holy craps.

Rubber-domed keyboards are genuinely 'orrible to type on!

Since I got this new keyboard (a week or two, now?), I just can't stop typing on it. I'm having to come up with excuses to work on something just so I can use it.

The upshot of this is that my productivity is going a bit insane. Even these posts you're reading are starting to be written days in advance - that's how much I'm writing now.

In fact, I've decided to add a whole new Automation section to the FileSieve documentation!

I don't even want to imagine how much output has been gimped while having to use that previous rubber-johnny keyboard. Ugh.


VB Runtimes Pack, release 8 2017

Speaking of unexpected happenings, I got contacted a couple of days ago by someone asking me if the Visual Basic 6 Runtimes Pack was free for enterprise use (spoiler: it is).

The pack doesn't require me having to recompile anything using the Visual Basic 6 IDE, as I haven't had that thing installed in well over a decade, so I thought I'd repackage it in an updated installer.

Running the original VB Pack installer itself shows that it was littered with errors. Not errors in the files it was installing (well, maybe a couple - but more on that later), but in the instructions and license.

As with all of my software installers, there are three text documents the installer shows to the user.

There's the License that is shown at the very start which contains the licensing terms that must be agreed to before the installer will do its thing.

Second is the Before, which contains general information. In this case, it outlines what the pack is and what it'll install. Also included is the pack version and its release date, just like the other two docs.

Thirdly, and lastly, is the After. This is just a quick message letting the user know that everything is installed.

The problems are down to the site each document points to: they're wrong. As this pack was originally released way back in 2002, the URLs are pointing to my old site as opposed to here.

Another problem is the release date. Each of the three documents mentioned above have entirely different dates! No idea how that happened.

Oh, the file errors that I alluded to above. There was a note in the Before text saying that any .dll registration errors that occur can safely be ignored. While setting other properties within the installer, I noticed there was a flag for ignoring any errors that are thrown during the regsvr process. Now that flag is enabled so the errors are no longer shown, if they indeed occur at all this time around.

As well as general quality control, I wanted to get this site properly advertised in there as there is a huge amount of downloads of that pack. It seems people and companies just tell their customers to download the installer from my site. I might as well get a little bit of advertisement from it as it's not exactly like I'm gaining anything.


Timed Publishing 2017

I could do with setting a Publish Date on unpublished posts and have them go live at specified times. Except of course such functionality isn't present on this blog.

I'm on shared hosting, so I never really got into messing with Cron jobs.

I think Laravel has support for some time-based thing, but I'm not entirely sure how it can trigger itself. Must be based on visitor access - that's the only thing I can think of. If the site doesn't have visitors then the job would be missed... too unreliable in my opinion, so gawd knows.