I am trying to add some items to a combo box in VB, but when I add the items to the click event handler of the combo-box, and run the code, the items added do not show.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.Refresh()
ComboBox1.Items.Clear()
ComboBox1.Items.Add("Mondad")
ComboBox1.Items.Add("Tuesday")
ComboBox1.Items.Add("Wenesday")
ComboBox1.SelectedIndex = 1
End Sub
I mean nothing shows inside the combo-box.
I add the items to the click event handler of the combo-box
Pay special attention to the name of the method:
ComboBox1_SelectedIndexChanged()
Note the emphasis. It sure looks like this is NOT the click event. The click event method would look like this:
ComboBox1_Click(object sender, EventArgs e)
Just changing the name of the method will not be enough, because the method still will not be wired up correctly. Create a new empty event handler in Visual Studio for the click event and move the code there.
Just add this to your code i think it will work
Private Sub ComboBox1_Click(sender As Object, e As EventArgs) Handles ComboBox1.Click
ComboBox1.Items.Clear()
ComboBox1.Items.Add("Mondad")
ComboBox1.Items.Add("Tuesday")
ComboBox1.Items.Add("Wenesday")
ComboBox1.SelectedIndex = 1
End Sub
hope it helps
Put in form load
` Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.Clear()
ComboBox1.Items.Add("Mondad")
ComboBox1.Items.Add("Tuesday")
ComboBox1.Items.Add("Wenesday")
End Sub`
Related
I have a user control (XListBox) with a listbox (ListBox1) and a button on it. The user control is on a form (Form1) which also has a button on it. The code is shown below. If click the button inside the user control the text 'Add Item from inside XListBox' appears in the Listbox no problem. If I click the button on the form however, the string 'Add item to XListBox from form' does not get added to the listbox although the Debug.Print does show it in the output window. It appears that I am missing something but I have no idea what. Any help would be appreciated. Thanks in advance.
Public Class Form1
Dim lstActions As New XListBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lstActions.AddItem("Add item to XListBox from form")
End Sub
End Class
Public Class XListBox ' user control
Private Sub XListBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add("Add Item from inside XListBox")
End Sub
Public Sub AddItem(sString As String)
ListBox1.Items.Add(sString)
Debug.Print(sString)
End Sub
End Class
Why do you have this in your code there?
Dim lstActions As New XListBox
Surely you added the user control to your form in the designer, like you would any other control. How do you usually access a control that you added to the form in the designer? Basically, you are ignoring the one you added in the designer, i.e. the one you can see, and you have created another one that you never add to the form, so you can't see it, and that's the one you're adding the items to. Don't. Get rid of that field and use the one you added in the designer.
I have several buttons in which all of them have come codes. I want the user to only can make click the buttons once. How can I do it without having a long disable buttons code?
Dim oneClick As Integer
Private Sub btnPro_Click(sender As Object, e As EventArgs) Handles btnPro.Click
oneClick += 1
If oneClick = 1 Then
Dim ucP As New ucPro
fillMenu(ucP)
End If
End Sub
This is the code I come up with.
EDIT : The point is that I want to click a button once, but the others can be clicked. For example, I have a form with 6 buttons, every button has some codes. If I click Button1, it'll do such code only once, therefore if I click it again, it will not do that code, because it already did it. Then, if I click Button2, it'll do the code once. And what about if I click Button1 again? well, it'll be able to do that code because I clicked to another button.
Sorry for the way I explain myself. I hope you get it.
Keep track of last clicked button and you can use one handler for all involved buttons
Private _lastClickedButton As Button = Nothing
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button = DirectCast(sender, Button)
If button Is _lastClickedButton Then Exit Sub
_lastClickedButton = button
' Now based on button instance you can execute corresponding logic/method
End Sub
Based on your rather poor explanation, it sounds like what you actually mean is that you only want each button to act once in a row, i.e. once you click a button you need to click another button before the first button does anything again. In that case, the proper way to handle this is to not use Button controls.
Instead of using Button controls and handling their Click events, you should be using RadioButton controls and handling their CheckedChanged events. You can set the Appearance property to Button and they will look just like regular Buttons. When checked, they will appear depressed and that will indicate to the user that they can't be used again.
Here's an example of a form using such controls:
And here's what the appropriate code might look like:
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked Then
'Do something.
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton1.Checked Then
'Do something else.
End If
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
If RadioButton1.Checked Then
'Do yet something else.
End If
End Sub
Here's what the form might look like when a RadioButton has been checked:
As you can see, it's nice and clear to the user which one they cannot use.
EDIT:
If you are really determined to use Button controls then you absolutely should be disabling them as that is the standard way that feedback has been provided to the user for decades. It's very simple to disable a Button if and only if it is the last one clicked. Here's an example:
Private Sub AllButtons_Click(sender As Object, e As EventArgs) Handles Button3.Click,
Button2.Click,
Button1.Click
For Each btn In Controls.OfType(Of Button)
btn.Enabled = (btn IsNot sender)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Do something.
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Do something else.
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Do yet something else.
End Sub
Note that you can handle multiple events with a single method and multiple methods can handle a single event, both of which are demonstrated here. All four of those methods were generated automatically by the designer, so I just had to add the body code. Note that the first method assumes that all Button controls on the form will be treated this way. If that's not the case then would need a slight adjustment but nothing too major.
Okay I realize the question is ambiguous, but I didn't know what else to name it. As all of you know if I double click a textbox in visual basic it gives me this code automatically.
Private Sub textBox1_Click(sender As Object, e As EventArgs) Handles textBox1.Click
'do stuff here
End Sub
What do you click or otherwise have to do to get this to show up automatically?
Private Sub textBox1_Enter(sender As Object, e As EventArgs) Handles textBox1.Enter
'do stuff here
End Sub
What about this one as well
Private Sub textBox1_TextChanged(sender As Object, e As EventArgs) Handles textBox1.TextChanged
'do stuff here
End Sub
Select the textbox. In the Properties pane/window (press F4 if can't see it), click the icon which looks a bit ilke a lightning bolt to get a list of the available handlers. Double-click the one you want and it will create the template for you.
I have created a game in visual basic, and have decided to expand by adding multiple rooms on the game. Each room will be a panel that shows when the user enters a certain area. To start, I created a class that inherits panel properties, then added it to my main Form. I then added all the items to the panel controls. My issue is that i cannot control the player when it is in the panel controls. I have tried all sorts, but couldn't get it to work.
I did some reading and found that panel does not come with keyUp() and keyDown() events. However, I would like to know if there is a way round this.
Thanks in advance!
Code of mainRoom() class (my panel):
Public Class mainRoom
Inherits Panel
Sub New()
With Me
.SetBounds(0, 0, Form1.Width, Form1.Height)
.SendToBack()
End With
End Sub
Public Sub mainRoom_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint
e.Graphics.FillRectangle(Brushes.DarkOrange, block)
End Sub
End Class
In main form class:
Sub setMap()
main = New mainRoom()
Me.Controls.Add(main)
End Sub
You can set the KeyPreview=True on the main form, and then handle the key up/down events from the form:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
'key down code.
e.Handled = True
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
'key up code.
e.Handled = True
End Sub
Note: if you don't want other controls' key up/down event handlers to fire after your form event handlers do, then set e.Handled=True as I have in the above example.
See MSDN for more info on KeyPreview.
BEWARE - when moving (cut/paste) existing controls via the IDE from a form into a new panel control, the event handlers are not carried across as they become children of the parent control (the new panel). So this happens:
Private Sub Button1_Click(sender As Object, e As EventArgs)
Add the event handlers back and don't spend an age googling why keypress events suddenly stop firing in panels ...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If I want to set the focus on a textbox when the form is first opened, then at design time, I can set it's tabOrder property to 0 and make sure no other form control has a tabOrder of 0.
If I want to achieve the same result at run-time, using code, how should I proceed?
Are there alternatives to using tabOrder?
I assume any run-time code will be in the form's constructor or its onload event handler?
EDIT
In other words I'd like to be able to type straight into the textbox as soon as the form appears without having to manually tab to it, or manually select it.
Because you want to set it when the form loads, you have to first .Show() the form before you can call the .Focus() method. The form cannot take focus in the Load event until you show the form
Private Sub RibbonForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Show()
TextBox1.Select()
End Sub
I think what you're looking for is:
textBox1.Select();
in the constructor. (This is in C#. Maybe in VB that would be the same but without the semicolon.)
From http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx :
Focus is a low-level method intended primarily for custom control
authors. Instead, application programmers should use the Select method
or the ActiveControl property for child controls, or the Activate
method for forms.
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
TextBox1.Select()
End Sub
Using Focus method
Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
yourControl.Focus()
End Sub
To set focus,
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
TextBox1.Focus()
End Sub
Set the TabIndex by
Me.TextBox1.TabIndex = 0
Quite simple :
For the tab control, you need to handle the _SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab.Name = "TabPage1" Then
TextBox2.Focus()
End If
If TabControl1.SelectedTab.Name = "TabPage2" Then
TextBox4.Focus()
End If
I think the appropriate event handler to use is "Shown".
And you only need to focus the appropriate text box.
Private Sub Me_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
myTextbox.Focus()
End Sub
create a textbox:
<TextBox Name="tb">
..hello..
</TextBox>
focus() ---> it is used to set input focus to the textbox control
tb.focus()