Get the month click of DateTimePicker in VB.net - vb.net

How do i check the month arrow click event of DateTimePicker. when i'm trying to change the month of datetimepicker then it is giving the 1st date of that next month so I've to prevent that month click.
Please see the image:
Thanks a lot.

Mixing the usage of CloseUp And DropDown events and Binding data may be the solution
Imports System.Windows.Forms
Namespace WindowsFormsApplication1
Public Partial Class Form1
Inherits Form
Private isDropedDown As Boolean
Private m_dataSale As DateTime = DateTime.Now
Public Property DataSale() As DateTime
Get
Return m_dataSale
End Get
Set
If isDropedDown Then
If m_dataSale.[Date].AddMonths(1).[Date] = value.[Date] OrElse m_dataSale.[Date].AddMonths(-1).[Date] = value.[Date] OrElse New DateTime(m_dataSale.Year, m_dataSale.Month, 1).AddMonths(1).[Date] = value.[Date] OrElse New DateTime(m_dataSale.Year, m_dataSale.Month, 1).AddMonths(-1).[Date] = value.[Date] Then
MessageBox.Show("Month is changing")
End If
End If
m_dataSale = value
End Set
End Property
Public Sub New()
InitializeComponent()
Dim binding = dateTimePicker1.DataBindings.Add("Value", Me, "DataSale")
binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged
End Sub
Private Sub dateTimePicker1_CloseUp(sender As Object, e As EventArgs)
isDropedDown = False
End Sub
Private Sub dateTimePicker1_DropDown(sender As Object, e As EventArgs)
isDropedDown = True
End Sub
End Class
End Namespace

Related

Changing Picture Box, BackgroundImage from an user control using another user control

I have some buttons from User Control(UserCtrl2) and want to dynamically change UserCtrl1,PictureBox BackgroundImage. From my code below, UserCtrl2 PictureBox BackgroundImage property did changed but winform is still showing the previous BackgroundImage.
I have tried the following methods,
me.refresh in UserCtrl1, still nothing happen.
Not sure how to implement {Get and Set} or dispose function.
Thanks in advance for any advise or reference.
Here is my code for UserCtrl1:
Public Class UserCtrl1
Public Sub UserCtrl1_Task(LEDno As UShort, LEDState As Boolean)
Select Case LEDno
Case 0 : Exit Sub
Case 1
If LEDState Then
PicBox_A.BackgroundImage = My.Resources.ResourceManager.GetObject("Blue_ON")
Else
PicBox_A.BackgroundImage = My.Resources.ResourceManager.GetObject("Blue_OFF")
End If
End Select
End Sub
End Class
Here is my Code for UserCtrl2:
Public Class UserCtrl2
Private ButtonClick(4) As Boolean
Private Sub Btn_A_Click(sender As Object, e As EventArgs) Handles Btn_A.Click
Dim InputControl = New UserCtrl1
If Not ButtonClick(0) Then
ButtonClick(0) = True
Btn_A.BackgroundImage = My.Resources.ResourceManager.GetObject("Switch1_ON")
InputControl.UserCtrl1_Task(1, True)
Else
ButtonClick(0) = False
Btn_A.BackgroundImage = My.Resources.ResourceManager.GetObject("Switch1_OFF")
InputControl.UserCtrl1_Task(1, False)
End If
End Sub
End Class

Set the textbox value of usercontrol from Main winForm Button

