vrijdag 12 juni 2009

VB6 fill form IE

This is a repost of an example many people were looking for.

In this sample I will fill the altavista search box, with the WebBrowser control. Below I will list some subs and functions which are used in this sample.

Open a new project (standard exe) and place a WebBrowser control, a textbox, and a command button on form1. Make sure you also have an reference to mshtml.tlb.


Add the following code to form1:


Private Sub Command1_Click()
Dim doc As HTMLDocument

'go to the altavista (text) search page
WebBrowser1.Navigate "http://www.altavista.com/cgi-bin/query?text"

'Wait until page is loaded
Do
DoEvents
Loop Until Not WebBrowser1.Busy

'Make doc reference to the document inside the webbrowser control
Set doc = WebBrowser1.Document

'Set field q with the value of Text1
SetInputField doc, 0, "q", Text1

'Submit the form (same result as click the search button)
doc.Forms(0).submit

'Wait until result are loaded
Do
DoEvents
Loop Until Not WebBrowser1.Busy

MsgBox "Altavista search result loaded"
End Sub


Add the following code to a module:


Public Sub SetInputField(doc As HTMLDocument, Form As Integer, Name As String, Value As String)
'doc = HTMLDocument, can be retrieved from webbrowser --> webbrowser.document
'Form = number of the form (if only one form in the doc --> Form = 0)
'Name = Name of the field you would like to fill
'Value = The new value for the input field called name
'PRE: Legal parameters entered
'POST: Input field with name Name on form Form in document doc will be filled with Value

For q = 0 To doc.Forms(Form).length - 1
If doc.Forms(Form)(q).Name = Name Then
doc.Forms(Form)(q).Value = Value
Exit For
End If
Next q
End Sub


Additional useful subs:


Sub to get the contents from a textbox:
Public Function GetInputField(doc As HTMLDocument, Form As Integer, Name As String) As String
For q = 0 To doc.Forms(Form).Length - 1
If doc.Forms(Form)(q).Name = Name Then
GetInputField = doc.Forms(From)(q).Value
Exit For
End If
Next q
End Function



Sub to set a Checkbox:
Public Sub SetCheckBox(doc As HTMLDocument, Form As Integer, Name As String, Value As Boolean)
For q = 0 To doc.Forms(Form).Length - 1
If doc.Forms(Form)(q).Name = Name Then
doc.Forms(From)(q).Checked = Value
Exit For
End If
Next q
End Sub



Sub set a radio button:
Public Sub SetRadioButton(doc As HTMLDocument, Form As Integer, Name As String, Name2 As String)
For q = 0 To doc.Forms(Form).Length - 1
If (doc.Forms(Form)(q).Name = Name) And (doc.Forms(Form)(q).Value = Name2) Then
doc.Forms(From)(q).Checked = True
Exit For
End If
Next q
End Sub


Sub set a combo box:
Public Function SetComboBoxValue(ByVal doc As IHTMLDocument3, Form As
Integer, Name As String, Name2 As String)
'**** This one bases it's selection on the Value of the - - Tag.
Dim q, i

For q = 0 To doc.Forms(Form).length - 1
If (doc.Forms(Form)(q).Name = Name) Then
For i = 0 To doc.Forms(Form)(q).length - 1
If doc.Forms(Form)(q).Options(i).Value = Name2 Then
doc.Forms(Form)(q).Options(i).Selected = True
Exit For
End If
Next i
End If
Next q
End Function


Public Function SetComboValue(ByVal doc As IHTMLDocument3, Form As Integer,
Name As String, Name2 As String)
'**** This one bases it's selection on the Value of the Text after the -
'Text - Tag.
Dim q, i

For q = 0 To doc.Forms(Form).length - 1
If (doc.Forms(Form)(q).Name = Name) Then
For i = 0 To doc.Forms(Form)(q).length - 1
If doc.Forms(Form)(q).Options(i).Text = Name2 Then
doc.Forms(Form)(q).Options(i).Selected = True
Exit For
End If
Next
End If
Next q

donderdag 11 juni 2009

Rails - HoboFields as Plugin

As I mentioned before, the documentation of Hobo is still not sufficient to build a real application with Hobo. One of the problems I encountered was that I wanted to change how certain fields are displayed in the new/edit form. The documentation does not describe this clearly or at all (I couldn't find it). For this Hobo is not really usable for me at this moment. One think I really like about Hobo is HoboFields though. HoboFields allows you to declare your table definitions in your model and automatically generate migrations from it. The good news is that we can actually use HoboField without using the complete Hobo package as it's available as plugin.

When HoboFields is installed you can do the following:


require 'hobo_fields'

class Category < ActiveRecord::Base
fields do
name :string
description :text
timestamps
end

belongs_to :category
belongs_to :administration
end


Next, you just run:

ruby script/generate hobo_migration


And the migration will be generated. Now you may think that this is not a good thing, because by making your own migrations you could keep track of changes and roll updates to other environments, right? Well, this is not a problem because HoboFields will actually detect any changes made to the model and generate the migrations for you!

If you are interested in using HoboFields, you can install it as follows:

gem install hobofields

vrijdag 5 juni 2009

The else if in Ruby

The basic if structure in ruby looks like listed below. Especially the 'else if' is uncommon: elsif.


if name='peter'
...
elsif name='john'
...
end

woensdag 3 juni 2009

Rails & Hobo: Changing the default layout

According to the POD tutorial on the Hobo project website you should be able to change the default layout of the page in app/views/taglibs/application.dryml. This is true, however the example they give does not work (any more):




Copyright 2009 XFerion




After some searching though the code generated by Hobo I found out how one can make this work with the current version of Hobo:




Copyright 2009 XFerion


Ruby on Rails - Hobo

Hobo is a framework for Ruby on Rails which is supposed to make the development of a web application even easier. I just tried it and it seems to have some great features to get an application working really fast. This project has one big downside as of now, the documentation is terrible. I hope soon there will be some more documentation available, as it hard to find out how everything works. There are some tutorials available but unfortunately they of some parts that have not been updated to the latest version of Hobo, with the result that some of the examples don't work.

I really hope they will get some good documentation online soon, as the project seems to be very promising. Even though the final 1.0 version of this project is not completed yet, I will give it a try for my next project. Hopefully they will also add the support for internationalization soon as that would be a really nice addition to this framework for the non US people out there like me.

You can find the Hobo project here: Ruby on Rails Hobo framework website

woensdag 29 april 2009

Try Catch Finally block in Ruby

For all the people with bad memory like me, here is the Ruby equivalent of the Try Catch Finally block:


begin
somecode()
rescue
puts "Error #{$!}"
ensure
this_code_will_execute_always()
end

Ruby on Rails 2.0 Scaffold

The scaffold generator is not as easy to use anymore in Rails 2.0 as it was in the previous version. Instead of working directly with database tables, now it only works with migrations. The use of migrations is a good thing in my opinion, because migrations keep it easy to track and apply database changes. What I don't like about the renewed scaffold generator is that you have to put all the fields you want to have in your table in the command line. If you create your own migration files, scaffold simply doesn't work. This may be OK with small tables, but if you have tables with lots of fields it is much easier to make the migration file your self. Putting all table fields on the command line doesn't give a good view of all the fields, and you won't notice if you forgot a field.

An example of a scaffold generation:

> ./script/generate scaffold ModelName field1:type field2:type field3:type