English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have one context menu and in one of the sub menus i have created a Background Color button. The Background Color button will display the color dialog box and will change the backcolor of the selected text box only. I got it all set up but when i debug it, all the text boxes change to the same color. How do I make it where only the selected text box changes color??


Also im am useing the Microsoft Visual Studios program.

2007-03-09 09:53:55 · 2 answers · asked by refuse2lose_2006 3 in Computers & Internet Programming & Design

2 answers

This issue deals with control focus... When you enter a control focus is shifted to it and the GotFocus event will fire. Conversly when you leave that control to goto another A lost focus event will fire as focus shifts to the next control.


When you click on a Menu bar item it will receive focus and the textbox will loose focus.

You cannot change the background color of the text box with focus because you have focus on a menu item ....

What you can do is use a module level variable to hold a copy of the last text box which was clicked on

private tb as textbox

create a multi control event handler for all of your textboxes which fires on got focus

Private Sub TextBox_GotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles TextBox1.GotFocus, TextBox2.GotFocus

tb = sender ' assign a copy of the current textbox to TB

This eventhandler will create a copy of the textbox which you just clicked on.

Now in your menu item assign a new background color to the TB object

TB.backcolor = dlg.color 'where dlg is your color dialog box

2007-03-09 10:29:26 · answer #1 · answered by MarkG 7 · 0 1

I think your not refering to the right obj.backcolor property.

Your button code should go something like this:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each obj As Object In Me.ContextMenuStrip1.Items
Me.ColorDialog1.ShowDialog()
obj.BackColor = Me.ColorDialog1.Color
Next

'You could also directly refer to the text box using the following:
'ContextMenuStrip1.Items(index).backcolor
End Sub

I have a project dominstrating the code in action
http://rapidshare.com/files/20247203/ContexColorChange.rar.html

2007-03-09 18:17:04 · answer #2 · answered by NoComment 2 · 0 0

fedest.com, questions and answers