Chexbox ChekState functionality in Windows Form - vb.net

I need a checkbox which will ask the user:"Do you need to add an address?" and if user clicks on it then address label and textbox appears on the form ( I mean By default they were invisible and then I have changed this state), I have implemented this functionality by CheckboxSate event but what if the user doesn't click on the checkbox at all, in this case after submission external details I am not able to move forward ( i mean after clicking on the next button my form don't call another subform, but when I check/uncheck checkbox it works correctly), is there any way to edit/update form validation or any default checkbox property in order to get rid of this bag?, here is my CheckState Event's code:
Private Sub AddAddress_CheckedStatrChange_1(sender As Object, e As EventArgs) Handles AddAddress.CheckStateChanged
If AddAddress.CheckState = CheckState.Checked Then
AddAddressLabel.Visible = True
AddAddressTextBox.Visible = True
AddAddressTextBox.Enabled = True
ElseIf AddAddress.CheckState = CheckState.Unchecked Then
AddAddressLabel.Visible = False
AddAddressTextBox.Visible = False
AddAddressTextBox.Enabled = False
End If
END Sub

That's how you would do:
Sub AddFTP_CheckedChanged() Handles AddFTP.CheckedChanged
AddAddressLabel.Visible = Not AddAddressLabel.Visible
AddAddressTextBox.Visible = Not AddAddressTextBox.Visible
AddAddressTextBox.Enabled = Not AddAddressTextBox.Enabled 'It's useless since it's not visible
End Sub

Related

Why won't the button hotkey show up with an underline in VB?

I just started a self-paced course in Visual Basic using Visual Studio. One of my assigned problems is to create a form with two buttons. When the form loads, Button1 is enabled and Button2 is disabled. When you click on Button1, Button1 is disabled and Button2 is enabled. When you then click on Button2, Button2 is disabled and Button1 is enabled.
I got that to work easily, so I decided to add an extra challenge to myself. The challenge is that I want to make the disabled button show the text "Disabled" and the enabled button show "Enabled" with the "E" underlined as the hotkey for the button. I set a string variable for the enabled button containing the string "&Enabled" to enable the "E" as the hotkey. The "E" works as the hotkey, but it does not display with an underline.
I have searched the web for a fix to this issue, but I've come up dry. I also tried resizing the buttons to see if the buttons were too small to display the underline. That did not work. I have scrutinized my code, but I really don't know the language well enough to understand why the "E" does not show up with an underline. I am submitting my code and asking for help. I want to understand why this doesn't work the way I expect it to work.
This is the VB.Net code that I wrote for the form using Visual Studio 2019.
Public Class frmEnabledProblem
Dim blnButton1Enabled As Boolean = True
Dim blnButton2Enabled As Boolean = False
Dim strButton1Enabled As String = "&Enabled"
Dim strButton1Disabled As String = "Disabled"
Dim strButton2Enabled As String = "&Enabled"
Dim strButton2Disabled As String = "Disabled"
Private Sub frmEnabledProblem_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
btnButton2.Text = strButton2Disabled
btnButton2.Enabled = False
btnButton1.Text = strButton1Enabled
btnButton1.Enabled = True
End Sub
Private Sub btnButton1_Click(sender As Object, e As EventArgs) Handles btnButton1.Click
blnButton1Enabled = False
blnButton2Enabled = True
btnButton1.Enabled = blnButton1Enabled
btnButton2.Enabled = blnButton2Enabled
btnButton1.Text = strButton1Disabled
btnButton2.Text = strButton2Enabled
End Sub
Private Sub btnButton2_Click(sender As Object, e As EventArgs) Handles btnButton2.Click
blnButton1Enabled = True
blnButton2Enabled = False
btnButton1.Enabled = blnButton1Enabled
btnButton2.Enabled = blnButton2Enabled
btnButton1.Text = strButton1Enabled
btnButton2.Text = strButton2Disabled
End Sub
End Class
Thanks for your help.
In a nutshell, because this setting in Ease of Access/Keyboard is off on your computer:
Turn it on and restart your app
I wouldn't personally advocate going out of your way to force it on for your app even when it's off elsewhere in the system; just provide the &OK text on your button, and let the user decide if they want it to show like OK or O̲K depnding on their own settings/appetite for how they want their UI

Disable Button If Combox Input Deleted

In my project, I have a few textbox inputs, and some combo boxes with maybe 2 indexed items on a form. There's a button I'm disabling on load if no input is supplied to both textbox inputs, and it works great even if I delete out any text. However, I'm having issues with forcing the combo box to behave in the same manner. This work however:
Private Sub cboPickShirts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPickShirts.SelectedIndexChanged
InputCheck_3 = True
If cboPickShirts.SelectedIndex < 0 Then
InputCheck_3 = False
End If
If InputCheck_3 = False Then
btnInputResult.Enabled = False
ElseIf InputCheck_3 = True Then
btnInputResult.Enabled = True
End If
End Sub
I have InputCheck_3 set up as a global variable in a Public Module. On form load, I'm disabling my button and it doesn't enable until I select one of the indexed items. My struggle to get the button disable again if any combo box text is entered and deleted out, leaving it null or empty. Any thoughts on what I'm missing or what I can add to get results? I guess I need a variable or event to notice the change (entering & deletion of text).
The problem you are having is that your SelectedIndexChanged event is not being triggered when you remove the selected item from your ComboBox. I would use the TextChanged event of your TextBox's and ComboBox and give it a common handler and check it that way. Something like this
Private Sub TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, cboPickShirts.TextChanged
EnableCheck()
End Sub
Private Sub EnableCheck()
btnInputResult.Enabled = (String.IsNullOrEmpty(TextBox1.Text) And String.IsNullOrEmpty(TextBox2.Text) And ComboBox1.SelectedIndex = -1)
End Sub
You can also check that the comboBox is NullorEmpty the same way as the textbox's. As it stands right now the combobox will be enabled when the text no longer matches a selection.
One line code
btnInputResult.Enabled = If((cboPickShirts.SelectedIndex<0),False, True)

