Make choice from combo box enable different combo box - visual-studio

I am new to this website, so I apologize if this question was asked and I could not find it.
I am trying to create a program using Visual Studio 2010 where if you select a word from either list A or B, (where lists and B are combo boxes), then it gets defined on the bottom of the screen.
The problem I am having is making a selection from a combo box with the choices for lists A and B enable/disable the appropriate list.
To phrase that better, I have a 3 combo box lists, one with choices "English and Hebrew", one titled cboEnglish, and one titled cboHebrew.
How do I make it so that if I select "English", then cboEnglish is enabled, and if I select "Hebrew", then THAT list is enabled?
I threw a bit of coding around, but all it accomplishes is that no matter what choice I pick, then the English gets enabled every time:
Private Sub English()
cboHebrew.Enabled = False
cboEnglish.Enabled = True
End Sub
Private Sub Hebrew()
cboEnglish.Enabled = False
cboHebrew.Enabled = True
End Sub
I had a draft where the choices English and Hebrew were buttons, which would make the coding really easy to do (The above code was copied from that version), but I did not really like how it looked.
I am fairly new to Visual Studio (like 1 1/2 months of use), so I apologize if this is a dumb question...

Figured it out. I ended up turning the main combo box into an integer, and defining each of the sub options to make the other boxes true/false using the same procedure as if I would have just been using 2 buttons to enable/disable them.
Not sure if this procedure is ideal, but hey, it works (for now)
Dim intLanguage As Integer
intLanguage = Me.cboLanguage.SelectedIndex
Select Case intLanguage
Case 0
English()
Case 1
Hebrew()
End Select
End Sub
Private Sub English()
'This attempts to enable English list
cboEnglish.Enabled = True
cboHebrew.Enabled = False
End Sub
Private Sub Hebrew()
'This attempts to enable English list
cboEnglish.Enabled = False
cboHebrew.Enabled = True
End Sub
End Class

I'm not into Visual Basic, but basically you should place your code into handler method. In your case it should SelectedIndexCHanged (see http://www.tutorialspoint.com/vb.net/vb.net_combobox.htm).

A simple if then else statement should work.
If combobox.optionA = selected Then
comboxBox.optionB = false
Else comobox.optionB = selected then
comboxBox.optionA = false
END if

Related

Access-VBA for loops

I am quite new to VB coding but I do understand some of the basics. I want to create a loop that checks the value within 8 text boxes and enabled a tick box next to the respected text box if it not blank. I am not quite sure on the syntax. I have 8 text boxes called Textbox1 : Textbox8 and 8 checkboxes with names checkbox1-8. This is the code I have already but I just need a bit of help with the syntax.
For i = 1 To 8
If Textbox(i) = "" Then
checkbox(i).enabled = false
else
checkbox(i).enabled = true
End If
Next
Something like:
For i = 1 To 8
Me.Controls("checkbox" & i).Enabled = (Nz(Me.Controls("Textbox" & i).Value) <> "")
Next

Optimize performance of Removing Hidden Rows in VBA

I am using the following code to remove hidden/filtered lines after applying autofilters to a big sheet in VBA (big means roughly 30,000 rows):
Sub RemoveHiddenRows()
Dim oRow As Range, rng As Range
Dim myRows As Range
With Sheets("Sheet3")
Set myRows = Intersect(.Range("A:A").EntireRow, .UsedRange)
If myRows Is Nothing Then Exit Sub
End With
For Each oRow In myRows.Columns(1).Cells
If oRow.EntireRow.Hidden Then
If rng Is Nothing Then
Set rng = oRow
Else
Set rng = Union(rng, oRow)
End If
End If
Next
If Not rng Is Nothing Then rng.EntireRow.Delete
End Sub
The code comes from here: Delete Hidden/Invisible Rows after Autofilter Excel VBA
Moreover I read this thread: Speeding Up Code that Removes Hidden Rows on a Sheet
The situation: I have applied 5 different filters to a table consisting of 12 columns, therefore a lot of rows are filtered out (hidden) after the process. When I try to delete those, the code above takes a very long time. In my case I don't know if Excel was still working, so I had to force an exit. That leads to the following question:
Is there any other way than looping through all the hidden rows and deleting them?
An idea which came to my mind was to copy only the remaining unfiltered (that is non-hidden) content to a new sheet and afterwards delete the old sheet, which contains the full information. If so, how can that be done?
I don't think you need to involve another worksheet. Simply copy the rows below the existing Range.CurrentRegion property and then remove the filter and delete the original data.
Sub RemoveHiddenRows()
With Sheets("Sheet10")
With .Cells(1, 1).CurrentRegion
With .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
If CBool(Application.Subtotal(103, .Columns(1))) Then
.Cells.Copy Destination:=.Cells(.Rows.Count + 1, 1)
End If
.AutoFilter
.Cells(1, 1).Resize(.Rows.Count, 1).EntireRow.Delete
End With
End With
End With
End Sub
You may also receive some good, focused help on this subject by posting on Code Review (Excel).
You can improve performance significantly with a function like this:
Option Explicit
Public Sub deleteHiddenRows(ByRef ws As Worksheet)
Dim rngData As Range, rngVisible As Range, rngHidden As Range
With ws
Set rngData = .UsedRange
With rngData
Set rngVisible = .SpecialCells(xlCellTypeVisible)
Set rngHidden = .Columns(1)
End With
End With
If Not (rngVisible Is Nothing) Then
ws.AutoFilterMode = False
' invert hidden / visible
rngHidden.Rows.Hidden = False
rngVisible.Rows.Hidden = True
' delete hidden and show visible
rngData.SpecialCells(xlCellTypeVisible).Delete
rngVisible.Rows.Hidden = False
End If
End Sub
I tested it on a file with 2 filters applied to it
The function was adapted from the code in this suggestion

