PDA

View Full Version : سوال: های لایت کردن کنترلی که فوکوس دارد



king.e.access
جمعه 22 خرداد 1394, 07:35 صبح
سلام

دوستان در یک فرم تعدادی کنترل از قبیل تکس باکس ، لیبل ، کمبو باکس و لیست باکس داریم

حال می خواهیم اگر که فقط تکس باکس های ما فوکوس را به خود گرفتند رنگ زمینه آنها به رنگ خاصی در آید و زمانیکه فوکوس از آنها خارج شد به رنگ دیگر در آیند


ساده ترین راه آن است که برای تک تک آنها بنویسیم زمانیکه فوکوس گرفت رنگ زمینه به این رنگ و زمانی که فوکوس خارج شد رنگ زمینه به رنگی دیگر تبدیل شود

خیر !

بنده می خواهم تابع ای داشته باشیم که هنگام باز شدن فرم این تابع فراخوانی شود و به محض اینکه کنترل های مورد نظر ما فوکوس گرفتند این عملیات صورت بگیرد.

و نه لزوما در رویداد getfocus کنترل های مد نظر تابع را فراخوانی کنیم .

با تشکر از دوستان خوبم

RESMAILY
یک شنبه 24 خرداد 1394, 07:10 صبح
به نام خدا
با سلام. اگر تعداد کنترل هایتا زیاد نیست بهتر است که برای تک تک شان کد بنویسید. وگرنه باید سه تابع برای فرم بنویسید(شاید با دو تابع هم بشود) در یک یا دو تابع نحوه تغییر رنگ را بنویسید و در یک تابع که در رویداد بازشدن قرار می دهید کنترل ها را ست کنید و رویداد نوشته شده در تابع اول را به آن نسبت دهید.
شرط لازم این است که کنترل ها در یک بخش از نام باهم مشترک باشند. یا در tag آن عبارت مشترکی درج نمایید.

king.e.access
یک شنبه 24 خرداد 1394, 21:28 عصر
سلام

ضمن عرض تشکر

بسیار ممنون می شوم

با نوشتن کدهای توابعی که فرمودید راهنمایی بیشتری بفرمایید

می دونم که وقت زیادی از شما خواهد گرفت ولی بسیار سپاسگذار خواهم بود

شاگرد آرام
دوشنبه 25 خرداد 1394, 13:02 عصر
132256سلام راه ساده تری هم هست البته اکسس 2007 به بعد تکست باکس رو انتخاب کنید تو قسمت فرمت کاندیشنال فرمتینگ یک رول جدید تعریف کنید و مشخص کنید موقع فوکوس چه رنگی بشه

king.e.access
دوشنبه 25 خرداد 1394, 20:40 عصر
سلام

عرض کردم که بصورت تابع باشد

بالاخره خودم توی فروم های خارجی پیدا کردم ولی خیلی گسترده است

اگر جناب استاد امیری و سایر اساتید کمک کنند راه حل ساده تری باشد ممنون می شویم

هم فهمش نیاز داره چند روز بهش اختصاص بدیم خلاصه خیلی دشواره!!!

:گریه:



Option Compare Database
Option Explicit
'Purpose: Call this in any form to:
' (i) highlight required fields, or
' (ii) highlight the control that has focus.
'
'Usage: To do both:
' =SetupForm([Form])
' To do just the required fields, or just the focus, use:
' =SetupForm([Form], 1)
' =SetupForm([Form], 2)
' Note: Do not substitute your form name for [Form] above.
'
'Author: Allen Browne allen@allenbrowne.com
'Version: 21 September 2008.
'Copyright: None. You can use this in your database for any purpose.
' We request that you acknowledge the source in your code.
'Documentation: http://allenbrowne.com
'
'Notes: 1. Colors work in Form view and Continuous Form (highlights entire column),
' but only the star shows Datasheet view.
' 2. Works on text boxes, combos, and list boxes only.
' 3. Change the colors immediately below to suit (RGB values.)
'
'The RGB value to use as a control's Back Color when it has focus.
Private Const mlngcFocusBackColor = &HB0FFFF
'The RGB value to use as Back Color if a control is bound to a required field.
Private Const mlngcRequiredBackColor = &HD0D0FF


'These constants are for assigning/reading the Tag property.
Private Const mstrcTagBackColor = "UsualBackColor"
Private Const mstrcTagSeparator = ";"
Private Const mstrcTagAssignmnent = "="


Public Function SetupForm(frm As Form, Optional iSetupWhat As Integer = &H7FFF)
'Purpose: Set up a form to do both (highlight required, and control with focus.)
'Argument: A reference to the form to setup.
'Usage: Set form's On Load property to: =SetupForm([Form])
'Note: The Required must be handled first (to save the right restore value.)
Const iSetupRequired = 1
Const iSetupFocusColor = 2

If (iSetupWhat And iSetupRequired) Then Call SetupRequiredFields(frm)
If (iSetupWhat And iSetupFocusColor) Then Call SetupFocusColor(frm)
End Function


Public Function SetupFocusColor(frm As Form)
On Error GoTo Err_Handler
'Purpose: Set properties for all text boxes, combos, and list boxes, _
so they are highlighted when they receive focus.
'Argument: A reference to the form to setup.
'Usage: Set any form's On Load property to: =SetupFocusColor([Form])
'Note: Skips any control that already has something in OnGotFocus or OnLostFocus.
'Return: True on success
Dim ctl As Access.Control 'Each control on the form.

