how to hide and show text if checkbox is checked vb.net - vb.net

So far my code is like this and they only enable and disable the Textbox.
Private Sub CheckBox17_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox17.CheckedChanged
If CheckBox17.Checked = True Then
TextBox1.Enabled = False
ElseIf CheckBox17.Checked = False Then
TextBox1.Enabled = True
End If
End Sub
I need some codes that hide the text when the checkbox is checked and show it when checked.

When the checkbox is checked copy the textbox contents to a string variable and set the textbox text property to an empty string (consideder setting textbox enabled to false). If the check is checked assign the string variable to the textbox text property again.

a cording to question i this this example help
this example show How to Hide and Show the Password Using a CheckBox
i create a new Windows Form Application. After that, do the Form just like this.
Double click the checkbox and do this following code in the method. This method will set the password into bullets to hide it, or set it into letters to know the password that you have input exactly according to your desire.
'CHECKING IF THE CHECKBOX WAS CHECKED OR NOT.
If CheckBox1.CheckState = CheckState.Checked Then
'IF TRUE, IT SHOW THE TEXT
txtpass.UseSystemPasswordChar = False
Else
'IF FALSE, IT WILL HIDE THE TEXT AND IT WILL TURN IT INTIO BULLETS.
txtpass.UseSystemPasswordChar = True
End If
Go back to the design views, double click the form and do this following code in the Form_Load.
'HIDE THE TEXT OF THE TXTPASS ON THE FIRST LOAD
txtpass.UseSystemPasswordChar = True
These are the full codes that you have made.
Public Class Form1
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
'CHECKING IF THE CHECKBOX WAS CHECKED OR NOT.
If CheckBox1.CheckState = CheckState.Checked Then
'IF TRUE, IT SHOWS THE TEXT
txtpass.UseSystemPasswordChar = False
Else
'IF FALSE, IT WILL HIDE THE TEXT AND IT WILL TURN INTO BULLETS.
txtpass.UseSystemPasswordChar = True
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'HIDE THE TEXT OF THE TXTPASS ON THE FIRST LOAD
txtpass.UseSystemPasswordChar = True
End Sub
End Class

If I understand correctly, you just want the textbox to be unseen should this box be checked. Hope I'm reading that correctly. So you would just use the visible property.
Private Sub CheckBox17_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox17.CheckedChanged
If CheckBox17.Checked = True Then
TextBox1.Visible = False
TextBox1.Enabled = False
ElseIf CheckBox17.Checked = False Then
TextBox1.Visible = True
TextBox1.Enabled = True
End If
End Sub
Is this what you were looking for?

I ended up setting the font color for the text to the same as the background color.. and its gone:
<asp:CheckBox ID="cb_DataReview" class="standardCheckbox" runat="server" Width="90%" **ForeColor="white"** TabIndex="115" />

Related

How can I make textboxes appear/disappear depending on a checkbox state?