Is there a better way to write the following VB6 snippet?

I work at $COMPANY and I'm helping maintain $LEGACY_APPLICATION. It's written in visual basic 6.
I was faced with doing an unpleasantly elaborate nested if statement due to the lack of VB6's ability to perform short circuit evaluations in if statements (which would simplify this a lot). I've tried AndAlso, but to no avail. Must be a feature added after VB6.
Some genius on SO somewhere pointed out that you can trick a select case statement into working like a short-circuiting if statement if you have the patience, so I tried that, and here's what I came up with:
Select Case (True) ' pretend this is an if-else statement
Case (item Is Nothing): Exit Sub ' we got a non-element
Case ((item Is Not Nothing) And (lastSelected Is Nothing)): Set lastSelected = item ' we got our first good element
Case (item = lastSelected): Exit Sub ' we already had what we got
Case (Not item = lastSelected): Set lastSelected = item ' we got something new
End Select
It's definitely a little unusual, and I had to make use of my fantastic whiteboard (which, by the way, is pretty much the most useful programming resource besides a computer) to make sure I had mapped all of the statements correctly.
Here's what's going on there: I have an expensive operation which I would like to avoid repeating if possible. lastSelected is a persistent reference to the value most recently passed to this calculation. item is the parameter that was just received from the GUI. If there has never been a call to the program before, lastSelected starts out as Nothing. item can be Nothing too. Additionally, if both lastSelected and item are the same something, skip the calculation.
If I were writing this in C++, I would write:
if (item == NULL || (lastSelected != NULL && item->operator==(*lastSelected))) return;
else lastSelected = item;
However, I'm not.
Question
How can I rewrite this to look better and make more sense? Upvotes will be awarded to answers that say either "YES and here's why: X, Y, Z" or "NO, and here's why not: X, Y, Z".
Edits
Fixed the C++ statement to match the VB6 one (they were supposed to be equivalent)
This is shorter and 100x more readable.
EDIT Wug edited the code in MarkJ's original answer, into this:
If (item Is Nothing)
Then Exit Sub ' we got a non-element
ElseIf (lastSelected Is Nothing) Then
Set lastSelected = item ' we got our first go
ElseIf (item = lastSelected) Then
Exit Sub ' we already had what we got
End If
Set lastSelected = item ' we got something new
Here's MarkJ's edit in response. One nested if, but only one Set. Seems neater to me.
If (item Is Nothing) Then
Exit Sub ' we got a non-element
ElseIf Not (lastSelected Is Nothing) Then ' not our first go
If (item = lastSelected) Then
Exit Sub ' we already had what we got
End If
End If
Set lastSelected = item ' we got something new
' does stuff here? #Wug is that true?
To compare reference equality in VB6 use item Is LastSelected. Because item = lastSelected will probably evaluate the default properties in the objects and compare those instead!
Since brevity appears to be a goal, consider this. If you Exit Sub when condition X is True, you don't need to check X again later. It is False! Unless it changes its value in between evaluations (e.g. X is a function that checks the system clock). You were checking whether item was lastSelected, then whether it wasn't. And if item Is Nothing is False, do not bother to check whether item Is Not Nothing is True!
VB6 does not short circuit for backwards compatibility with ancient versions of Basic
Stop worrying that VB6 is not some other language and relax!
YES
I translated it from your case statement. I find it easier to read, personally.
If Item Is Nothing Then
Exit Sub ' we got a non-element
ElseIf LastSelected Is Nothing Then
Set LastSelected = Item ' we got our first good element
ElseIf Item = LastSelectedItem Then
Exit Sub ' we already had what we got
Else
Set LastSelected = Item ' we got something new
End If
You asked for explanation. I tried not to have to give much (by re-using your own code comments).
But here it is anyway :-)
Firstly if there is no item, just exit. Easy.
Otherwise if LastSelected Is Nothing then we know, because the first if condition failed, that Item exists, and it's safe to mark that value as having been Last Selected. As you say, we got our first good element. The sub continues on.
However if we have existing values for Item and LastSelected, then either they are equal or not. If they are equal, just quit.
If they aren't equal, then update LastSelected. As you say, we got something new.
You can use a helper function like this:
Private Function pvGetItemData(oItem As ListItem) As Variant
If Not oItem Is Nothing Then
pvGetItemData = oItem.Tag
Else
pvGetItemData = -1
End If
End Function
and then
If pvGetItemData(Item) = pvGetItemData(LastSelected) Then
' cache hit
Else
' do calc
Set LastSelected = Item
End If
YES
I'd make that simpler:
If item Is Nothing Then
Exit Sub ' we got a non-element
Else
Set lastSelected = item ' we got something to assign
End If
Unless there are side effects assigning lastItem (it can be property with invalid assignment code), then code logic is essentially same.
If you are not required to exit the sub (snippet is at the end of sub or something), then next is even simpler:
If Not (item Is Nothing) Then Set lastSelected = item
BTW, your Select Case (True) looks really odd to VB programmer :)

