Archive for May, 2009

how to create a modeless (non-modal) dialog in WTL (ATL , C++)

May 25, 2009

modeless dialog:
thanks to http://www.sowbug.org/mt/2004/01/modeless-dialogs.html#more

1) in MainDlg.h , a class global for the dialog (otherwise it will be created and destroyed in
the same subroutine and throw an exception)
CBigFocusDlg dlgBigFocus;

2) Create the modeless dialog in response to a button click in the main dialog

// **************************************
LRESULT CMainDlg::OnBnClickedShow_Big_Focus_Dlg(WORD /*wNotifyCode*/, WORD

/*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
// if it already exists, then bring it to top and return
if ( dlgBigFocus ) {
::BringWindowToTop(dlgBigFocus.m_hWnd);
return 0;
}

// MODELESS DIALOG
// http://www.sowbug.org/mt/2004/01/modeless-dialogs.html#more

dlgBigFocus.Create(NULL );
dlgBigFocus.CenterWindow(this->m_hWnd);
dlgBigFocus.ShowWindow(SW_SHOWNORMAL);

return 0;
}

// ******************************************************************************
// how to close the dialog:

LRESULT CBigFocusDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// center the dialog on the screen
CenterWindow();

return TRUE;
}

LRESULT CBigFocusDlg::OnCloseCmd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
this->DestroyWindow();
return 0;
}

// **************************************
LRESULT CBigFocusDlg::OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
this->DestroyWindow();
return 0;
}

html checkbox in 1.1 datagrid bound to data

May 20, 2009

If you’re used to .net server controls, you’ll probably want to set the checked property on a checkbox control to true. Well that won’t work.
It displays either checked or not checked depending on whether the string checked is one of its attributes.

Here is a way to make it happen in a templated column in a datagrid in old-school .net 1.1:


1) the templated column

<input type="checkbox" onclick="ajaxws_OnCheckBoxChanged(this)"
id='chkgrid$tDownloads$isHomeUse$sdKy$'

>

a) the line must be either the
string "checked" (without the quotes), or nothing at all. You cannot set checked=1, or checked=true, etc.
b) NOTE - we don't have quotes around the stuff, so you can not edit the page in design mode

2) therefore we convert the boolean to a string with a case statement when we do the database query:
(also take care for isNull)
CASE isNull(isHomeUse, 0)
WHEN 1 THEN 'checked'
ELSE ''
END AS isHomeUse

Note: I’m not so good at getting the Eval syntax to work. I don’t like returning an html string from my sql query, but at least this way works. I’d be interested in hearing from someone who can improve on this.
But I won’t hold my breath, since this code seems so 2002.

function to return the current value and id of all controls

May 14, 2009

I ended up not using this yet, but it returns a dictionary collection of all controls contained in a page, for example, and their current value. This includes textboxes, dropdowns, radios, and checkboxes.
I usually credit the original source for functions like these, but I lost where the recursive sub came from

' **************************************************************************
' purpose: function to call the recursive sub and then return the dictionary containing the results
Public Shared Function get_all_controls(ByVal container As Control) As Hashtable
htControls.Clear()
Dim strTemp As String = container.ID & " " & container.ClientID
strTemp = strTemp + container.UniqueID + " " + container.Page.Title
Diagnostics.Debug.Print(strTemp)
get_all_controls_recursive(container)
Return htControls
End Function

' **************************************************************************
' recursive sub to list all controls in a container
Public Shared Sub get_all_controls_recursive(ByVal container As Control)
Dim strTemp As String
Dim bFound As Boolean = False

For Each ctl As Control In container.Controls

bFound = False

strTemp = ctl.ID

If (Not bFound) Then
Dim txt As TextBox = TryCast(ctl, TextBox)
If (Not IsNothing(txt)) Then
htControls.Add(txt.ID, txt.Text)
bFound = True
End If
End If

If (Not bFound) Then
Dim ddl As DropDownList = TryCast(ctl, DropDownList)
If (Not IsNothing(ddl)) Then
htControls.Add(ddl.ID, ddl.Text)
bFound = True
End If
End If

If (Not bFound) Then
Dim chk As CheckBox = TryCast(ctl, CheckBox)
If (Not IsNothing(chk)) Then
htControls.Add(chk.ID, chk.Checked.ToString())
bFound = True
End If
End If

If (Not bFound) Then
Dim rdio As RadioButton = TryCast(ctl, RadioButton)
If (Not IsNothing(rdio)) Then
htControls.Add(rdio.ID, rdio.Checked.ToString())
bFound = True
End If
End If

If (ctl.Controls.Count > 0) Then get_all_controls_recursive(ctl)

Next

End Sub

get the current page name

May 14, 2009

I wanted to get the page name of the current *.aspx page that I was in. This is what I did (not rocket science, admittedly)

' *****************************************************************************
' purpose - return the aspx page name
Public Shared Function GetCurrentPageName() As String
Dim strRet As String = ""
'Return System.Web.HttpContext.Current.Request.Url.ToString
Dim sBasePath As String = System.Web.HttpContext.Current.Server.MapPath("")
Dim sFullPath As String = System.Web.HttpContext.Current.Request.ServerVariables("PATH_TRANSLATED")
sBasePath = sBasePath.ToLower() + "\"
sFullPath = sFullPath.ToLower()
strRet = sFullPath.Replace(sBasePath, "")
Return strRet
End Function

change the style at the server

May 14, 2009

I made a user control, and I wanted to add a ctl_Width property and apply the value to all the contained sub-controls.
But setting the “Width” property of each did not work – I had to change the style of each. So I made some functions to help me in setting the style attribute of each of the sub controls contained in my user control. Here are the functions:

' ------------------------------------------------------------------------------
' purpose - set some style at the server side
' how to call: iRet = SetStyleValue(ddlCombo, "width", sWidth, strError)
' ref: http://codebetter.com/blogs/brendan.tompkins/archive/2003/10/23/2827.aspx
Public Shared Function SetStyleValue(ByVal o As System.Web.UI.WebControls.WebControl, ByVal sKey As String, ByVal sVal As String, Optional ByRef strError As String = "") As Integer
Try
o.Style.Add(sKey, sVal)
Return 1
Catch ex As Exception
strError = ex.Message
Return -666
End Try
End Function

' *********************************************************************
' purpose - get the current style string for the control
' ref:http://codebetter.com/blogs/brendan.tompkins/archive/2003/10/23/2827.aspx
' how to call:
Public Shared Function GetStyleString(ByVal o As System.Web.UI.WebControls.WebControl) As String
Dim strStyle As String = ""
Try
Dim S As System.Web.UI.CssStyleCollection = o.Style
For Each key As String In S.Keys
strStyle += key + ":" + S(key) + ";"
Next
Catch ex As Exception
strStyle = "ERROR"
End Try
Return strStyle
End Function