How to Pass Different EventArgs to another sub - vb.net

I'm new to VB.NET and I was experimenting with trying to call a Sub from another. I have the first Sub that handles a button then executes some code. I then want to call the other Sub I have that handles a rectangle being tapped. I'm having trouble call the second Sub from the first Sub. I think it might be the EventArgs. The first Sub is RoutedEventsArgs and the second is TappedRoutedEventArgs. I can't seem to find any documentation about this particular instance. I only see if it as the same e.
Public Class MainPage
Private Sub Show_Message(sender As Object, e As RoutedEventArgs) Handles Button_1.Click
If Message.Visibility = Visibility.Collapsed Then
Message.Visibility = Visibility.Visible
Else
Message.Visibility = Visibility.Collapsed
End If
ChangeColorItem_Click(sender, e)
End Sub
Private Sub ChangeColorItem_Click(sender As Object, e As TappedRoutedEventArgs) Handles Rectangle.Tapped
'Change fill color to red to blue and back
If rectangleFill.Color = (Windows.UI.Colors.Red) Then
rectangleFill.Color = Windows.UI.Colors.Blue
Else
rectangleFill.Color = Windows.UI.Colors.Red
End If
End Sub
End Class
I want it so that if Show_Message is run also run the ChangeColorItem_Click.

RoutedEventArgs is a square peg, TappedRoutedEventArgs is a round hole... you're trying to put a square peg in a round hole... and you don't need to.
Unless there's a need for the event args, and I don't see the event args being used, you're better off putting the code into their own sub and simply calling it.
Public Class MainPage
Private Sub Show_Message(sender As Object, e As RoutedEventArgs) Handles Button_1.Click
ShowMessage()
End Sub
Private Sub ChangeColorItem_Click(sender As Object, e As TappedRoutedEventArgs) Handles Rectangle.Tapped
ChangeColorItem()
End Sub
Private Sub ShowMessage()
If Message.Visibility = Visibility.Collapsed Then
Message.Visibility = Visibility.Visible
Else
Message.Visibility = Visibility.Collapsed
End If
ChangeColorItem()
End Sub
Private Sub ChangeColorItem()
'Change fill color to red to blue and back
If rectangleFill.Color = (Windows.UI.Colors.Red) Then
rectangleFill.Color = Windows.UI.Colors.Blue
Else
rectangleFill.Color = Windows.UI.Colors.Red
End If
End Sub
End Class

Related

Adding an event handler to custom control

I have created a custom control (Check Box) with a custom EventHandler
Public Event CheckedChanged As EventHandler
Private Sub setCheckStateUI(sender As Object, e As EventArgs)
...
RaiseEvent CheckedChanged(sender, e)
End Sub
It works fine without any errors if I added this control directly to a form. But when I add this to another custom control (a page of settings window) and that second custom control add to a form (settings window) the 'settings window' freeze and visual studio auto restart.
If I removed this event handler in the code the problem is gone.
What can be the problem here?
Thanks in advance
Update: (Complete code of the Custom Control)
Public Class cusCheckBox
Private mystring As String
Private CheckButtonState As Integer = 0
Public Event CheckedChanged As EventHandler
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
mystring = Me.Name
setSizes()
End Sub
Public Property CheckState() As Integer
Get
CheckState = CheckButtonState
End Get
Set(ByVal value As Integer)
CheckButtonState = value
chkButton.CheckState = CheckButtonState
End Set
End Property
Public Property LabelText() As String
Get
LabelText = mystring
End Get
Set(ByVal value As String)
mystring = value
lblText.Text = mystring
setSizes()
End Set
End Property
Public Overrides Property Font As Font
Get
Return lblText.Font
End Get
Set(value As Font)
lblText.Font = value
End Set
End Property
Private Sub chkButton_CheckedChanged(sender As Object, e As EventArgs) Handles chkButton.CheckedChanged
If chkButton.CheckState = 1 Then
chkButton.Image = Global.MYLogs.My.Resources.Resources.btnToggleOn
CheckButtonState = 1
Else
chkButton.Image = Global.MYLogs.My.Resources.Resources.btnToggleOff
CheckButtonState = 0
End If
End Sub
Private Sub lblText_Click(sender As Object, e As EventArgs) Handles lblText.Click
setCheckStateUI(sender, e)
End Sub
Private Sub cusCheckBox_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
setCheckStateUI(sender, e)
End Sub
Private Sub cusCheckBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
setCheckStateUI(sender, e)
setSizes()
End Sub
Private Sub cusCheckBox_Resize(sender As Object, e As EventArgs) Handles Me.Resize
setSizes()
End Sub
Private Sub setSizes()
Me.Size = New Size(chkButton.Width + lblText.Width + 4, chkButton.Height)
End Sub
Private Sub setCheckStateUI(sender As Object, e As EventArgs)
If chkButton.CheckState = 0 Then
chkButton.Image = Global.MYLogs.My.Resources.Resources.btnToggleOn
chkButton.CheckState = 1
CheckButtonState = 1
Else
chkButton.Image = Global.MYLogs.My.Resources.Resources.btnToggleOff
chkButton.CheckState = 0
CheckButtonState = 0
End If
RaiseEvent CheckedChanged(Me, EventArgs.Empty)
chkButton.Select()
End Sub
End Class

