Make a button have multiple uses - vb.net

okay... How do I explain this without being totally confusing?... Alright, I have this form that has MenuScripts (top-levels and second-levels). The problem that I am having is one of the second-levels is "Add" which brings you to another form when clicked. This other form has a button ("Record") and text boxes. This other form allows the user to input data and when the record button is clicked, the inputted data is written into a text file. Ok, so back to the first form. Another second-level MenuScript is "Update" which also brings the user to the other form; but first, the user has to click an item within a listbox to proceed. How do I get the data from the selected item to appear in the appropriate textboxes and how do I get the record button to update data instead of being confused and thinking it is only a add-data button?
Is there a way to use an "if" statement to say something like "if mnuAdd is clicked then" "elseif mnuUpdate is clicked then". Would something like that work for giving the record button multiple uses?
Also, if someone can give me some pointers on making sure the user selects an item within the listbox would definitely be a plus! Thanks, guys!
Unfortunately, I cannot add images since my reputation is too low.
Here is a visual representation of my ultimate goal

Easiest way: before displaying the second form set it's Tag property to something distinct – say "Add" or "Update" – depending on which menu item is selected. Then you just test the Tag value in the button's Click event and proceed accordingly.
As for determining whether a list item is selected: well if there isn't the ListBox's SelectedIndex property will be set to -1.

You need to put a public property on the second form (Details) which specifies which mode it is in. For instance, you could create a mode enumeration like this:
Public Enum EntryModes
AddBook
UpdateBook
End Enum
Then, define a public mode property on the second form, like this:
Public Property EntryMode As EntryModes
Get
Return _entryMode
End Get
Set(ByVal value As EntryMode)
_entryMode = value
End Set
End Property
Private _entryMode As EntryMode
Then, when you show the second form from the menu, just set the property first, before showing it:
Private Sub mnuAdd_Click(sender As Object, e As EventArgs)
Dim dialog As New DetailsDialog()
dialog.EntryMode = EntryModes.AddBook
dialog.ShowDialog()
End Sub
Private Sub mnuUpdate_Click(sender As Object, e As EventArgs)
Dim dialog As New DetailsDialog()
dialog.EntryMode = EntryModes.UpdateBook
dialog.BookToUpdate = ListBox1.SelectedItem
dialog.ShowDialog()
End Sub
As you can see, in the Upate menu click, I also added a line that passes the information for which book should be updated.

Related

How do I properly use if/then for a bolean value in order to make an object visible?

I want to display an endless access form. For each data set there is a yes/no value (PA). If yes, then a hidden object should be displayed. It seems very straight forward, but it doesn't work.
I have tried by changing the value of PA to 1, 0, -1. Either nothing happens, or the object will be displayed for all data sets.
The object is defined as hidden in the form.
Private sub form_current()
If PA.value = true Then
me.object.visible = True
End if
End Sub
I would be very happy for some advice. /LP
Handle the control's Change event, and then you can assign to its value:
Private Sub PA_Change()
Me.object.Visible = PA.Value ' TODO: give 'object' an actual name
End Sub
Find PA in the top-left codepane dropdown, then select the Change event in the top-right code pane dropdown if it's not automatically selected - the VBE will generate the event handler procedure for you.
As the object is unbound, you can't do this. When unbound, it will either be visible or not - for all records.
One workaround is to move the control to a tiny subform having a master/child relation to the main form.

Combobox not updating bindtable fields in VB

I have a combobox that is of type dropdown, i.e. allows user to either select something from the list or to type in a new value. Based on this combo value, other values are selected in the form automatically as they are databound.
Customer_ID is the field that needs to be picked or typed in, and based on it Customer first name and second name are updated in the form automatically. This works fine if I pick some value from dropdown, lets say Customer Id = 1111.
Now in place of picking, if i type in this value and press tab, no udpate happens on the Name fields. Please suggest what am I missing here.
I think the best you are going to do with the standard ComboBox is to handle the Leave event, or possibly the TextChanged event. I did some experimenting and typing in a value does not add to the list, it just changes the Text property. It doesn't even change the value of the item you typed over. And, it doesn't fire SelectedItem or IndexChanged events.
So if you handle the Leave event, you have a place to do your lookup when someone tabs off the ComboBox. But the item won't get added to the list.
If you need functionality not provided in the default control, you could redesign your UI with perhaps a TextBox and Button that add an item. Or, you could look for third party implementations of ComboBox controls that would have more functionality.
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
Dim theCB As ComboBox = DirectCast(sender, ComboBox)
DoLookup(theCB.Text)
End Sub

