Discussion:
Refresh Button Labels from Database? (Cross-Post)
(too old to reply)
Patrick A
2009-12-23 19:53:55 UTC
Permalink
All,

Sorry for the cross-post, I keep forgetting there's a forum just for
controls.

I've searched and read and tried for 3 hours, but I'm still stuck.

I have a form (frmMain) with a series of buttons I am creating in
code.


The labels, colors, etc. for each button are read from a DB via the
code below.


I have another form (frmSetupButtons) that the user can use to change
the labels, colors, etc.


Once the user changes a label, color, etc. and closes the Setup Button
form, how do I get the labels, etc. on the buttons to update?


(If I close the app and open it again, it updates...but I can't figure
out how to force the update in code.)


Any suggestions for the proper combination of refreshing the Data Set
and then the form?


Thanks,


Patrick


Here is the code I use to create the buttons.


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


oTimeSliceCollection = New Collection
Me.QRY_ButtonsTableAdapter.ClearBeforeFill = True
Me.QRY_ButtonsTableAdapter.Fill
(Me.SRTimerDataSet.QRY_Buttons)
Dim Row As Integer = 0
Dim ButLbls = SRTimerDataSet.Tables
("QRY_Buttons").AsEnumerable
()
Dim sortedButLbls = From tp In ButLbls _
Select tp _
Order By tp("TimerPos")
For Each tp In sortedButLbls 'Iterate through the
collection.
Row = Row + 1 'Increment up.
Dim NewButton As New MyButton() ' Sets a new button
Me.Controls.Add(NewButton) 'Adds the new button to the
form.
NewButton.butID = tp!TimerID.ToString 'Pull the TimerID
from DS and use it.
NewButton.Name = (tp!TimerPos) 'Pull the TimerPos from
DS and use it as a tag.
NewButton.Text = (tp!butLabel) 'Pull the butLabel from
DS
and use it as text.
NewButton.butLabel = tp!butLabel.ToString '
NewButton.butDesc = tp!butDesc.ToString 'Read the Work
Description from the DB.
NewButton.butDispCM = tp!DispCM.ToString 'Read the Cli/
Mat
from the DB.
NewButton.butTask = tp!Task.ToString 'Read the Task Code
from the DB.
NewButton.butAct = tp!Activity.ToString 'Read the
Activity
Code from the DB.
NewButton.butLoc = tp!Location.ToString 'Read the work
location from the DB.
NewButton.ContextMenuStrip = cmTimeButs
NewButton.BackColor = Color.FromName(tp!butColor) 'Read
the color of the button from the DB.
oTimeSliceCollection.Add(NewButton, tp
("TimerPos").ToString)
Next
Captain Jack
2009-12-23 20:21:03 UTC
Permalink
Post by Patrick A
All,
Sorry for the cross-post, I keep forgetting there's a forum just for
controls.
I've searched and read and tried for 3 hours, but I'm still stuck.
I have a form (frmMain) with a series of buttons I am creating in
code.
One option would be to make a Public (or Friend) Sub in frmMain that builds
the buttons, and move the code out of the Load event handler. Put a call to
the new function in the load event handler. Assuming there's only one
instance of frmMain being created, then you can have your setup form call
frmMain.BuildButton (or whatever you call it) when it exits.

Yout build routine would need to keep track of whether it had already put
the buttons on the form, so as not to make duplicates. To make it faster to
find them, you can make a list of type Button and put them in there when
they get added to the controls collection.

--
Jack
Patrick A
2009-12-31 22:33:55 UTC
Permalink
Thanks Jack, I'll give that a try.

Loading...