Better way to accomplish simple task in VB.Net

So, I have a small test for school where I need to DISABLE three radio buttons which are in a panel in the least amount of possible code WHILE the contents of two text boxes are empty. As soon as both textboxes are filled, I enable the panel.
The below solution obviously works with one single text box, but what happens with two?
I know I can override each button KeyPress and check both text boxes at the same time. But I wanted to go fancy. The problem is I think there is no way I can solve this problem like this.
Or is it?
Public Class Form1
Public Sub vacios(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)
Panel1.Enabled = (sender.Text <> "")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler txtNombre.KeyPress, AddressOf vacios
AddHandler txtApellido.KeyPress, AddressOf vacios
End Sub
End Class
Note - I know I can do this But I wanted to be fancy.
Public Class Form1
Private Sub txtNombre_TextChanged(sender As Object, e As EventArgs) Handles txtNombre.TextChanged
Panel1.Enabled = (txtNombre.Text <> "" And txtApellido.Text <> "")
End Sub
Private Sub txtApellido_TextChanged(sender As Object, e As EventArgs) Handles txtApellido.TextChanged
Panel1.Enabled = (txtNombre.Text <> "" And txtApellido.Text <> "")
End Sub
End Class
in VB you can have multiple handles for the same sub just by adding "Handles Object1.Event,Object2.Event ..." at the end.
here an example of what i would do in this case
Public Class Form1
Private Sub panel1TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles txtApellido.TextChanged,txtNombre.TextChanged
Panel1.Enabled = (txtNombre.Text <> String.Empty And txtApellido.Text <> String.Empty)
End Sub
End Class

PictureBox is not showing up on button click event

I am using a PictureBox inside a button click event. When the button is clicked I am enabling the PictureBox and I am running a long database call and at the end of the process, I am trying to disable the PictureBox. Inside the PictureBox I have a loading GIF.
But I don't know what's happening. My PictureBox does not show up..
Please suggest how can I fix this. I tried Thread.Sleep(1000), but it didn't work.
Private Sub btnRetrieve_Click(sender As Object, e As EventArgs) Handles btnRetrieve.Click
Me.PictureBox1.Visible = True
lblSuccess.Text = Nothing
UltraNumberOfConveyance.Value = Nothing
GetData() --Long Running Query
Me.PictureBox1.Visible = False
End Sub
My GetData Function:
Private Function GetData()
dsCheckPointTimes = GetCheckPointTimesByTerminalID()
dtDataTable = dsCheckPointTimes.Tables(0)
chkdtDataTable = dsCheckPointTimes.Tables(1)
If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("ConveyanceName")) Then
lblConveyanceNameText.Text = chkdtDataTable.Rows.Item(0).Item("ConveyanceName").ToString()
End If
If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("NumberOfConveyance")) Then
UltraNumberOfConveyance.Value = chkdtDataTable.Rows.Item(0).Item("NumberOfConveyance")
End If
If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("Dock")) Then
UltratxtChangeLabel1.Value = chkdtDataTable.Rows.Item(0).Item("Dock")
End If
If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("Lines")) Then
UltratxtChangeLabel2.Value = chkdtDataTable.Rows.Item(0).Item("Lines")
End If
If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("CHECKPOINTTYPE")) Then
SetFields(chkdtDataTable.Rows.Item(0).Item("CHECKPOINTTYPE"))
End If
If dtDataTable.Rows.Count > 0 Then
LoadFlow()
Else
lblSuccess.Text = "No Records Found! Please check the ordernumber"
lblSuccess.ForeColor = Color.Red
End If
Return Nothing
End Function
Use a BackgroundWorker to do this:
Private WithEvents bgw As New BackgroundWorker
Private Sub btnRetrieve_Click(sender As Object, e As EventArgs) Handles btnRetrieve.Click
PictureBox1.Visible = True
bgw.RunWorkerAsync()
End Sub
Private Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork
GetData() --Long Running Query
End Sub
Private Sub bgw_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
PictureBox1.Visible = False
End Sub

Automatic SelectAll on all textboxes and numericUpDown controls on gotfocus