Access Subform Source object

What I am trying to achieve is for a combo box (Combo_sf) selection to dictate the form in the subform control (sf_record) I have about 10 forms, their names are in the combo box data. I am new to VBA and am not sure if my approach is right:
Private Sub Combo_sf_AfterUpdate()
Dim strLoadTable As String
strLoadTable = "Form." & Me.Combo_sf.Value
MsgBox strLoadTable
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
End Sub
I have placed this in the combobox's after update event but when I make my selection nothing happens in the form. Am I approaching this right or would another way work better?
Your approach should work. I put a combo box named cbxSubform on my main form and added one line of code to its AfterUpdate() event handler...
Private Sub cbxSubform_AfterUpdate()
Me.mySubform.SourceObject = Me.cbxSubform.Value
End Sub
...and changing the selection in the combo box switches the subforms immediately. Are you sure that the AfterUpdate() code for your combo box is actually firing? (You could add a MsgBox or a Debug.Print to check.)
It could be this line which is tripping you up:
strLoadTable = "Form." & Me.Combo_sf.Value
What is your form object called? If your form is called Form.myTableName it could be the . that is throwing it out, try setting it to a form without a dot in its name.
In this line, it seems the code attempts to change the SourceObject property of a Form object.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
However, SourceObject is a property of a subform control, not the form contained in that control. So if the subform control is named sf_record, do it this way.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.SourceObject = strLoadTable
Also, if the after update procedure runs from [frm_Mnu_Manage Configuration Settings], you can use Me to refer to the form.
Me!sf_record.SourceObject = strLoadTable
Finally, if Me.Combo_sf.Value is the name of a form, you don't need to prefix its name with "Form.". It worked either way in my test, but I would just leave off "Form.".
strLoadTable = Me.Combo_sf.Value

Dynamic Textboxes

I have a gridview - GridView1.
I am adding rows to this Gridview1 dyanamically through code.
One of the columns is a dropdownlist that reads from a sqldatasource.
The textboxes, dropdownlists & sql datasources are all arrays.
If I change the value of the dropdownlist, it maintains its state even after the page reloads on any button click event.
This is ok.
However, the values of the textboxes are not maintained.
Say I enter "Hello World" in the textbox & click "Add" button, I want to collect the text value in dropdownlist(which I can read) & the value in textbox(which returns blank.)
Please suggest a method so that on add button click I can retrive the value I had typed in the textbox.
Each textbox has a unique id & I tried using the ID to get its value
eg
protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) handles add.click
{
Dim valueinText = gettext(1).text
}
now if I type "Hello World" in textbox: gettext(1),
Reults:
valueinText = ""
Thanks in advance
You will have to reset the values for unbound textboxes that get their value from code when you post back. In your Page Load Event...
If Page.IsPostback Then
'Code that uses dropdownlistAtoZ.SelectedValue
'to fill in correct value for Textboxes.
End If
I was trying to create the same textboxes on every page_load event. But this just erased their values & their client_ID kept changing.
Instead, I tried making all textboxes Shared and created them only once on add click event, added them to a Row (which is again shared) and on page_load if page.postback = true I just added the row to the table again. If you don't do so, the row will not be shown on reload.
This solved my problem(now the values entered in Textbox were not cleared like before).
I accessed the value by classname.textboxname(i).text.
Now the solution seems obvious, but I spent a few days trying to solve this.
Thanks!

Canceling a TabControl tab selection in VB.NET

Really wracking my brain here and I'm sure it's something simple I'm missing.
Basically I have a form with two tabs. I'm checking the controls on each tab to see if they're dirty and want to prevent a user from clicking a tab if there are changes on the current tab.
I had thought if I check the dirty variable and just set the tab index to the one that hasn't been selected I'd be fine but every time I programatically set a tab's property, it fires off a bunch of the tab's events that just produce an undesireable result such as the right control set is showing but the wrong tab is selected.
Here is the code for as close as I've gotten to getting it to work.
Private Sub objTabs_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles objTabs.Selected
If bIsDirty Then
If Me.objTabs.SelectedIndex = 1 Then
Me.objTabs.SelectedTab = tabLetterofCreditBanks
Me.objTabs.SelectTab(0)
Else
Me.objTabs.SelectedTab = tabWireTransferBanks
Me.objTabs.SelectTab(1)
End If
End If
End Sub
Thanks in advance.
Try using the Selecting event instead of the Selected event - this will give you the opportunity to override the user's behavior (i.e. setting TabControlCancelEventArgs.Cancel to True).