For Each ctl In frm.Controls
With ctl
Select Case .ControlType
Case acTextBox, acComboBox, acListBox
If (.OnGotFocus = vbNullString) And (.OnLostFocus = vbNullString) Then
.OnGotFocus = "=Hilight([" & .Name & "], True)"
.OnLostFocus = "=Hilight([" & .Name & "], False)"
.Tag = .Tag & IIf(.Tag <> vbNullString, mstrcTagSeparator, Null) & _
mstrcTagBackColor & mstrcTagAssignmnent & .BackColor
End If
End Select
End With
Next
SetupFocusColor = True

Exit_Handler:
Set ctl = Nothing
Exit Function


Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "SetupFocusColor()"
Resume Exit_Handler
End Function


Public Function SetupRequiredFields(frm As Form)
On Error GoTo Err_Handler
'Purpose: Set properties for all text boxes, combos, and list boxes, _
to highlight those bound to a required field.
'Argument: A reference to the form to setup.
'Usage: Set any form's On Load property to: =SetupRequired([Form])
'Return: True on success
Dim rs As DAO.Recordset 'Recordset of the form.
Dim ctl As Access.Control 'Each control on the form.
Dim strField As String 'Name of the field a control is bound to.

Set rs = frm.Recordset
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox
'Ignore unbound, or bound to an expression.
strField = ctl.ControlSource
If (strField <> vbNullString) And Not (strField Like "=*") Then
With rs(strField)
If (.Required) Or (.ValidationRule Like "*Is Not Null*") Then
ctl.BackColor = mlngcRequiredBackColor
Call MarkAttachedLabel(ctl)
End If
End With
End If
End Select
Next
SetupRequiredFields = True

Exit_Handler:
Set ctl = Nothing
Set rs = Nothing
Exit Function


Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "SetupRequiredFields()"
Resume Exit_Handler
End Function


Public Function Hilight(ctl As Access.Control, bOn As Boolean)
'Purpose: This code that gets called when focus moves into/out of a control.
'Arguments: ctl = the control whose BackColor should be changed.
' bOn = flag: True if receiving focus, False if losing focus.
Dim strBackColor As String

If bOn Then
'Assign the 'got focus' color.
ctl.BackColor = mlngcFocusBackColor
Else
'Restore the color from the control's Tag property (white if not found.)
strBackColor = ReadFromTag(ctl, mstrcTagBackColor)
If IsNumeric(strBackColor) Then
ctl.BackColor = Val(strBackColor)
Else
ctl.BackColor = vbWhite
End If
End If
End Function


Private Function MarkAttachedLabel(ctl As Access.Control)
On Error GoTo Err_Handler
'Purpose: Bypass the error if a control has no attached label.

With ctl.Controls(0)
If Not .Caption Like "*
" Then
.Caption = .Caption & "*"
.FontBold = True
End If
End With


Exit_Handler:
Exit Function


Err_Handler:
Resume Exit_Handler
End Function


Private Function ReadFromTag(ctl As Control, strName As String) As String
'Purpose: Parse a value from the Tag property of a control.
'Arguments: ctl = the control whoe Tag property you want to look in.
' strName = the name of the variable to search for.
'Return: The value of the property. ZLS if not found.
'Example: ReadFromTag([Text0], "test") will returns 99 if Text0.Tag is:
' test=99
Dim varArray As Variant
Dim strValue As String
Dim i As Long

If ctl.Tag <> vbNullString Then
varArray = Split(ctl.Tag, mstrcTagSeparator)
If IsArray(varArray) Then
For i = LBound(varArray) To UBound(varArray)
If varArray(i) Like strName & mstrcTagAssignmnent & "*" Then
ReadFromTag = Mid(varArray(i), Len(strName) + Len(mstrcTagAssignmnent) + 1&)
End If
Next
End If
End If
End Function




و در رویداد on load بنویسیم



Call SetupForm([Form])

شاگرد آرام
سه شنبه 26 خرداد 1394, 13:05 عصر
دوست عزیز این مدلی که من خدمتتون عرض کردم بوسیله کد نویسی هم میشه و نیازی به این همه کد نیست

در رویداد onload مقدار controls رو به تابع زیر پاس بدید

Private Sub Form_Load()
fct Controls
End Sub


اینم کد تابع


Function fct(cs As Controls)


For Each cnt In cs

If cnt.ControlType = 109 Then
Dim fc As FormatCondition
Set fc = cnt.FormatConditions.Add(acFieldHasFocus)
fc.BackColor = RGB(255, 0, 0)

End If
Next
End Function

Abbas Amiri
سه شنبه 26 خرداد 1394, 21:46 عصر
سلام

عرض کردم که بصورت تابع باشد

بالاخره خودم توی فروم های خارجی پیدا کردم ولی خیلی گسترده است

اگر جناب استاد امیری و سایر اساتید کمک کنند راه حل ساده تری باشد ممنون می شویم

هم فهمش نیاز داره چند روز بهش اختصاص بدیم خلاصه خیلی دشواره!!!



سلام

در یک ماژول کدهای زیر را کپی کنید:
Function GotFocusColor(frm As Form)
Dim ctl As Control
Set ctl = frm.ActiveControl
ctl.BackColor = RGB(255, 255, 153)
End Function

Function LostFocusColor(frm As Form)
Dim ctl As Control
Set ctl = frm.ActiveControl
ctl.BackColor = vbWhite
End Function

تکست باکس ها و کمبو ها و لیست باکس های مورد نظر را انتخاب کنید و در رویدادهای مربوطه بنویسید:
در پنجره Events
در On Got Focus عبارت =GotFocusColor([Forms]![FormName])
در On Lost Focus عبارت =LostFocusColor([Forms]![FormName])
را بنویسید

king.e.access
سه شنبه 26 خرداد 1394, 22:30 عصر
سلام

بسیار عالی!

از جناب استاد امیری و جناب شاگرد آرام سپاسگذارم

بلطف ایشان مشکلم حل شد