Visual Basic Function Procedure

I need help with the following H.W. problem. I have done everything except the instructions I numbered. Please help!
A furniture manufacturer makes two types of furniture—chairs and sofas.
The cost per chair is $350, the cost per sofa is $925, and the sales tax rate is 5%.
Write a Visual Basic program to create an invoice form for an order.
After the data on the left side of the form are entered, the user can display an invoice in a list box by pressing the Process Order button.
The user can click on the Clear Order Form button to clear all text boxes and the list box, and can click on the Quit button to exit the program.
The invoice number consists of the capitalized first two letters of the customer’s last name, followed by the last four digits of the zip code.
The customer name is input with the last name first, followed by a comma, a space, and the first name. However, the name is displayed in the invoice in the proper order.
The generation of the invoice number and the reordering of the first and last names should be carried out by Function procedures.
Seeing as this is homework and you haven't provided any code to show what effort you have made on your own, I'm not going to provide any specific answers, but hopefully I will try to point you in the right direction.
Your first 2 numbered items look to be variations on the same theme... string manipulation. Assuming you have the customer's address information from the order form, you just need to write 2 separate function to take the parts of the name and address, take the data you need and return the value (which covers your 3rd item).
To get parts of the name and address to generate the invoice number, you need to think about using the Left() and Right() functions.
Something like:
Dim first as String, last as String, word as String
word = "Foo"
first = Left(word, 1)
last = Right(word, 1)
Debug.Print(first) 'prints "F"
Debug.Print(last) 'prints "o"
Once you get the parts you need, then you just need to worry about joining the parts together in the order you want. The concatenation operator for strings is &. So using the above example, it would go something like:
Dim concat as String
concat = first & last
Debug.Print(concat) 'prints "Fo"
Your final item, using a Function procedure to generate the desired values, is very easily google-able (is that even a word). The syntax is very simple, so here's a quick example of a common function that is not built into VB6:
Private Function IsOdd(value as Integer) As Boolean
If (value Mod 2) = 0 Then 'determines of value is an odd or even by checking
' if the value divided by 2 has a remainder or not
' (aka Mod operator)
IsOdd = False ' if remainder is 0, set IsOdd to False
Else
IsOdd = True ' otherwise set IsOdd to True
End If
End Function
Hopefully this gets you going in the right direction.

QTP narrow a list of ChildObjects

[The description is a bit fudged to obfuscate my real work for confidentiality reasons]
I'm working on a QTP test for a web page where there are multiple HTML tables of items. Items that are available have a clickable item#, while those that aren't active have an item# as plain text.
So if I have a set of ChildObjects like this:
//This is the set of table rows that contain item numbers, active or not.
objItemRows = Browser("browserX").Page("pageY").ChildObjects("class:=ItemRow")
What is the simplest way in QTP land to select only the clickable link-ized item #s?
UPDATE: The point here isn't to select the rows themselves, it's to select only the rows that have items in them (as opposed to header/footer rows in each table). If I understand this correctly, I could then use objItemRows.Count to count how many items (available and unavailable) there are. Could I then use something like
desItemLink = Description.Create
desItemLink("micclass").value = "Link"
objItemLinks = objItemRows.ChildObjects(desItemLink)
To get the links within only the item rows?
Hope that clarifies things, and thanks for the help.
I think I have this figured out.
Set desItemLink = description.create
desItemLink("micclass").value = "Link"
desItemLink("text").RegularExpression = True
//True, Regex isn't really required in this example, but I just wanted to show it could be used this way
//This next part depends on the format of the item numbers, in my case, it's [0-9]0000[0-9]00[0-9]
For x = 0 to 9
For y = 0 to 9
For z = 0 to 9
strItemLink = x & "0000" & y & "00" & z
desItemLink("text").value = strItemLink
Set objItemLink = Browser("browser").Page("page").Link(desItemLink)
If objItemLink.Exist(0) Then
//Do stuff
End If
Next
Next
Next
Thanks for your help anyways, but the code above will iterate through links with names in a given incrementing format.

Resources