Monday, April 6, 2009

[Delphi] Using Ampersands inside Delphi

How can I make a Window control's text property contain an ampersand character?

Use two ampersand characters. The Windows default text handling for controls interprets a single Ampersand character as meaning the next character is a hot key. Example: Button .Caption := 'This && That';

Source: http://www.delphi32.com/info_facts/faq/faq_184.asp

Wednesday, January 21, 2009

[Java] Creating JTable from Resultset

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.table.AbstractTableModel; 

public
class ResultTableModel extends AbstractTableModel {
      /**
       *
       */
      private static final long serialVersionUID = 1L;
      boolean DEBUG = false;
      private String[] columnNames;
      private Object[][] data;

      
public ResultTableModel(ResultSet rs, String[] columnNames) {
            ResultSetMetaData meta;
            if (rs != null) {
                  this.columnNames = columnNames;
                  try {
                        meta = rs.getMetaData();
                        int rowcount = 0, colcount = 0;
                        if (rs.last()) {
                              rowcount = rs.getRow();
                              rs.first();
                        }
                        colcount = meta.getColumnCount();
                        data = new Object[rowcount][colcount];
                        int i = 0;
                        do {
                              for (int j = 0; j < colcount; j++) {
                                    data[i][j] = rs.getObject(j + 1);
                              }
                              i++;
                        } while (rs.next());
                  } catch (SQLException e) {
                        e.printStackTrace();
                  }
            }
      }

      
public int getColumnCount() {
            return columnNames.length;
      }

      
public int getRowCount() {
            return data.length;
      }

      
public String getColumnName(final int col) {
            return columnNames[col];
      }

      
public Object getValueAt(final int row, final int col) {
            return data[row][col];
      }

      
/*
       * JTable uses this method to determine the default renderer/ editor for
       * each cell. If we didn't implement this method, then the last column would
       * contain text ("true"/"false"), rather than a check box.
       */
      public Class getColumnClass(final int c) {
            return getValueAt(0, c).getClass();
      }

      
/*
       * Don't need to implement this method unless your table's editable.
       */
      public boolean isCellEditable(final int row, final int col) {
            // Note that the data/cell address is constant,
            // no matter where the cell appears onscreen.
            if (col < 2) {
                  return false;
            } else {
                  return true;
            }
      }

      
/*
       * Don't need to implement this method unless your table's data can change.
       */
      public void setValueAt(final Object value, final int row, final int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
      }
}

Monday, January 12, 2009

[Delphi] String Split

function TForm1.Split(s:String): TStringList;
var
sl : TStringList;
i : Integer;
tmp : String;
begin
sl := TStringList.Create;
for i := 1 to Length(s) do
begin
if s[i] <> ',' then
begin
if s[i] <> '"' then // skip "
begin
tmp := tmp + s[i];
end;
end else
begin
sl.Add(tmp);
tmp := '';
end;
end;
sl.Add(tmp);
Result := sl;
end;

Thursday, January 8, 2009

Windows XP Service Shortcut

Do you know that Windows XP has a shortcut which can be activated by put it into “Run” menu.
for the example press “Start” and Then Press “Run“. and the dialog box will show up.

and then fill it with the service that you want “perfmon.msc”, press OK

” The Performance Monitor ” will comes up to your windows

as easy as that.

hereunder other, dissimilar example of service name which can be activated by through “Run” menu.
* Component Services - comexp.msc
* Computer Management - compmgmt.msc
* Device Manager - devmgmt.msc
* Disk Defrag - dfrg.msc
* Disk Managment - diskmgmt.msc
* Event Viewer - eventvwr.msc
* Group Policies - gpedit.msc
* Local Security Settings - secpol.msc
* Local Users and Groups - lusrmgr.msc
* Performance Monitor - perfmon.msc
* Resultant Set of Policies - rsop.msc
* Services - services.msc
* Shared Folders - fsmgmt.msc


Original from: Windows XP service shortcut: Posted by: trueheart77 | September 25, 2007

Sunday, January 4, 2009

[Delphi] Canvas Print Right Aligned Text


