Pulkit Gaur

dream - do - deserve

Controlling Monitor Power - Switch On / Switch Off

Filed under: Visual Basic at 03:28:25 On 2006-11-18

Nothing new ..

 

but this code fragment will allow you to control your terminal display to be switch on or off.

Well you may use it to schedule your monitor power after a predetmined time instead of screensaver - go greener :)

 

 

To use this code put this code in your form code and put 2 buttons on the form.

 

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Any) As Long
Const SC_MONITORPOWER = &HF170&
Const MONITOR_ON = -1&
Const MONITOR_OFF = 2&
Const WM_SYSCOMMAND = &H112

Private Sub Command1_Click()
SendMessage Me.hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF
Command2.SetFocus
End Sub

Private Sub Command2_Click()
SendMessage Me.hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON
End Sub

Combobox and TreeView handy tricks

Filed under: Visual Basic at 09:21:19 On 2006-09-27

While working on a small project i used these frequently,thought should share it !

 

1)To Select First Item as default in Dropdown Combobox use :

 

ComboBoxName.ListIndex = 0


2)To remove a particular nodes children from tree view use this fragment :

While treeControl.SelectedItem.Children > 0
treeControl.Nodes.Remove treeControl.SelectedItem.Child.Key
Wend


3)This code fragment facilitates ajax type autosuggest/autocomplete feature in combobox.This code is not mine !

 

'Api declaration
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long

'Cont declaration
Private Const CB_FINDSTRING As Long = &H14C

'Procedure declaration
Public Sub ComboAutoComplete(ByRef SourceCtl As VB.ComboBox, _
ByRef KeyAscii As Integer, ByRef LeftOffPos As Long)
Dim iStart As Long
Dim sSearchKey As String

With SourceCtl
'If text entered so far matches item(s) in the list, use autocomplete
Select Case Chr$(KeyAscii)
Case vbBack
'Let backspace characters process as usual; otherwise try to match text
Case Else
If Chr$(KeyAscii) <> vbBack Then
.SelText = Chr$(KeyAscii)

iStart = .SelStart

If LeftOffPos <> 0 Then
.SelStart = LeftOffPos
iStart = LeftOffPos
End If

sSearchKey = CStr(Left$(.Text, iStart))
.ListIndex = SendMessage(.hwnd, CB_FINDSTRING, -1, _
ByVal CStr(Left$(.Text, iStart)))

If .ListIndex = -1 Then
LeftOffPos = Len(sSearchKey)
End If

.SelStart = iStart
.SelLength = Len(.Text)
LeftOffPos = 0

KeyAscii = 0
End If
End Select
End With
End Sub


Use this code as follows:

Private Sub Combo1_KeyPress(KeyAscii As Integer)
Static lWhere As Long
ComboAutoComplete Combo1, KeyAscii, lWhere
End Sub