I have an application that I'm developing for a school project. This is what the application looks like
Essentially whenever one of the CheckBoxes is checked, a TextBox.Visible property is changed to true and is supposed to appear underneath the checkbox. You can have all three of them checked (or any combination checked) if you like, as long as when you uncheck it the TextBox disappears and the CheckBox appears empty/unchecked.
I've gotten to the point where I can make the TextBoxes appear and disappear but the TextBoxes are never empty. There's always a black square there that looks like this
Those black squares don't go away and I'm not sure exactly what the problem is. The TextBox also only appears when the CheckBox has that square as opposed to an actual check which is what is required. I have used a combination of If...ElseIf statements and Select Cases, which haven't done it. I've tried a few different events like CheckChanged and Click.
This is the code that I currently use that allows me to toggle the boxes.
Private Sub chkBox_Click(sender As Object, e As EventArgs) Handles chkBox.Click
If chkBox.Checked = False Then
txtBox.Visible = False
txtBox.Text = ""
Else
txtBox.Visible = True
End If
chkBox.Checked = True
End Sub
Private Sub chkLawn_Click(sender As Object, e As EventArgs) Handles chkLawn.Click
If chkLawn.Checked = False Then
txtLawn.Visible = False
txtLawn.Text = ""
Else
txtLawn.Visible = True
End If
chkLawn.Checked = True
End Sub
Private Sub chkPav_Click(sender As Object, e As EventArgs) Handles chkPav.Click
If chkPav.Checked = False Then
txtPav.Visible = False
txtPav.Text = ""
Else
txtPav.Visible = True
End If
chkPav.Checked = True
End Sub
If you guys can think of a solution or could point me in the right direction I would appreciate that.
I'd recommend this in the form load to setup a relationship between the checkboxes and the textboxes:
chkBox.Tag = txtBox
chkLawn.Tag = txtLawn
chkPav.Tag = txtPav
Then one handler:
Private Sub chkBox_Click(sender As Object, e As EventArgs) Handles chkBox.Click, chkPav.Click, chkLawn.Click
CType(sender.Tag, TextBox).Visible = CType(sender, Checkbox).Checked
End Sub
Try to remove the chkPav.Checked = True, chkLawn.Checked = True and chkBox.Checked = True in your .click events.
Also, i would recommand to use the "CheckStateChanged" vb.net event.
This will handle all your CheckBox.CheckChanged events. It finds the TextBox based on the name of the CheckBox. So just name them the same as you have (i.e. chkA and txtA).
Private textBoxPrefix As String = "txt"
Private checkBoxPrefix As String = "chk"
Private Sub chk_CheckedChanged(sender As Object, e As EventArgs) Handles chkBox.CheckedChanged, chkLawn.CheckedChanged, chkPav.CheckedChanged
Dim chk = CType(sender, CheckBox)
Dim suffix = chk.Name.Substring(checkBoxPrefix.Length)
Dim txt = Me.Controls().Find(textBoxPrefix & suffix, True).Single()
txt.Visible = chk.Checked
txt.Text = If(chk.Checked, "", txt.Text)
End Sub
Making it a little more scaleable, add handlers to all CheckBoxes in the GroupBox in Form_Load programmatically. (remove Handles chkBox.CheckedChanged, chkLawn.CheckedChanged, chkPav.CheckedChanged from the event handler declaration)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' assuming the GroupBox is named gbTickets, add all handlers programmatically
For Each chk As CheckBox In Me.gbTickets.Controls.OfType(Of CheckBox)
AddHandler chk.CheckedChanged, AddressOf chk_CheckedChanged
Next
End Sub

vb.net - enable/disable a button depending on user input