I have a main Form 'frmMaster' that contains on one button 'btnSet',
inside this main form I put a panel control contains on usercontrol,
on this user control there is one textbox,
my question is how to set the value of this textbox when I click on the button 'btnset' from main form,
for example: when I click on 'btnset' from main form, the value of textbox on the usercontrol will be "Welcome"
In userControl I Put:
Public Property TextBoxTxt () As String
Get
Return txtText1.Text
End Get
Set(value As String)
txtText1.Text = value
End Set
End Property
On Main Form I Put inside button:
Dim uc As New ucControl1
uc.txtText1.Text= "Welcome!"
Your UserControl Must be like :
Public Class UserControl1
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Property TextBoxTxt() As String
Get
Return txtText1.Text
End Get
Set(value As String)
txtText1.Text = value
End Set
End Property
End Class
In you MainForm add a button "btnSet" and a Panel "Panel1" ,so your code inside MainForm must be like :
Public Class frmMaster
Private Sub btnSet_Click(sender As Object, e As EventArgs) Handles btnSet.Click
Dim uc As New UserControl1
uc.txtText1.Text = "Welcome!"
Panel1.Controls.Add(uc)
End Sub
Private Sub frmMaster_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
I have create a simple exemple for you
In the user control:
Public Property TextBoxTxt () As String
Get
Return Me.textbox.Text
End Get
Set(value As String)
Me.txtebox.Text = value
End Set
End Property
In your even click of the button 'btnset' :
Private Sub btnset_Click(sender As Object, e As EventArgs) Handles btnset.Click
Dim uc As New MyUserControl
uc.TextBoxTxt ="Welcome!"
End Sub
You will need to add a public property to the usercontrol, e.g.
Public Property TextBoxMessage As String
Get
Return textbox.Text
End Get
Set(ByVal value As String)
textbox.Text = value
End Set
End Property
Then you can display a message from inside your frmMaster:
usercontrol.TextBoxMessage = "Welcome!"

change GroupControl visibility of opened usercontrol from the form vb.net

I have one 'UserControl' that opened inside form,
this usercontrol has a 'GroupControl'
how can I make this 'GroupControl' hidden or visible when user click on a button that beings on the form
using vb.net
In the user control:
Public Property GroupControlVisible() As Boolean
Get
Return Me.GroupControl1.Visible
End Get
Set(value As Boolean)
Me.GroupControl1.Visible = value
End Set
End Property
Try this :
In you UserControl add a procedure how set visibility to your GroupControl :
Public sub SetVisibility (V as boolean)
YourGroupControl.visible=v
End Sub
In your form
Public Class Form1
Dim uc As New MyUserControl
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Controls.Add(uc)
uc.Dock()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
uc.SetVisibility(False)
'NB :MyUserControl is name of your usercontrol
End Sub
End Class
To Simplify, you can use :
MyParentForm.UserControlName1.GroupControlName.Visible = False
or
CType(MyParentForm.Controls("UserControlName").Controls("GroupControlName"), _
GroupControl).Visible = False
But the best way is create a property which allows changing the Visible property of the GroupControl in UserControl Class like this :
Public Property GroupControlVisibility() As Boolean
Get
Return Me.GroupControlName.Visible
End Get
Set(value As Boolean)
Me.GroupControlName.Visible = value
End Set
End Property

VB.NET - Created Class, Working with WriteOnly property but its not replacing variable value

