PDA

View Full Version : آموزش: تغییر backclor تمامی text box های فرم با گرفتن فوکوس



mohsenaminzare
شنبه 19 مرداد 1392, 10:30 صبح
با سلام به دوستان
عیدتان مبارک
کد زیر شاید بدرد خیلی ها بخوره.
در فرم هر textbox با گرفتن focus تغییر رنگ می ده و با از دست دادن فوکوس به حالت اول بر می گرده که با کمی تغییر می تونین کارهای جالبی کنین.

Private Sub Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

For Each c As Control In Me.Controls
AddHandler DirectCast(c, Control).GotFocus, AddressOf control_GotFocus
AddHandler DirectCast(c, Control).LostFocus, AddressOf control_LostFocus
Next
Private Sub control_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)

Dim c As Control = DirectCast(sender, Control)
c.BackColor = Color.White
End Sub

Private Sub control_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
Dim c As Control = DirectCast(sender, Control)
c.BackColor = Color.Yellow
End Sub

mohsenaminzare
چهارشنبه 23 مرداد 1392, 19:07 عصر
دوستان نظری ندارن ؟

vb341
چهارشنبه 23 مرداد 1392, 19:38 عصر
دوست عزیز ممنون . جالب بود

Fery666
پنج شنبه 24 مرداد 1392, 11:38 صبح
دستت درد نکنه .
ولی اگر توی فرم textbox و listbox یا هر کنترلی باشه وقتی روش کلیک کنی backcolor زرد میشه .

نمیشه فقط برای تکس باشه ؟؟

hosein320
پنج شنبه 24 مرداد 1392, 12:18 عصر
سلام
کافیه که نوع کنترل رو مشخص کنید


For Each c As Control In Me.Controls
If TypeOf c Is TextBox Then
AddHandler DirectCast(c, Control).GotFocus, AddressOf control_GotFocus
AddHandler DirectCast(c, Control).LostFocus, AddressOf control_LostFocus
End If
Next

neverlieme
جمعه 25 مرداد 1392, 03:59 صبح
میتونید اینو در غالب یه کامپوننت کلاس به پروژتون اضافه کنید

Imports System.ComponentModel

<DefaultProperty("Mandatory")>
Public Class FocusedTextBox
Private _mandatory As Boolean
Private _enterFocusColor
Private _mandatoryColor As Color
Private _backColor As Color

<Category("Appearance"), _
Description("Determines whether the control can be left blank or not"), _
DefaultValue(False)> _
Public Property Mandatory() As Boolean
Get
Return _mandatory
End Get
Set(ByVal Value As Boolean)
_mandatory = Value
End Set
End Property

<Category("Appearance"), _
Description("The background color of a FocusedTextBox control when it has the focus"), _
DefaultValue(GetType(Color), "Color.Tomato")> _
Public Property EnterFocusColor() As Color
Get
Return _enterFocusColor
End Get
Set(ByVal Value As Color)
_enterFocusColor = Value
End Set
End Property

<Category("Appearance"), _
Description("The background color of a mandatory FocusedTextBox control when it loses the focus and it's blank"), _
DefaultValue(&H11FFBBCC)> _
Public Property MandatoryColor() As Color
Get
Return _mandatoryColor
End Get
Set(ByVal Value As Color)
_mandatoryColor = Value
End Set
End Property

Private Sub FocusedTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Enter
_backColor = Me.BackColor
Me.BackColor = _enterFocusColor
End Sub

Private Sub FocusedTextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
If Trim(Me.Text).Length = 0 And _mandatory Then
Me.BackColor = _mandatoryColor
Else
Me.BackColor = _backColor
End If

End Sub
End Class

juza66
جمعه 25 مرداد 1392, 04:32 صبح
میشه بگید چطور تکس باکس backcolor رو به حالت trans دراورد

من یک صفحه دارم عکس بک راند گذاشتم میخوام تک باکس بدون رنگ روی صفحه باشه وقتی کاربر کلیک کرد دور تکس باکس زرد بشه ولی رنگش تغییر نکنه ( ترنس باشه )

systam
جمعه 25 مرداد 1392, 10:20 صبح
یشه بگید چطور تکس باکس backcolor رو به حالت trans دراورد

من یک صفحه دارم عکس بک راند گذاشتم میخوام تک باکس بدون رنگ روی صفحه باشه وقتی کاربر کلیک کرد دور تکس باکس زرد بشه ولی رنگش تغییر نکنه ( ترنس باشه )
سلام
اینم یک کامپوننت برای tranceprante کردن textbox ها
موفق باشید
یا علی

systam
جمعه 25 مرداد 1392, 12:07 عصر
سلام
برای تغییر رنگ موقع GotFocus , LostFocus
برای چی اینقدر کد رو بپیچونی این شکلی بنویسیم چی میشه
Private Sub txt_family_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_family.GotFocus
txt_family.BackColor = Color.Yellow
End Sub
Private Sub txt_family_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_family.LostFocus
txt_family.BackColor = Color.White
End Sub