As I'm putting the finishing touches on my program I'm having some troubles.
Theres several user inputs and a submit button, once the inputs has been filled I wish to enable the submit button, else the button should be disabled. This is what I have:
Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
If Input1.Text = "" Then
ButtonSubmit.Enabled = False
ElseIf Input1.Text <> "" Then
ButtonSubmit.Enabled = True
End If
End Sub
The thing is it disables nomatter what and then it doesnt enable when my input is filed
Your code will work if you have another control that can receive the focus. Control Validation occurs on the loss of focus. If you need to have just one focusable item active you will need to use either KeyPress, KeyDown or Textchanged events to enable your button, also make sure that the CausesValidation property of your TextBox is true.
I would also make the method more generic so you could call it from multiple textbox's by using the sender object to access the textbox that raised the event. Also if you have a True/False condition you only need to do the comparison in the first if statement and then you just use an else not an elseif.
for example:
Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
If DirectCast(sender, TextBox).Text = "" Then
ButtonSubmit.Enabled = False
Else
ButtonSubmit.Enabled = True
End If
End Sub
You can also use the String.IsNullOrWhiteSpace Method to check if just spaces have been entered if you are using the 4.0 framework or above. Like this TextChanged EventHandler.
Private Sub ButtonControl(sender As Object, e As EventArgs) Handles Input1.TextChanged
If String.IsNullOrWhiteSpace(DirectCast(sender, TextBox).Text) Then
ButtonSubmit.Enabled = False
Else
ButtonSubmit.Enabled = True
End If
End Sub
You are going to need to use the TextBox "TextChanged" event, and be sure to set each Textbox AutoPostback="True". You can use an UpdatePanel to make the postbacks that occur on each Textbox you wish to validate less obnoxious to your end-user.
So, your textbox (if you have many, make sure they all have the OnTextChanged="ValidateForm":
<asp:TextBox ID="Input1" runat="server" OnTextChanged="Validate_TextChanged" />
Inside your textchanged ("ValidateForm") event (which each Textbox is attached to), one quick to implement route to do would just be
' Validation inside this event
Protected Sub Validate_TextChanged(sender As Object, e As EventArgs)
if Input1.text <> "" AndAlso Input2.text <> "" AndAlso ' ...etc.
End Sub
If you go the route of the UpdatePanel, you may find this useful.
This is the kind of thing I would do:
Private Sub TextBoxes_TextChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles _
TextBox1.TextChanged, _
TextBox2.TextChanged, _
TextBox3.TextChanged
Dim textBoxes = { TextBox1, TextBox2, TextBox3 }
Button1.Enabled = textBoxes.All(Function (tb) tb.Text <> "")
End Sub
You can then add as many text boxes in to the textBoxes array as you need to check. Just make sure that the text boxes to the Handles list.
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length > 0 Then
Me.Button1.Enabled = True
Else
Me.Button1.Enabled = False
End If
End Sub
Do that code in the Input1_TextChanged event
This is what I did and worked:
Dim CheckInput1 As Boolean
Private Sub Input1_TextChanged(sender As Object, e As EventArgs) Handles Input1.TextChanged, Input1.Enter
CheckInput1 = True
If Input1.Text = "" Then
CheckInput1 = False
End If
If CheckInput1 = False Then
ButtonSubmit.Enabled = False
ElseIf CheckInput1 = True Then
ButtonSubmit.Enabled = True
End If
End Sub
There must be a more efficient code that this but I think this solves your problem.

Making one column in DataGridView read only

Within my program i have included a datagridview that is filled when the form loads. When it first loads i have set the whole form to read-only. However if the user then wants to edit the data within it they can click an edit button i have included on the form, this has the code:
datagrdSnippets.AllowUserToDeleteRows = True 'Allows user to delete rows
datagrdSnippets.ReadOnly = False 'Allows user to edit cells within the data grid
However i do not want one of the columns within the datagridview to be made editable, what code can i use to do this?
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Column1.ReadOnly = True
Column2.ReadOnly = True
Column3.ReadOnly = True
End Sub
set readonly true your desired column on form load event
Me, I have data source from database in my dataGridView so I use for loop to get the exact column address that I want to make ReadOnly=true and the rest is ReadOnly=false
Code:
For i = datagridview1.columns.count - 1 to 0 Step -1
If i = (YourSpecificColumnAddress) Then
Datagridview1.columns(i).ReadOnly=true
Else
Datagridview1.columns(i).ReadOnly=false
End if
Next
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
datagrdSnippets.Columns(0).ReadOnly = True
datagrdSnippets.Columns(1).ReadOnly = True
datagrdSnippets.Columns(2).ReadOnly = True
End Sub
dataGrid.Columns(index).ReadOnly = True
dataGrid.Columns(index).ReadOnly = True
dataGrid.Columns("column_name").ReadOnly = True

Enable and disable TextBox with a checkbox in visual basic

I've 6 TextBox and 6 CheckBox. Now I want to disable the TextBox1 with a CheckBox1 and reactivate it with the Same CheckBox.
How a can do it?
Edit1 15.55 14/02/2013
I have done so to solve my problem!
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = False
ElseIf CheckBox1.Checked = False Then
TextBox1.Enabled = True
End If
End Sub `
This will work, just add more for the other check boxes
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = True
Else
TextBox1.Enabled = False
End If
End Sub
What this does: if checkbox1 is check, the checked_changed event fires and the code inside is ran. The if statement looks to see if the checkbox is checked or not. If it is checked, then it sets the textbox1 to enabled, if not it sets it to disabled. Be sure to set the enabled property to either enabled or disabled when you create your program. If you want it to be enabled from the start, that is the default....otherwise set it to disabled in its properties view.
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Enabled = CheckBox1.Checked
End Sub
Take a look at this tutorial below. After, look at the checkbox control's events and choose the most fitting one. The property you will be changing on the textbox is Enabled.
http://www.youtube.com/watch?v=4PbUryXqZ50
This works if you have a layer built in that you can send objects behind (therefore hide things). I use this as a way to make text boxes and other items appear and disappear depending on other selections.
Private Sub checkbox_Click()
If (checkbox = True) Then
ActiveSheet.Shapes("textbox").ZOrder msoSendToFront
ActiveSheet.Shapes("textbox").ZOrder msoSendToFront
Else
ActiveSheet.Shapes("textbox").ZOrder msoSendToBack
ActiveSheet.Shapes("textbox").ZOrder msoSendToBack
End If
End Sub
This worked for me:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Enabled = False
If Not TextBox1.Enabled Then
TextBox1.BackColor = Color.White
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = True
Else
TextBox1.Enabled = False
End If
End Sub
End Class

ComboBox's SelectedIndexChanged event not being called on Enter

I'm working on VS 2010 with VB using .NET Framework 4.0
I have a combobox. It has some items in it and displays just fine. Here's where it gets a little weird:
If I click the drop-down arrow on the combobox and CLICK on the item I want, SelectedIndexChanged is called - good.
If I click inside the text area of the combobox and start typing what I want selected and finish it by pressing the up (or down) key, SelectedIndexChanged is called - also good.
If I click the drop-down arrow on the combobox and start typing what I want selected and finish it by pressing ENTER, SelectedIndexChanged is not called - PROBLEM.
Is there a different event that is caused by the ENTER in the last case? I've tried using the TextChanged and TextUpdate events, but those do not seem to be working:
Private Sub cmbStatus_TextChanged(sender As System.Object, e As System.EventArgs) Handles cmbStatus.TextChanged
If e.Equals(Keys.Enter) Then
Call SomeMethod()
End If
Should I use something besides e.Equals(Keys.Enter)?
Is there another event I should be looking for?
EDIT:
An example of the items in the ComboBox are:
10 - NEW ENTRY AND COMPLETENESS CHECK ---> this is the most common type
13 - ASSIGNED TO TRB/HRB ---> there are a few with '/'
60 - EXTERNAL (HOLD UNTIL FURTHER NOTICE) ---> there are a few with '(' and ')'
Basically, the type of each listing is "## - SOME TEXT".
Disclaimer: this is written in C# - let me know if you need it translated to VB.
private void comboBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
//It's important to also check that the Combo Box is displaying its Drop Down. If
//you want this to execute even when it is not displayed, remove the check for
//comboBox1.DroppedDown.
if (e.KeyCode == Keys.Enter && comboBox1.DroppedDown &&
!string.IsNullOrEmpty(comboBox1.Text))
{
int index;
//Attempt to locate the string typed in by the user. An index of -1
//means it was not found.
if ((index = comboBox1.FindStringExact(comboBox1.Text)) != -1)
{
//Update the SelectedIndex.
comboBox1.SelectedIndex = index;
}
}
}
Interestingly, the docs say that we should be using the SelectionChangeCommitted event instead of the SelectedIndexChanged event when handling selection changes made by the user. It is necessary to do so in this case as the SelectedIndexChanged event fires twice using my approach.
Edit:
To prevent the user from having to type the entire string, use the advice from Adi's answer: go to the properties of the combo box and set set AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems - I actully used these settings when creating my answer, so it should work for you.
Option Strict On
Public Class Form1
Friend WithEvents ComboBox1 As New ComboBox With {.Parent = Me}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange({"hello", "tes1ted", "word", "item", "tes2ted"})
ComboBox1.Text = ComboBox1.Items(0).ToString
End Sub
Private Sub ComboBox1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles ComboBox1.KeyUp
'You can put this in the keydown event, or adapt it a small bit and put it in the keypress event
'putting it in the textchanged event is problematic and not recommended.
Dim OriginalText As String = ComboBox1.Text
If e.KeyCode = Keys.Enter Then
If ComboBox1.SelectionLength > 0 Then
ComboBox1.Text = ComboBox1.Text
ComboBox1.SelectionLength = 0
ComboBox1.SelectionStart = ComboBox1.Text.Length
End If
End If
If Not IsTextKey(e.KeyCode) Then Exit Sub
Dim Filter As String = ComboBox1.Text & "*"
If Filter.Length = 1 Then Exit Sub
For I = 0 To ComboBox1.Items.Count - 1
If LCase(ComboBox1.Items(I).ToString) Like LCase(Filter) Then
ComboBox1.SelectedItem = ComboBox1.Items(I)
ComboBox1.Select(OriginalText.Length, (ComboBox1.Text.Length - OriginalText.Length))
Exit Sub
End If
Next
End Sub
Function IsTextKey(ByVal Key As Integer) As Boolean
Select Case True
Case Key = Keys.Up : Return False
Case Key = Keys.Down : Return False
Case Key = Keys.Left : Return False
Case Key = Keys.Right : Return False
Case Key = Keys.Back : Return False
Case Key = Keys.Delete : Return False
Case Key = Keys.LWin : Return False
Case Key = Keys.RWin : Return False
'add whatever I missed
'return false if the key either removes text from the textbox
'or does not produce a character
Case Else
'return true if the key produces a visible character(including space)
Return True
End Select
End Function
End Class
Private Sub ComboBox1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
If e.KeyCode = Keys.Enter Then
MessageBox.Show(ComboBox1.SelectedText)
Call SomeMethod()
End If
End Sub
I believe that you should set AutoCompleteMode to SuggestAppend and AutoCompleteSource to ListItems. This way, if what you type in is sustainable with the items loaded in the ComboBox, by writing it down and finding that item, when pressed Enter, the SelectedIndexChanged will be fired (even if a match wouldn't be found - the first in the list will be selected)
I've prepared something to point out this to you.
Regards,
Adi Konstantin
Subscribe to the KeyPressed event:
Private Sub yourComboBox_KeyPressed(sender As System.Object, e As System.KeyPressedEventArgs) Handles yourComboBox.KeyPressed
If e.KeyChar.Equals((char)Keys.Enter) Then
Call SomeMethod()
End If
this will helps your problems
Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter Then
MsgBox("hello")'call some functions
End If
End Sub
Can you let me know if this works for you?
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
If e.KeyChar = Chr(13) Then
ComboBox1_SelectedIndexChanged(sender, e)
End If
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
MsgBox(ComboBox1.Text)
'Your code here
End Sub
I had a similar problem when the combobox dropdownstyle was set to simple with autocomplete set to append, sourcing from my listitems. My workaround was to have a string variable save the combobox text upon each textchanged event. That way, when ENTER is pressed, you can retrieve the deleted text and reassign it to the combobox text.
'Global declaration
'Global declaraion
dim selected_text as string = ""
Private Sub combobox_textchanged(sender As Object, e As EventArgs) Handles combobox.TextChanged
If combobox.Text IsNot Nothing And combobox.Text <> "" Then
selected_text = combobox.Text
End If
End Sub
Private Sub combobox_keydown(sender As Object, e As KeyEventArgs) Handles combobox.KeyDown
If e.KeyCode = Keys.Enter Then
combobox.Text = selected_text
'put logic here
End If
End Sub
There are some great answers above, but I liked that I didn't need to sort out all the command keys and arrows etc.
Close the list manualy on PreviewKeyDown event:
Private Sub cmbStatus_PreviewKeyDown(sender As ComboBox, e As PreviewKeyDownEventArgs) Handles cmbStatus.PreviewKeyDown
If e.KeyCode = Keys.Enter Then sender.DroppedDown = False
End Sub