After program execution, the extension that the FileStream uses is the default one provided in the Class header instead of the one i specified via the "set property".
How come it never changed?
Form1.vb Code
Option Strict On
Imports S3_BalanceBook_Dayan.Wallet
Public Class Form1
Dim myWallet As New Wallet(DataGridView1, DateTimePicker1)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Make default selection when program starts.
optCheck.Checked = True
myWallet.StatementsFileName = "statements.dat"
DataGridView1.Rows.Add(New String() {"12/21/1986", "Test", "44554", "44.22", "45.12"})
End Sub
Private Sub cmdAddTransaction_Click(sender As System.Object, e As System.EventArgs) Handles cmdAddTransaction.Click
If optCheck.Checked Then
lblAvailableFunds.Text = FormatCurrency(myWallet.Check(CInt(Trim(txtCheck.Text)), _
CDec(Trim(txtMoney.Text))))
End If
End Sub
Private Sub optDeposit_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles optDeposit.CheckedChanged
'Disable un-needed fields when Deposit Radio button is selected!
txtCheck.Enabled = False
txtMoney.Enabled = False
txtDeposit.Enabled = True
txtFee.Enabled = False
txtDescription.Enabled = True
End Sub
Private Sub optCheck_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles optCheck.CheckedChanged
'Disable un-needed fields when Check Radio button is selected!
txtCheck.Enabled = True
txtMoney.Enabled = True
txtDeposit.Enabled = False
txtFee.Enabled = False
txtDescription.Enabled = True
End Sub
Private Sub optServiceFee_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles optServiceFee.CheckedChanged
'Disable un-needed fields when Fee Radio button is selected!
txtCheck.Enabled = False
txtMoney.Enabled = False
txtDeposit.Enabled = False
txtFee.Enabled = True
txtDescription.Enabled = True
End Sub
End Class
Wallet.vb Code
Option Strict On
Imports System
Imports System.IO
Public Class Wallet
Private lcheckNumber As Integer = Nothing
Private lcheckmoney As Decimal = Nothing
Private ldepositAmount As Decimal = Nothing
Private lfee As Decimal = Nothing
Private holdInstance As DataGridView
Private holdDate As DateTimePicker
Private holdPath As String = "default.txt"
Private _file As New FileStream(holdPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
Private file As New StreamWriter(_file)
'Default Constructor
Public Sub New()
End Sub
Public Sub New(ByRef Data As DataGridView, ByRef StatementDate As DateTimePicker)
'This constructor takes in references to use in class as private
holdInstance = Data
holdDate = StatementDate
End Sub
''' <summary>
''' Property allows the change of file path for the statements log.
''' </summary>
Public WriteOnly Property StatementsFileName() As String
Set(ByVal Value As String)
holdPath = Value
End Set
End Property
''' <summary>
''' Enter Deposit amount as Decimal, returns remainding account balance as Decimal.
''' </summary>
Public Function Deposit(ByVal Amount As Decimal) As Decimal
Return 0D
End Function
'Function Check - Deduct the amount and returns current balance.
Public Function Check(ByVal CheckNumber As Integer, ByVal CheckAmount As Decimal) As Decimal
Try
file.WriteLine(CheckNumber & " - " & CheckAmount)
Catch e As IOException
MessageBox.Show(e.ToString)
End Try
Return 0D
End Function
'Function Fee - Deduct the fee from balance and returns current balance.
Public Function Fee(ByVal FeeAmount As Decimal) As Decimal
Return 0D
End Function
End Class
Yup - it's here that the problem is:
Private holdPath As String = "default.txt"
Private _file As New FileStream(holdPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
Private file As New StreamWriter(_file)
That will initialize _file with the value of holdPath, at the point at which the Wallet instance is created. Instead of having _file and file as member fields, why not make them local variables inside Check? That way, when they're constructed, they'll use the value of holdPath, as it is at that time.
You should also, probably, put them inside Using statements, or otherwise ensure that they're closed at a suitable point - otherwise, the contents you write to the file may not appear until your program is closed.
So, we'd have:
Public Function Check(ByVal CheckNumber As Integer, ByVal CheckAmount As Decimal) As Decimal
Try
Using _file As New FileStream(holdPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
Using file As New StreamWriter(_file)
file.WriteLine(CheckNumber & " - " & CheckAmount)
End Using
End Using
Catch e As IOException
MessageBox.Show(e.ToString)
End Try
Return 0D
End Function

AddressOf with parameter

One way or another I need to link groupID (and one other integer) to the button I am dynamically adding.. any ideas?
What I can do;
AddHandler mybutton.Click, AddressOf PrintMessage
Private Sub PrintMessage(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("Dynamic event happened!")
End Sub
What I can't do, but want to;
AddHandler mybutton.Click, AddressOf PrintMessage(groupID)
Private Sub PrintMessage(ByVal groupID as Integer)
MessageBox.Show("Dynamic event happened!" & groupID .tostring)
End Sub
There is no way to do this with AddressOf itself. What you're looking for is a lambda expression.
AddHandler myButton.Click, Function(sender, e) PrintMessage(groupId)
Private Sub PrintMessage(ByVal groupID as Integer)
MessageBox.Show("Dynamic event happened!" & groupID .tostring)
End Sub
You can create your own button class and add anything you want to it
Public Class MyButton
Inherits Button
Private _groupID As Integer
Public Property GroupID() As Integer
Get
Return _groupID
End Get
Set(ByVal value As Integer)
_groupID = value
End Set
End Property
Private _anotherInteger As Integer
Public Property AnotherInteger() As Integer
Get
Return _anotherInteger
End Get
Set(ByVal value As Integer)
_anotherInteger = value
End Set
End Property
End Class
Since VB 2010 you can simply write
Public Class MyButton
Inherits Button
Public Property GroupID As Integer
Public Property AnotherInteger As Integer
End Class
You can access the button by casting the sender
Private Sub PrintMessage(ByVal sender As Object, ByVal e As EventArgs)
Dim btn = DirectCast(sender, MyButton)
MessageBox.Show( _
String.Format("GroupID = {0}, AnotherInteger = {1}", _
btn.GroupID, btn.AnotherInteger))
End Sub
These new properties can even be set in the properties window (under Misc).
The controls defined in the current project automatically appear in the toolbox.
Use the Tag property of the button.
Button1.Tag = someObject
AddressOf gets the address of a method, and thus you cannot pass parameters to it.
You can use delegate which very clear for your code follow as:
Define a delegate
Public Delegate Sub ControlClickDelegate(ByVal sender As Object, ByVal e As EventArgs)
Custom button class
Public Class CustomButton
Inherits System.Windows.Forms.Button
#Region "property delegate"
Private controlClickDelegate As ControlClickDelegate
Public Property ClickHandlerDelegate As ControlClickDelegate
Get
Return controlClickDelegate
End Get
Set(ByVal Value As ControlClickDelegate)
controlClickDelegate = Value
End Set
End Property
#End Region
Public Sub RegisterEventHandler()
AddHandler Me.Click, AddressOf OnClicking
End Sub
Private Sub OnClicking(ByVal sender As Object, e As System.EventArgs)
If (Me.controlClickDelegate IsNot Nothing) Then
Me.controlClickDelegate(sender, e)
End If
End Sub
End Class
MainForm
Public Class MainForm
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.CusButton1.ClickHandlerDelegate = AddressOf Me.btnClick
Me.CusButton1.RegisterEventHandler()
End Sub
Private Sub btnClick(ByVal sender As Object, ByVal e As EventArgs)
Me.TextBox1.Text = "Hello world"
End Sub
End Class
The below worked for me:
Dim bStart = New Button With {.Text = "START"}
AddHandler bStart.Click, Function(sender, e) TriggerProcess(any Long value)
Private Function TriggerProcess(ByVal paramName As Long) As Boolean
' any processing logic
Return True
End Function
My solution:
AddHandler menuItemYear.Items(i).MouseUp, Sub() menu_year(2019)
Private Sub menu_year(ByVal intYear As Integer)
'do something
End Sub
There are few ways to do that depending of the complexity and number of parameters required.
1. Use Tag for adding a complex structure
2. Inherit the the Button class and add the values as class members then populate them before using it. That gives you a lot more flexibility.
If you are using web version
3. You cannot add it to Tag, but for simple values assign it to index use .Attributes.Add("name"). This gets added to the HTML tags and not the Server side. You can then use the index to access a server side structure for complex systems.
4. Use sessions to store values and store the session reference to Name attribute as described above (#3).
No problem ;-)
For example:
Private ComboActionsOnValueChanged As New Dictionary(Of ComboBox, EventHandler)
'somewhere in function
dim del = Sub(theSender, eventArgs)
MsgBox(CType(theSender, ComboBox).Name & " test")
End Sub
ComboActionsOnValueChanged.Add(myCombo, del)
'somewhere else
Dim delTest = ComboActionsOnValueChanged(myCombo)
RemoveHandler myCombo.SelectedValueChanged, delTest
myCombo.DataSource = someDataSource
AddHandler myCombo.SelectedValueChanged, delTest
as we expect, event won't fire after DataSource change in this place