Modification of the UltraDateTimeEditor background - vb.net

I need to set the background color of Infragistics.Win.UltraWinEditors.UltraDateTimeEditor to yellow when the time is not today, for example. Also, I need to show a warning message when I move the cursor over the editor. I know XAML has such property I can use. How about in winform?

A quick example on how you could accomplish your goal. I am pretty sure that there are other ways to get this done, but this works
' Globals controls and Forms
Dim f As Form
Dim dt As Infragistics.Win.UltraWinEditors.UltraDateTimeEditor
Dim tt As Infragistics.Win.ToolTip
' This Main has been built using LinqPAD, you could have problems
' running it, as is in a WinForms project, but the concepts are the same
Sub Main()
dt = New UltraDateTimeEditor()
dt.Dock = DockStyle.Top
' Add the event handlers of interest to the UltraDateTimeEditor
AddHandler dt.Validating, AddressOf onValidating
AddHandler dt.Enter, AddressOf onEnter
tt = New Infragistics.Win.ToolTip(dt)
' Just another control to trigger the OnEnter and Validating events
Dim b = New Button()
b.Dock = DockStyle.Bottom
f = New Form()
f.Size = New Size(500, 500)
f.Controls.Add(dt)
f.Controls.Add(b)
f.Show()
End Sub
Sub onValidating(sender As Object , e As EventArgs)
' Some condtions to check and then set the backcolor as you wish
dt.BackColor = Color.Yellow
End Sub
Sub onEnter(sender As Object, e As EventArgs)
' Set the background back
dt.BackColor = Color.White
' Some condition to check to display the tooltip
If dt.DateTime <> DateTime.Today Then
tt.ToolTipText = "Invalid date"
' Time to leave the message visible
tt.AutoPopDelay = 2000
tt.DisplayStyle = ToolTipDisplayStyle.BalloonTip
' Calculation to set the tooltip in the middle of the editor
Dim p = New Point(dt.Left + 50, dt.Top + (dt.Height \ 2))
p = dt.PointToScreen(p)
' Show the message....
tt.Show(p)
End If
End Sub

Related

I'm trying to add pictureboxs to an array so I can reference them later as they are being created programmatically