I have such code to make all text in textbox selected on got_focus:
Private Sub myText_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles myText.GotFocus
myText.SelectAll()
End Sub
Is here a way in VB.NET to get that all TextBoxes and NumericUpDown controls selects his text on _GotFocus or _Enter without to explicitly set such behavior for every single control and no matter how this control gets a focus (keyboard, mouse or programmatic)?
Yes, there is and very simple.
Private Sub TextBox2_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox2.GotFocus
TextBox2.Select(0, TextBox2.Text.Length)
End Sub
Public Class MyTextBox
Inherits System.Windows.Forms.TextBox
Private _focused As Boolean
Protected Overrides Sub OnEnter(e As EventArgs)
MyBase.OnEnter(e)
If MouseButtons = MouseButtons.None Then
SelectAll()
_focused = True
End If
End Sub
Protected Overrides Sub OnLeave(e As EventArgs)
MyBase.OnLeave(e)
_focused = False
End Sub
Protected Overrides Sub OnMouseUp(mevent As MouseEventArgs)
MyBase.OnMouseUp(mevent)
If Not _focused Then
If SelectionLength = 0 Then
SelectAll()
End If
_focused = True
End If
End Sub
End Class

Making combobox visible when it is disabled

I am disabling combobox in VB.net.
But in disable mode it not visible properly.
I tried changing both BackColor and ForeColor but it is not working.
Code :
cmbbox.BackColor = Color.FromName("Window")
or
cmbbox.ForeColor = Color.FromName("Window")
Please help
Dear Adam:
I am making my component enable false.But I want to make it viewable.You can reffer the link.This is what exacly I want but in VB.Net : A combobox that looks decent when it is disabled
To achieve disabling combobox without fading it, first change the dropdown style of the combobox to DropDownList, Then tweak with the events to achieve the goal.
Here is a piece of code by which you can achieve the same:
Public Class Form1
Dim selectindex As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("1")
ComboBox1.Items.Add("2")
ComboBox1.Items.Add("3")
ComboBox1.Items.Add("4")
selectindex = 3
ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
ComboBox1.SelectedIndex = selectindex
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
ComboBox1.SelectedIndex = selectindex
End Sub
End Class
Create anew form Form1 and add a combobox to the form, then add the above code to get a readonly combobox.
Have a look at this thread which has a solution for a readonly combobox and the code is all VB.NET.
A version of their code is as follows. You'll need to but it inside a class of your own which inherits System.Windows.Forms.ComboBox
Private _ReadOnly As Boolean = False
Public Property [ReadOnly]() As Boolean
Get
Return _ReadOnly
End Get
Set(ByVal Value As Boolean)
_ReadOnly = Value
End Set
End Property
Public Overrides Function PreProcessMessage(ByRef msg As Message) As Boolean
'Prevent keyboard entry if control is ReadOnly
If _ReadOnly = True Then
'Check if its a keydown message
If msg.Msg = &H100 Then
'Get the key that was pressed
Dim key As Int32 = msg.WParam.ToInt32
'Ignore navigation keys
If key = Keys.Tab Or key = Keys.Left Or key = Keys.Right Then
'Do nothing
Else
Return True
End If
End If
End If
'Call base method so delegates receive event
Return MyBase.PreProcessMessage(msg)
End Function
Protected Overrides Sub WndProc(ByRef m As Message)
'Prevent list displaying if ReadOnly
If _ReadOnly = True Then
If m.Msg = &H201 OrElse m.Msg = &H203 Then
Return
End If
End If
'Call base method so delegates receive event
MyBase.WndProc(m)
End Sub
I've been looking for the same not long ago and ended up doing the following. You may not like it, but i'll share it in case. I am using TableLayoutPanel to arrange my controls on the form and then i am swapping the positions of the desired controls.
For example I've created the following Items:
Form1 Design
TableLayoutPanel1 (two columns, three rows)
TextBox1 Read-only = True, BackColor = White
ComboBox1 Visible = False, DropDownStyle = DropDownList, FlatStyle = Popup
Button1 (named it to Change)
Button2 (named it to Done) -> Visible = False
Runtime - Screenshots
Here is my code:
Public Class Form1
Private Sub SwapControls(tlp As TableLayoutPanel, ctr1 As Control, ctr2 As Control)
Dim ctl1pos As TableLayoutPanelCellPosition = tlp.GetPositionFromControl(ctr1)
ctr1.Visible = False
tlp.SetCellPosition(ctr1, tlp.GetPositionFromControl(ctr2))
ctr2.Visible = True
tlp.SetCellPosition(ctr2, ctl1pos)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SwapControls(TableLayoutPanel1, TextBox1, ComboBox1)
SwapControls(TableLayoutPanel1, Button1, Button2)
Label1.Select()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SwapControls(TableLayoutPanel1, ComboBox1, TextBox1)
SwapControls(TableLayoutPanel1, Button2, Button1)
Label1.Select()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Select()
ComboBox1.SelectedIndex = 0
TextBox1.Text = ComboBox1.SelectedItem
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox1.Text = ComboBox1.SelectedItem
End Sub
End Class