Archive for the ‘ _tech ’ Category

VB6 Val() equivalent in C#

So today I came across a bug in our new system, which is written in C#.NET 4.0.

It was an upgrade from an old VB6 app, and it happened to be the way that Val() was used in VB6.

Since there is no direct equivalant, the internet didn’t provide me fast enough with a good enough method and it’s behavior is kinda odd, here’s my best approiximation in C#:

public static Double Val(string value)
{
    String result = String.Empty;
    foreach (char c in value)
    {
        if (Char.IsNumber(c) || (c.Equals('.') && result.Count(x => x.Equals('.')) == 0))
            result += c;
        else if (!c.Equals(' '))
            return String.IsNullOrEmpty(result) ? 0 : Convert.ToDouble(result);
    }
    return String.IsNullOrEmpty(result) ? 0 : Convert.ToDouble(result);
}

Works great!
(my initial Regex method was much faster, but lacked the accuracy I wanted)

PrestaShop wholesale mode fix (force catalog mode when uses is logged in)

After searching all of Google’s entries for how to make PrestaShop hide it’s prices when the user is not logged on, which is the common behaviour for dealer/wholesale stores (where by non-registered users do not see prices) I was rather disappointed by the variety of the non-elegant solutions out there.

Also, there are some paid solutions, but what’s the point of paying 50 bucks for open source applications?

Here’s my solution, it works like a charm and is a very easy fix!

In classes\Configuration.php (around line 114) it looks like this

static public function get($key, $id_lang = NULL)
{
	if ($id_lang AND isset(self::$_CONF_LANG[(int)$id_lang][$key]))
		return self::$_CONF_LANG[(int)$id_lang][$key];
	elseif (is_array(self::$_CONF) AND key_exists($key, self::$_CONF))
		return self::$_CONF[$key];
	return false;
}

change it to this:

static public function get($key, $id_lang = NULL)
{
	//Grab access to the $cookie which is already loaded in the FrontController as global $cookie;
	global $cookie;
	if ($id_lang AND isset(self::$_CONF_LANG[(int)$id_lang][$key]))
		return self::$_CONF_LANG[(int)$id_lang][$key];
	elseif (is_array(self::$_CONF) AND key_exists($key, self::$_CONF))
		//If the system is trying to find out if Catalog Mode is ON, then return the configuration setting,
		//but override it with the user logon status
		if($key == 'PS_CATALOG_MODE')
		{
			return !$cookie->logged || self::$_CONF[$key];
		}
		else
		{
			return self::$_CONF[$key];
		}
	return false;
}

Essentially, I wanted to force the system to display the “Catalog Mode” when the user is not logged in, and to turn this off when he is logged in. Instead of going through every call that checks if it’s on or off, we override the call to also check if the user is logged in.

I can guarantee this works for v1.4.3.0 and the code for the current version 1.4.8.2 (at the time of this post) has not changed, so it should work there.

If you have any difficulties with this, let me know with a comment below!

