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
Related
When I press the chkCP1, it unchecks chkYP but chkCP doesn't display its checked state2; I need to double click chkCP before it displays its checked state3.
I used these codes:
Private Sub chkCP_CheckedChanged(sender As Object, e As EventArgs) Handles chkCP.CheckedChanged
chkYP.Checked = False
End Sub
Private Sub chkYP_CheckedChanged(sender As Object, e As EventArgs) Handles chkYP.CheckedChanged
chkCP.Checked = False
End Sub
Figure 1:
Figure 2:
Figure 3:
I would personally use radiobuttons for this as this is what they were intended to do. However, I have seen a time where the option was to select neither option like you can easily do with checkboxes. That being said, You should be able to achieve the desired result by just simply moving your original code into the click event of the checkboxes instead of the checkchanged event. The reason is that when you click one, it triggers the checkchanged event which sets it to false which in turn triggers that controls checkchanged event. Try replacing your original code with
Private Sub chkCP_Click(sender As Object, e As EventArgs) Handles chkCP.Click
chkYP.Checked = False
End Sub
Private Sub chkYP_Click(sender As Object, e As EventArgs) Handles chkYP.Click
chkCP.Checked = False
End Sub
edit:
I tried using if statements and it worked! However, I cannot uncheck the checkbox anymore.
Private Sub chkCP_CheckedChanged(sender As Object, e As EventArgs) Handles chkCP.CheckedChanged
If chkYP.Checked = True Then
chkYP.Checked = False
Else
chkCP.Checked = True
End If
End Sub
Private Sub chkYP_CheckedChanged(sender As Object, e As EventArgs) Handles chkYP.CheckedChanged
If chkCP.Checked = True Then
chkCP.Checked = False
Else
chkYP.Checked = True
End If
End Sub
You might have some issues with recursive event handlers here. If you set chkYP.Checked in chkCP_CheckedChanged, chkYP_CheckedChanged will be triggered. This sets chkCP.Checked, which triggers chkCP_CheckedChanged again.
You might try something like this:
Private _checking As Boolean
Private Sub chkCP_CheckedChanged(sender As Object, e As EventArgs) Handles chkCP.CheckedChanged
If Not _checking Then
_checking = True
chkYP.Checked = False
_checking = False
End If
End Sub
Private Sub chkYP_CheckedChanged(sender As Object, e As EventArgs) Handles chkYP.CheckedChanged
If Not _checking Then
_checking = True
chkCP.Checked = False
_checking = False
End If
End Sub
It may not win a beauty contest, but it might just do the job.
Using Radio Buttons might be a better solution if you want only one of N options selected.
Edit:
Charles May's answer is way more elegant. He handles the Click event instead of the CheckedChanged event. And that also seems to work fine when using the keyboard (pressing the spacebar to toggle the checkbox).
So I have a program set up so if checkbox 1 is checked, it starts me.keypreview and starts timer3. Then I have timer 3 checking if Spacebar is held down and if it is, it starts Timer1. For some reason, the code doesnt pick up that spacebar is held down and it detects left click instead, When I tried clicking left click it started timer1.
Here is my code:
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
hotkey = GetAsyncKeyState(Keys.Space)
If CBool(hotkey) = True Then
Timer1.Start()
Else
Timer1.Stop()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
Me.KeyPreview = True
Timer3.Start()
Else
Me.KeyPreview = False
Timer3.Stop()
End If
End Sub
End Class
Can anybody help?
i am new at VB.net.. i am making one Application for my friend. but i have one problem while using toolstrip...
i want to use toolstrip menu as tabmenu... like if i select any button from toolstrip menu, than form content change..just like while we change tab then form content will change which is inside that tab...is it possible to do so?
I don't have any code at the moment so i can not attach it..i have tried googling my problem but i didn't found any solution of this problem...hope you guys understand my problem..thank you!
If I understand you correctly, for each "tab" you could create a panel on your form and set the Visible property of each to False. Then for each button click event in your ToolStrip, you could make them all Visible=False apart from the one you want to show.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Panel1.Visible = False
Panel2.Visible = False
Panel3.Visible = False
End Sub
Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
Panel1.Visible = True
Panel2.Visible = False
Panel3.Visible = False
End Sub
Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
Panel1.Visible = False
Panel2.Visible = True
Panel3.Visible = False
End Sub
Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
Panel1.Visible = False
Panel2.Visible = False
Panel3.Visible = True
End Sub
End Class
Here, I have two side-by-side panel the first_pnl and second_pnl, the second panel is not visible by default. Initial thoughts of what I need:
If my cursor is over the the first one (MouseEnter)
It's BackColor would change to Black
Second panel would be visible
If my cursor leaves first one (MouseLeave)
It's BackColor would change back to Gray
Second panel would not be visible
Which is simple as:
Private Sub PanelMouseEnter(sender As Object, e As EventArgs) _
Handles first_pnl.MouseEnter
first_pnl.BackColor = Color.Black
second_pnl.Visible = True
End Sub
Private Sub PanelMouseLeave(sender As Object, e As EventArgs) _
Handles first_pnl.MouseLeave
first_pnl.BackColor = Color.Gray
second_pnl.Visible = False
End Sub
But what I want to happen is:
When the cursor moves to the second panel (which by now is visible)
second_pnl would remain visible unless the cursor leave its area.
It sustains first_pnl property as if it where on a MouseEnter event
And here's the scenario to be clear:
And here's my logic of making that possible: (Giving their same events with same code)
Private Sub PanelMouseEnter(sender As Object, e As EventArgs) _
Handles first_pnl.MouseEnter, second_pnl.MouseEnter
first_pnl.BackColor = Color.Black
second_pnl.Visible = True
End Sub
Private Sub PanelMouseLeave(sender As Object, e As EventArgs) _
Handles first_pnl.MouseLeave, second_pnl.MouseLeave
first_pnl.BackColor = Color.Gray
second_pnl.Visible = False
End Sub
Looks reasonable, but I think the system consider first the MouseLeave of first_pnl before it even consider the MouseEnter of second_pnl.
Any way to do it?
jmcilhinney's comment solves this easily.
Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
first_pnl.BackColor = Color.Gray
second_pnl.Visible = False
End Sub
Private Sub first_pnl_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles first_pnl.MouseEnter
first_pnl.BackColor = Color.Black
second_pnl.Visible = True
End Sub
I am using VB.NET and I have:
a Start button
a Stop button
a While loop
When the Start button is pressed, the While loop begins. I want to stop the loop when the Stop button is pressed.
I had tried to use Applications.DoEvents, but the loop stops when the Stop button is pressed twice.
Below is my code using Applications.DoEvents
Dim stopclick As Boolean = False
Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
btnForward.Enabled = False
btnStop.Enabled = True
While
' Perform the while statements
If stopclick = True Then
stopclick = False
Application.DoEvents()
Exit While
End If
End While
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
stopClick = True
End Sub
Have I used Application.DoEvents incorrectly?
What are the other options besides Application.DoEvents? How can I stop the loop with a single Stop button click?
You need to put the call to Application.DoEvents outside of your If statement, like this:
While True
Application.DoEvents()
' Perform the while statements
If stopclick Then
stopclick = False
Exit While
End If
End While
The Stop_Click won't have a chance to be processed until you call DoEvents, so, with the way you had it, stopClick would never get set to True.
However, the larger issue, here, is that calling DoEvents, at all, is very bad practice and can lead to some very difficult-to-fix bugs if you aren't very careful. It would be far better if the loop was performed in a background thread. Check out the BackgroundWorker component for an easy to implement threading mechanism for WinForm projects. You'll find it in the tool box of your form designer.
Here's an example of how to do it with the BackgroundWorker component:
Dim stopclick As Boolean = False
Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
btnForward.Enabled = False
btnStop.Enabled = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
stopClick = True
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
stopClick = False
While Not stopClick
' Perform the while statements
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
btnForward.Enabled = True
btnStop.Enabled = False
End Sub
You can simplify your while loop significantly by using stopClick as your condition:
While Not stopClick
Application.DoEvents()
End While
I know this is an old thread, but I found a simple workaround. Add a CheckBox to the form (it can even be "off the side of the form," if that made sense).
Private Sub btnStopLoop_Click(sender As System.Object, e As System.EventArgs) Handles btnStopLoop.Click
chkEndLoop.CheckState = CheckState.Checked
End Sub
Then, in the Loop you wish to exit:
Do Until chkEndLoop.CheckState = CheckState.Checked
'Do Stuff Here
Loop
As long as the CheckBox.CheckState = CheckState.Unchecked, the Loop will continue, but when the CheckBox becomes Checked, the Loop will Exit
You have to use btnstop.focus() at the start of the loop. This will be sure to solve your problem.
Example:
Dim stopclick As Boolean = False
Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
btnForward.Enabled = False
btnStop.Enabled = True
btnStop.Focus() ' new code---------------------
While
' Perform the while statements
If stopclick = True Then
stopclick = False
Application.DoEvents()
Exit While
End If
End While
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
stopClick = True
End Sub