Checkbox does does not work in VB.Net form

I have two checkboxes for two groupboxes to enable visibility or invisibility of each one at a time but somehow one is working(chboNew) the other one(chboIssue) doesn't!
here is the code I have written for it, any help would be appreciated:
Private Sub chboIssue_CheckStateChanged(sender As Object, e As EventArgs) Handles chboIssue.CheckStateChanged
If chboIssue.Checked = True Then
gbIssueSearch.Visible = True
gbNewSearch.Visible = False
chboNew.Checked = False
ElseIf chboIssue.Checked = False Then
gbIssueSearch.Visible = False
End If
End Sub
Private Sub chboNew_CheckStateChanged(sender As Object, e As EventArgs) Handles chboNew.CheckStateChanged
If chboNew.Checked = True Then
gbNewSearch.Visible = True
gbIssueSearch.Visible = False
chboIssue.Checked = False
ElseIf chboIssue.Checked = False Then
gbNewSearch.Visible = False
End If
End Sub
If user has to choose between new issue and issue search,one at a time.
Then you should use radio buttons, instead of checkbox.
Checkbox gives idea that user can choose both checkboxes at same time.
Which in your case is not true.
Changing the name of the checkboxes is not going to solve your issue. I noticed that for your chboNew.CheckStateChanged event handler in your elseif clause you are checking if chboIssue is checked, whereas in your other handler for chboIssue both your if/else clauses look at chboIssue. I'm thinking that may be part of your issue. Also, if only one of these boxes is supposed to be checked at a time, you may want to add logic to automatically uncheck the other whenever one is checked. For instance, in your chboNew handler, "If chboNew.Checked = True Then chboIssue = False", and the inverse in your chboIssue handler. Hope this helps.

How do I make it so that when a variable is true in "Form1," it does something in "Form2?"

So, I'm trying to make it so when the user clicks the login button on the login form, it sets a variable to true, then it makes something activate on the main form.
Some pseudo code that would be placed in Form 2:
If Form1.button = clicked Then
do something
end if
Thanks for any help!
By the way, I'm a complete noob with vb.net. Sorry.
On button click event write below
If Form1.button = clicked Then
Dim myForm As New Form2
if LoggedIn = True then
myForm.UserGroupBox.visible = true
me.close
myForm.Show()
end if
End if
Double click your Application name in the solution explorer then go to settings and create a new one with the name: clicked and the value on : 0 and write on the button1 in the form1
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
My.Settings.clicked = "1"
form1.show
me.hide
End Sub
and on the form were you want the something
if my.settings.clicked = "1" then
'Do Something what you want here
my.settings.clicked = "0"
end if
end sub
what this code does is:
when you click then button if gives the my.settings.clickde an value 1 = true
and when the other form loads its gonna check if the value = 1 if the value = 1 then it clicks the button and then changes the clicked value back to 0
so its not gonna repeat forever
hope this helps you and dont forget if it helps click button_up on my post

VB.NET Windows Forms context menu position

I am enhancing a previously written VB.NET WIndows forms application.
In one of the forms, there is a DataGridView populated from a stored procedure at Form Load.
If the user has the privileges, when the user right clicks on a record in the datagridview, I want to show a context menu allowing the user to delete the record.
Here is my code:
Private Sub m_SetEnabledContextMenu()
' for Delete record
If Not objUser.HasAuthority("Delete Record") Then
Me.DataGridView1.ContextMenu = Nothing
Me.mnuContextEquationSubmission.Enabled = False
Else
' Me.DataGridView1.ContextMenu = Me.mnuContextEquationSubmission
Me.mnuContextEquationSubmission.Enabled = True
End If
'Rest is not problem
End Sub
Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
Handles DataGridView1.CellMouseDown
If e.Button = System.Windows.Forms.MouseButtons.Right Then
For Each row As DataGridViewRow In DataGridView1.SelectedRows
row.Selected = False
Next
DataGridView1.Rows(e.RowIndex).Selected = True
'MessageBox.Show("DataGridView1_CellMouseDown on row " & e.RowIndex.ToString)
m_intSelRow = e.RowIndex
m_intSelCol = e.ColumnIndex
m_strRecordID = DataGridView1.Rows(e.RowIndex).Cells(0).Value
'mnuContextEquationSubmission.Show(DataGridView1, e.Location)
mnuContextEquationSubmission.Show(CType(sender, Control), e.Location)
End If
End Sub
as you can see, in the procedure 'm_SetEnabledContextMenu' I determine whether of not the user will have the authority to delete records, and thus the context menu will be activated (or at least, that's what I want) - whilst of the user doesn't have the authority, the menu should be invisible and disabled.
In the event handler DataGridView1_CellMouseDown, if it's a right-click and if the user has clicked in a data row, then I want the context menu to display where the mouse is, NOT at the top!
Use mnuContextEquationSubmission.Show(Me, Me.PointToClient(MousePosition))