As you know, TCanvas don't have a method to print right aligned text. So what to do?

If the text is left aligned, it should start at X1. Now we want to start from X2, so we use the calculation above to get our X2.

procedure TForm1.Button3Click(Sender: TObject);
var
  X1,X2,W1,W2,W3: Integer;
  _text: String;
begin
  W1 := 100;
  X1 := 5;   // Original X
  _text := '12345.78';
  with Image1.Canvas do
  begin
    W2 := TextWidth(_text);
    W3 := W1 - W2;
    X2 := X1 + W3;
    TextOut( X2, 5, _text);   // 5 = Y
  end;
end;

Saturday, November 22, 2008

Delphi - Aligning text in StringGrid Cell




Sorry i am too lazy to code. Only use print screen. But it is a working sample.

Monday, October 27, 2008

Delphi Data Access Utility

I want to share this one out as if
1. Any one like to use it, be my guest.
2. Any one has any comment on it, please :)
Note: I am a biginner in Delphi, so please manage your expectation. Thanks.

[
unit LIB_DATA_ACCESS;

interface

uses
  Windows, SysUtils, Variants, DB, ADODB;

const
  CONNECTION_STRING='Provider=Microsoft.Jet.OLEDB.4.0;Data Source=data\test.mdb;Persist Security Info=False;Jet OLEDB:Database Password=******';

Type
  TResultSet = Array of Array of Variant;

function ExecuteQuery( Conn:TADOConnection; Query:TADOQuery; Sql : String;
    Parameters : Array of String;
    Values : Array of Variant): TResultSet;
function ExecuteUpdate( Conn:TADOConnection; Query:TADOQuery; Sql : String; 
    Parameters : Array of String; Values : Array of Variant ): Integer;

implementation


function ExecuteQuery(Conn:TADOConnection; Query:TADOQuery; Sql : String;
    Parameters : Array of String;
    Values : Array of Variant): TResultSet;
var
  tempArray : TResultSet;
  row_index : Integer;
  col_index : Integer;
  i : Integer;
  ColumnSize : Integer;
begin

  Conn.ConnectionString := CONNECTION_STRING;
  Conn.LoginPrompt := False;
  Query.Connection := CONN;
  if Query.Active then
  begin
    Query.Close;
  end;

  try

    Query.SQL.Clear;
    Query.SQL.Add(Sql);

    if Length(Parameters) > 0 then
    begin
      for  i := 0 to (Length(Parameters) - 1) do
      begin
        Query.Parameters.ParamByName(Parameters[i]).Value := Values[i];
      end;
    end;

    Query.Open;
    SetLength(tempArray, Query.RecordCount);

    row_index := 0;

    while not Query.Eof do
    begin
      ColumnSize := Query.Recordset.Fields.Count;
      SetLength(tempArray[row_index], ColumnSize);

      col_index := 0;
      for i := 0 to (ColumnSize - 1) do
      begin
        tempArray[row_index][col_index] := Query.Recordset.Fields[i].Value;
        col_index := col_index + 1;
      end;

      row_index := row_index + 1;
      Query.Next;
    end;
  finally
    Query.Close;
    Conn.Close;
    Result := tempArray;
  end;
end;

function ExecuteUpdate( Conn:TADOConnection; Query:TADOQuery; Sql : String;
    Parameters : Array of String;
    Values : Array of Variant): Integer;
var
  i : Integer;
begin
  Conn.ConnectionString := CONNECTION_STRING;
  Conn.LoginPrompt := False;
  Query.Connection := CONN;
  if Query.Active then
  begin
    Query.Close;
  end;

  try
    Query.SQL.Clear;
    Query.SQL.Add(Sql);

    if Length(Parameters) > 0 then
    begin
      for  i := 0 to (Length(Parameters) - 1) do
      begin
        Query.Parameters.ParamByName(Parameters[i]).Value := Values[i];
      end;
    end;

//  Query.Open;
    Result := Query.ExecSQL;

  finally
    Query.Close;
    Conn.Close;
  end;
end;

end.
]