So far all I have is this code...
Dim lineb As New PictureBox
lineb.Size = New Size(24, 24)
lineb.BackColor = Color.FromArgb(0, 192, 192)
lineb.Location = New Drawing.Point(xb, yb)
Controls.Add(lineb)
lineb.Name = "lineb" + CStr(creatorb)
creatorb += 1
This generates a infinite line of pictureboxs as it is inside a timer.tick event. Xb and Yb continuously move and this works. I need to figure out how to add each picturebox to an array or another way to reference them later. Each one gets created and renamed to lineb + 1...2...3...4... etc.
Here is an example of a way to keep track of dynamically created controls. It will be easier than adding every control to an array.
Dim pbox as New PictureBox
With pbox
.location = New Point(xb, yb)
.size = New Size(24, 24)
.Tag = "box1" 'choose a tag that will help identify it later
' and so on
Addhandler pbox.click, AddressOf pbox_click '' to add a click event handler
End With
Me.Controls.add(pbox)
Private Sub pbox_click(sender As Object, e As EventArgs)
Dim the_sender As PictureBox = DirectCast(sender, PictureBox)
Dim reference As String = DirectCast(the_sender.tag, String)
''' do whatever you need to do now that you know which picturebox was clicked.
End Sub
Alternatively you can always loop through your controls to find the one you are looking for:
For Each Ctrl As Control In Me.Controls
If TypeOf Ctrl Is PictureBox Then
If ctrl.tag = "box1" Then '''or whetever you are looking for
''' do your stuff here
End If
End If
Next

controls not being displayed

I have two similar methods to add a user control to a panel as needed. However, upon attempting to add, the method is called and completed, but does not add the user control to the form. While attempting various different ways of adding the user control, one time I could move the initial user control around and it would move to leave space for another user control, but the user control was not visible.
Edit: the initial add method (addInitialItemGroupTest) does work
Public Sub addItemGroupTest(ByVal sender As Object, ByVal e As EventArgs)
Console.WriteLine(Me.GetType.ToString() + "||" + System.Reflection.MethodInfo.GetCurrentMethod.ToString())
Dim item_block_new As New ucItemsetItemBlock
' item_block_new.Visible = True
' item_block_new.Dock = DockStyle.Top
item_block_new.flpMain.Name = (10 + item_set.blocks.Count()).ToString
item_block_new.BringToFront()
frm.flpItemBlocks.Controls.Add(item_block_new)
' item_block_new.Show()
AddHandler item_block_new.flpMain.Click, AddressOf addItemToItemBlock
End Sub
Public Sub addInitialItemGroupTest()
Console.WriteLine(Me.GetType.ToString() + "||" + System.Reflection.MethodInfo.GetCurrentMethod.ToString())
Dim item_block As New ucItemsetItemBlock
' item_block.Dock = DockStyle.Top
item_block.flpMain.Name = (10 + item_set.blocks.Count()).ToString
item_block.BringToFront()
frm.flpItemBlocks.Controls.Add(item_block)
AddHandler item_block.flpMain.Click, AddressOf addItemToItemBlock
' item_block.Sh
End Sub
Public Sub showEditor()
frm = New frmItemsetEditor
frm.TopLevel = False
frm.WindowState = FormWindowState.Maximized
frm.FormBorderStyle = FormBorderStyle.None
If frm.Location.X < 0 Then
frm.Location = New Point(0, frm.Location.Y)
End If
If frm.Location.Y < 0 Then
frm.Location = New Point(frm.Location.X, 0)
End If
frm.Show()
addItems()
If id > 0 Then
Console.WriteLine("loading item set")
For Each item_block In item_set.blocks
frm.flpItemBlocks.Controls.Add(item_block)
Next
Else
Console.WriteLine("creating item set")
item_set = New LeagueItemSet
addInitialItemGroupTest()
' addInitialItemGroup()
End If
AddHandler frm.btnAddItemGroup.Click, AddressOf addItemGroupTest
loadUserControl()
frm.pnlItemSetUserControl.Controls.Add(uc)
frm.flpItemBlocks.BringToFront()
AddHandler uc.btnAddUpdate.Click, AddressOf updateSetting
End Sub
Private Sub btnCreateItemset_Click(sender As Object, e As EventArgs) Handles btnCreateItemset.Click
Parent.AccessibleDescription = "status:Loading: Itemset Creation Wizard"
rgoism.addSetting()
rgoism._Settings.Last.frm.Parent = Me.Parent
rgoism._Settings.Last.frm.Location = New Point(Convert.ToInt32((Parent.Size.Width / 2) - (rgoism._Settings.Last.frm.Size.Width / 2)), Convert.ToInt32((Parent.Size.Height / 2) - (rgoism._Settings.Last.frm.Size.Height / 2)))
rgoism._Settings.Last.frm.BringToFront()
Me.Hide()
Parent.AccessibleDescription = "status:Ready"
End Sub
I kept debugging for nearly 12 hours straight. While nearly falling asleep I entered a bunch of console.writeline() into a method to test it since I have been lost. Next thing I know, it is adding visible user controls to the form. I just need to test them and make sure they're unique but I am too tired. I have no idea how it works now since I did not intentionally change any lines of code. Maybe I just lucked out and changed the right line of code by accident. I did comment out and back in a few lines as well. I am completely and utterly lost but it works now.

Creating multiple click events for dynamic buttons in vb.net

When creating a button I want to create a click event for each button. How do I create a click event for each button created dynamically? Here is my code. It works to create a single event based on the first button created.
Sub CreateDynamicButton()
Dim ButtonNumber As Integer = 1
Dim axisX As Integer = 53
Dim axisY As Integer = 13
' Create a Button object
Do Until ButtonNumber = 11
Dim dynamicButton As New Button
' Set Button properties
dynamicButton.Location = New Point(axisX, axisY)
dynamicButton.Height = 30
dynamicButton.Width = 200
' Set background and foreground
dynamicButton.BackColor = Color.Beige
dynamicButton.ForeColor = Color.Black
dynamicButton.Text = "I am Dynamic Button" & ButtonNumber
dynamicButton.Name = "DynamicButton" & ButtonNumber
dynamicButton.Font = New Font("Georgia", 10)
AddHandler dynamicButton.Click, AddressOf dynamicButton_Click
' Add Button to the Form. Placement of the Button
' will be based on the Location and Size of button
Controls.Add(dynamicButton)
axisY = axisY + 35
ButtonNumber = ButtonNumber + 1
Loop
'Add Exit Button
Dim dynamicButtonExit As New Button
' Set Button properties
dynamicButtonExit.Location = New Point(axisX, axisY)
dynamicButtonExit.Height = 30
dynamicButtonExit.Width = 200
' Set background and foreground
dynamicButtonExit.BackColor = Color.Beige
dynamicButtonExit.ForeColor = Color.Black
dynamicButtonExit.Text = "Exit"
dynamicButtonExit.Name = "Exit"
dynamicButtonExit.Font = New Font("Georgia", 10)
AddHandler dynamicButtonExit.Click, AddressOf dynamicButtonExit_Click
' Add Button to the Form. Placement of the Button
' will be based on the Location and Size of button
Controls.Add(dynamicButtonExit)
End Sub
You don't need to create multiple event to handle your dynamic buttons, dynamicButton_Click is enough to handle all click. Better give ID for each button and you only need to do is in this event, do code as below:
Dim btn As Button = DirectCast(sender, Button)
If btn.ID = "DynamicButton1" then
'Do logic here for button 1
End If
You just need to add a little logic to handle each button click in the one event handler. If you use as 'Select Case' Statement it keeps things tidy as well.
If you have a lot of code for each button's logic, it may be better to write separate subs for each button and call the appropriate sub using each 'Case' block like in the second case block
Private Sub DynamicButton_click(sender As Object, e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Select Case btn.Name
Case "DynamicButton1"
MessageBox.Show("button1")
Case "DynamicButton2"
dynamicbutton2_Click(sender, e)
End Select
End Sub
Private Sub dynamicbutton2_Click(sender As Button, e As EventArgs)
MessageBox.Show("Button2")
'and lots of other code
End Sub

How do I add then select the dynamic control I just added

I've got an application in VB.net which adds a picturebox to the selected mouse position within another picturebox control. I need to create a click event to select that new picturebox so I can drag it and drop it to a new location in the event that the first one was wrong or use a keypress event, those events I will code later, but I can not figure out how to select ANY of the dynamic controls.
In vb6 there was a way to select an index of the control, but there is no such animal in VB.net.
I've tried control groups, but for some reason I'm not getting results from them.
Here is the code I have so far
Private Sub PictureBox1_Click(sender As System.Object,
e As System.EventArgs) Handles PictureBox1.Click
Dim pb As New PictureBox
pb.BackColor = Color.Blue
Me.PictureBox1.Controls.Add(pb)
pb.Size = New Size(64, 110)
pb.Location = New Point(Cursor.Position.X - 64, Cursor.Position.Y - 110)
pb.Visible = True
End Sub
What in the name of all good things am I doing wrong here?
You need to write a generic event handler before time, using the sender parameter to refer to the object that raised the event.
Private Sub PictureBoxes_Click(sender As Object, e As EventArgs)
Dim pb = DirectCast(sender, PictureBox)
'Use pb here.
End Sub
When you create your control at run time, use an AddHandler statement to attach the method to the event.
Dim pb As New PictureBox
AddHandler pb.Click, AddressOf PictureBoxes_Click
That said, if you want to implement drag-n-drop then it's not the Click event you should be handling.
This little bit of code took some time, but I was able to do what I set out to so far...
This is before the Sub Main event
Public Class dynamicPB 'create a picturebox element which
'can be called anytime
Inherits PictureBox 'sets the type of control to a
'picturebox
Public Sub New() 'sets the function of the new box to
'default values
MyBase.BackColor = Color.AliceBlue
MyBase.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
MyBase.Height = 50
MyBase.Width = 26
End Sub
End Class
in the actual main class
Private Sub <control_event> (blah...) Blah...
Dim intPosAdj_X As Integer = 13 'get an offset for the cursor
Dim intPosAdj_Y As Integer = 25
Dim newPictureBox As New dynamicPB 'sets the click of the mouse into a
'mode of drawing a new PB
With newPictureBox 'clean use of the code
AddHandler newPictureBox.Click, _
AddressOf PictureBox_Click 'establishes events for the mouse
'activity on the objects
AddHandler newPictureBox.MouseEnter, _
AddressOf PictureBox_MouseEnter
AddHandler newPictureBox.MouseLeave, _
AddressOf PictureBox_MouseLeave
pbName += 1 'gives a unique name to the
'picturebox in an "array" style
.Location = New System.Drawing.Point _
(xPos - intPosAdj_X, yPos - intPosAdj_Y) 'establish where the box goes
'and center the object on the
'mouse pointer
.Visible = True 'ensures that the box is visible
.Name = pbName 'names the new control
End With
Controls.Add(newPictureBox) 'add control to form
End Sub
Private Sub PictureBox_Click(sender As System.Object, e As System.EventArgs)
Dim dblMouseClick As Double = CType(DirectCast _
(e, System.Windows.Forms.MouseEventArgs).Button, MouseButtons) _
'make it simple to manipulate
'the event by putting the long
'code into a variable
If dblMouseClick = MouseButtons.Left Then
MsgBox("Left CLick")
ElseIf dblMouseClick = MouseButtons.Right Then
MsgBox("right click")
Else
MsgBox("Center")
End If
This actually resolves the issue of adding and being able to select the object
Thanks for all of the suggestions and help

How to add event handler to a dynamically created control in VB.NET?

I have searched and seen countless samples here in this forum and in other sites but I'm still stuck with this problem;
I want to add a Click Handler for dynamically created PictureBox-es and pas an argument on it so I know which one of picture boxes was clicked).
Here is my current code:
Public Class frmMbarimAbonimi
Private Sub frmMbarimAbonimi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'FitnessdbDataSet.clients' table. You can move, or remove it, as needed.
'Me.ClientsTableAdapter.Fill(Me.FitnessdbDataSet.clients)
'===============
Dim dt As DataTable = PaPaguar()
Dim i As Integer = 0
Dim gr(dt.Rows.Count) As GroupBox
Dim pp(dt.Rows.Count) As PictureBox
Dim lb(dt.Rows.Count) As Label
For Each row As DataRow In dt.Rows
gr(i) = New GroupBox
gr(i).Width = 200
gr(i).Height = 180
pp(i) = New PictureBox
pp(i).SizeMode = PictureBoxSizeMode.StretchImage
lb(i) = New Label
'-------------------------
Try
Using str As Stream = File.OpenRead("C:\Fotot\" + dt.Rows(i).Item("Foto"))
pp(i).Image = Image.FromStream(str)
End Using
lb(i).Text = dt.Rows(i).Item("Emer")
Catch ex As Exception
MsgBox("Fotoja nuk mund te ngarkohet, ju lutem realizoheni nje foto tjeter!!!")
End Try
'-------------------------
pp(i).Visible = True
pp(i).Width = 200
pp(i).Height = 150
AddHandler pp(i).Click, AddressOf testini
gr(i).Controls.Add(pp(i))
lb(i).Visible = True
lb(i).Width = 200
lb(i).Height = 30
lb(i).Left = pp(i).Left
lb(i).Top = pp(i).Top + 150
lb(i).BackColor = Color.WhiteSmoke
lb(i).BringToFront()
gr(i).Controls.Add(lb(i))
flpanel.Controls.Add(gr(i))
i = i + 1
Next row
End Sub
End Class
So I was trying to use AddHandler pp(i).Click, AddressOf testini but obviously this does not allow me to call "testini" with a parameter to identify which picture box was clicked.
Can someone point me in the right direction or give some advice? Greatly appreciated.
You need to add something to your created PictureBox to identify them in the event handler because you can't change the signature of the click event handler adding a 'parameter'
For example, you could set the Name property
pp(i) = New GroupBox
pp(i).Name = "PictureBox" + i.ToString
then in the event handler you could recognize your picture box casting the sender object to a picturebox and grabbing the Name property.
Remember, sender is always the control that triggers the event. In your case is always one of your dinamically created PictureBoxes
Private Sub testini(sender As Object, e As System.EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
Dim pbIdentity As String = pb.Name
.....
End Sub