All posts by Kenji

Fixing the default of the .Net Label’s AutoSize property

This is a story about creating form applications using Visual Basic and Visual C#.

Have you ever wished that the default for the AutoSize property of labels was False? While there might be cases where you create them with AutoSize = True, in my company, we use AutoSize = False. However, it’s cumbersome to set the property every time you paste a label. So, the idea behind this code is to create labels with the AutoSize default value set to False. This has been confirmed using Visual Studio 2010 and Visual Basic.

In Visual Studio Professional Edition, you can create inherited controls. In the Express Edition, you couldn’t create inherited controls from the start, as far as I recall. In that case, you first create an inherited class.

The class to inherit is System.Windows.Forms.Label. A common approach here is to use the property window of the control’s designer screen to set AutoSize to False (though nothing appears in the designer screen, the property window is usable). However, for the Label control, things get tricky, as it reverts to AutoSize = True when actually pasted onto a form. To tackle this, display the code and add the following snippet.

Continue reading Fixing the default of the .Net Label’s AutoSize property

How to Convert Numeric Dates to Date Format in Crystal Reports

This is a guide on how to convert numeric date values to a year-month-day format in CrypstalReports. This code is useful when you have date data stored as 8-digit numeric values representing the year, month, and day.

First, create a formula field. Within it, write the following expression. To edit the expression, right-click and select “Edit Formula.”

With this, you can convert the numeric value to a date. Replace {DBColumn} with the name of your database field.

However, this will still display in the format 1986/01/06. To change the date format, you’ll need to set the properties of this formula field.

For DateFirstSeparator, DateSecondSeparator, and DateSuffixSeparator, input “年” (for year), “月” (for month), and “日” (for day), respectively. This will display the date as 1986年01月06日.

テキストファイルを分割するスクリプト

Sorry, this entry is only available in 日本語.

matriXscan というメール関連製品のログをチェックしたことがありました。 そのときに使ったテキストファイルを分割するスクリプトを紹介します。 ログのチェックはテキストエディタで開けばできるのですが、 そのときのログは 1.3 GB あり、テキストエディタでは開けない巨大なファイルになっていました。

環境

  • Windows
Continue reading テキストファイルを分割するスクリプト

VBA: ファイル名に関連した文字列操作

Sorry, this entry is only available in 日本語.

過去に書いていた VBA のファイル名に関連した文字列操作の記事をまとめました。

ファイルパスからディレクトリ名を取得する

ファイルパスから一番右のバックスラッシュの位置を探して、ファイルパスのそれより左の部分を抽出します。 バックスラッシュが見つからなかった場合(ディレクトリがファイルパスに含まれない場合)は、空文字列が返ります。

ファイル名から拡張子を取り除く

注意

InStrRev が返す文字位置は 1 から始まります。 (配列インデックスのように 0 から始まるわけではありません。)

ファイル名に “.”(ピリオド) が含まれない場合は Left の第2引数に負の数が渡るため、例外が発生します。 これを解消するには次のようにします。

Right を使って拡張子を取得することもできます。

ファイル名から拡張子を取得する

ここでは、あるところで使われていたコードを見てみます。

与えられた文字列にバックスラッシュがあればそれより後ろの文字列にして、 その後ピリオドがあればそれより後ろを抜き出しています。 ピリオドがなければ空文字列が返ります。

関係する関数

InStrRev(string1, string2[, start[, compare]])

ある文字列 (string1) の中から指定された文字列 (string2) を最後の文字位置から検索を開始し、最初に見つかった文字位置を返す文字列処理関数です。 引数 start を省略すると -1 が使用され、最後の文字位置から検索を開始します。 compare は比較するモード(テキスト/バイナリ)を指定します。省略時はバイナリモードになります。

検索文字列が見つからなければ 0 が返ります。

Left(ByVal str As String, ByVal Length As Integer)

与えられた文字列の左端から指定された長さの文字列を抽出します。 Length が 0 より小さいと例外が発生します。

Mid(string, start[, length])

与えられた文字列の部分文字列を返します。 start には先頭の文字を 1 として文字位置を指定します。

Right(ByVal str As String, ByVal Length As Integer)

与えられた文字列の右端から指定された長さの文字列を抽出します。 Length が 0 より小さいと例外が発生します。