Filter GridView as you type (ASP.NET C# with jQuery)

I’ve often found that our client’s web portals have way too much data on screen, so here’s a quick and easy “Filter as you type” JavaScript/jQuery block:

function SetupFilter(textboxID, gridID, columnName) {
    $('#' + textboxID).keyup(function () {
        var index;
        var text = $("#" + textboxID).val();
 
        $('#' + gridID + ' tbody tr').each(function () {
            $(this).children('th').each(function () {
                if ($(this).html() == columnName)
                    index = $(this).index();
            });
 
            $(this).children('td').each(function () {
                if ($(this).index() == index) {
                    var tdText = $(this).children(0).html() == null ? $(this).html() : $(this).children(0).html();
 
                    if (tdText.indexOf(text, 0) > -1) {
                        $(this).closest('tr').show();
                    } else {
                        $(this).closest('tr').hide();
                    }
                };
            });
        });
    });
};

Then all you need to do, after you include the above code segment in your page head or startup .js file is to call the below for each textbox you want to actively filter your grid:

$(function () { SetupFilter("myTextBox", "myGridView", "My Column Name"); });

Just make sure you reference your controls with the correct ID. By using ClientIDMode=”Static” you can ensure it’s the same ID as you defined it in the ASP.NET markup.

<asp:TextBox ID="myTextBox" runat="server" ClientIDMode="Static" />
 
<asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="False" ClientIDMode="Static"
ShowHeaderWhenEmpty="True">
    <Columns>
        <asp:BoundField DataField="one" HeaderText="My first column" />
        <asp:BoundField DataField="two" HeaderText="My Column Name" />
    </Columns>
</asp:GridView>

You can even include the textbox as template fields in the grid header.

Make sure you are making use of jQuery libraries in order for this to work.

let’s choose!

As part of a competition, i’m making an iPhone iOS App!

The app’s name is “Let’s Choose

It is an application that establishes a live collaboration between a subscribed group which shares one common screen and provides the ability to add, delete and vote for choices.

Example A: (applicable to youngsters).
Consider a common scenario of Friday night and 5 friends deciding to choose where to spend their night.
Let’s name them Alice, Bob, Charlie, Dave and Eve. Let’s also assume they are only using SMS messages to communicate between them (to ease the example below, as actual cases may include more than 4 ways of communication).
Alice sends to Bob and suggests TGI Friday’s.
Bob replies he likes the idea.
Alice sends to Bob that she will ask the others.
Alice sends to Charlie and suggests TGI Friday’s.
Charlie sends to Eve and suggests TGI Friday’s but prefers Spur.
Eve replies she also prefers Spur but also suggests the Irish Pub.
Charlie sends to Alice about Spur and the Irish Pub.
Alice sends to Bob new suggestions.
..
.. (23 smsses later).
..
Alice is still busy sending to Eve and discussing another Pub.
Dave is hungry.
Bob is eating already.
Charlie is driving to Spur with Rachel.

Let’s now assume that they all had Let’s Choose installed on their iPhones.
Alice sends a suggestion to TGI Friday’s to a group she created a while back.
Bob gives +1 vote to that.
Charlie adds Spur.
Eve adds the Irish Pub.
Dave adds a vote to the Irish Pub.
Alice dislikes Spur.
Eve posts to the mini-wall “The Frajolistic Bloomers are playing at the pub! Vote up!”
Bob adds a moves his vote to the pub.
Bob hits “happy with this”.
Alice moves her vote to the pub.
All participants hit “happy with this”.
The top selection (irish pub) is selected.

Long story short, this application can have many applications and many cases. The simple idea that you can suggest an idea and others can choose and suggest makes everyday choices easier than verbal communication.
Choosing lunch at work. Making a shopping list with your wife while she’s home and you’re nearing the shop. Choosing your baby’s name between family. Suggesting WPF over WinForms. I can go on ;)

Comments are appreciated from all three of you who know this website exists ;)

InstallShield. *bleh*

Perhaps my title should be: InstallShield Profesional 2010: How to write your own InstallScript to handle new installs, update (or upgrade) installs and uninstalls (removal).

However, *bleh* is more like the thing I say after the word InstallShield, after spending 4 days getting it to work.

The real reason why I dislike it, isn’t the program itself, it’s mostly the lack of proper documentation. I found myself learning how to add a new property and value, (by using right click) instead of finding what %&*#@ values I can include!

Anyway, to help out those who might has less hair now, I’ll be listing parts of my InstallScript Rule and later a guide on how to setup an MSI based installation that saves the config to xml for the app to use (which can be configured during installation) and reads from that xml during update installations. (stay close for that post).

The most useful thing I found online (from FlexeraSoftware) was this image:
EventMap

Can also be found here.

Here’s a snippet of my script, which should help you get started. For some reason, mine never had any “default” scripts or “samples” so I had to lose some hair getting there. (not that I have plenty left).

//========= ALWAYS RUN ========
//this is called always, first
function OnBegin()
begin
//call 1st
end;

//this is always called, third
function OnMoving()
begin
//call 3rd
end;

//this is always called, fourth
function OnMoved()
begin
//call 4th
end;

//this is always called, last
function OnEnd()
begin
//call last
end;

//========= NEW INSTALL =======
//this is called when it's a new install, second
function OnFirstUIBefore()
begin
//call 2nd
MsiSetProperty(ISMSI_HANDLE,"APP_STATE","NEW");
NewInstall();
end;

//this is called when it's a new install, fifth
function OnFirstUIAfter()
begin
EndInstall();
end;

//========= UNINSTALL ========
//this is called when it's a uninstall, second
function OnMaintUIBefore()
begin
MsiSetProperty(ISMSI_HANDLE,"APP_STATE","REMOVE");
GetRegistryInstalledValue();
MaintInstall();
end;

//this is called when it's a uninstall, fifth
function OnMaintUIAfter()
begin
EndUninstall();
end;

//========= UPGRADE ===========
//this is called when it's a upgrade install, second
function OnResumeUIBefore()
begin
MsiSetProperty(ISMSI_HANDLE,"APP_STATE","UPDATE");
GetRegistryInstalledValue();
UpgradeInstall();
end;

//this is called when it's a upgrade install, fifth
function OnResumeUIAfter()
begin
EndUpgrade();
end;

i wrote this like ages ago and never posted it. ages, meaning ‘Last edited by admin on November 9, 2010 at 11:46 am’.
so i decided to go ahead and post it anyways. yes, and not edit it. thank you for saying that loud.