Variable depending on Tab focus - vb.net

i have question that is bugging me for quite some time but im unable to get this around my mind.
So i have a program that contains 8 tabs, all using same formula for calculating when i click's the button on this page but base value is depending on whitch tab is selected and which button is clicked
variables a1 and a2 are the same values accessing exacly the same database just like b1&b2 and c1&c2 working as comboboxes
Private Sub Calculate() Handles button1.Click
value1 = 0.5
sum1 = a1.selectedValue*b1.SelectedValue*c1.SelectedValue*value1
textbox1.value = sum1.value
Private Sub Calculate2() Handles button2.Click
value2 = 0.55
sum2 = a1.selectedValue*b2.selectedValue*c2.selectedValue*value2
texbox2.value = sum2.value
is there any way to make value by depending on the tab that's focused and simplify it? like
Private Sub Calculate(sender As Object, e As EventArgs) Handles button1.Click, button2.Click, button3.Click
if tabpage1 is focus then
value = 0.05
elseif tabpage2 is focus then
value = 0.55
end if
if a1 is selected then
a=a1.value
elseif a2.is selected then
a=a2.value
......
endif
.......
sum = a*b*c*value
if tabpage1 is focus then
textbox1.value = sum.value
else
....
endif
??
i know code above is far from "simple" and actually the handling buttons in each sub is maybe hmmm... better? Just looking at other perspectives to do this 8 tabs in just one sub

I'd suggest that the best solution is something like this:
Dim selectedTab = TabControl1.SelectedTab
Dim tb = selectedTab.Controls.OfType(Of TextBox)().First()
Dim value = CInt(selectedTab.Tag)
'Use tb and value here.
You just need to set the Tag property of each TabPage to the appropriate value and you may need to adjust that second line if there are other TextBoxes on each page or it's inside another container. There's obviously multiple ComboBoxes on each page so that part might look like this:
Dim cbs = selectedTab.Controls.OfType(Of ComboBox)().ToArray()
Dim cbb = cbs(0)
Dim cbc = cbs(1)

you can use method Focused or ActiveControl or SelectedTab/Index as :
Private Sub Calculate(sender As Object, e As EventArgs) Handles button1.Click, button2.Click, button3.Click
If sender Is button1 Then '' Button1 is clicked
If TabControl1.SelectedTab Is tabpage1 Then
'' If tabpage1.Focused OrElse Me.ActiveControl Is tabpage1 Then '' Tabpage1 if focused Or Tabpage1 is Active Control
'' Your Code
End If
End If
If sender Is button2 Then '' Button2 is clicked
If TabControl1.SelectedTab Is tabpage2 Then
'' If tabpage2.Focused OrElse Me.ActiveControl Is tabpage2 Then '' Tabpage2 if focused or Tabpage2 is Active Control
'' Your Code
End If
End If
End Sub

Related

VB .net Change text of selected textbox on button click

I have 10 buttons and I want to click on a button so it changes text of the focused textbox and switch to next textbox.
i tried this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Focus()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Focus Then
TextBox1.Text = "1"
TextBox1.Enabled = False
TextBox2.Focus()
TextBox2.Enabled = True
End If
If TextBox2.Focus Then
TextBox2.Text = "1"
TextBox2.Enabled = False
TextBox3.Focus()
TextBox3.Enabled = True
End If
If TextBox3.Focus Then
TextBox3.Text = "1"
TextBox3.Enebled= False
TextBox4.Focus()
TextBox4.Enabled = True
End If
End Sub
But it writes the value in every textbox instead of just going to the next textboxt
This code does not do what you think it does:
If TextBox1.Focus Then
Focus is not a boolean property. In VB.Net, you can call methods without parentheses, and that's what you're doing here. The conditional block actually tries to set the focus. And since this is always gonna succeed unless you explicitly handle the event and block it, all of those If conditions result in True.
To find which control has focus, do this:
Public Shared Function FindFocusedControl(control As Control) As Control
Dim container = TryCast(control,IContainerControl)
While container IsNot Nothing
control = container.ActiveControl
container = TryCast(control, IContainerControl)
End While
Return control
End Function
In your Click event handler, you are calling the Focus method of each TextBox in turn and then populating them if it succeeds. It will succeed every time so you populate every TextBox.
I suspect that what you meant to do was test the Focused property rather than call the Focus method. That would make more sense because then it would only populate the TextBox that had focus. That is still flawed though, because the Button that you just clicked will have focus, so you won't actually populate any TextBox.
You have two main choices here. Firstly, you could use a custom Button control that will not take focus when it is clicked. That way, the TextBox that had focus when you clicked will still have focus. Alternatively, you could remember which control last had focus by assigning it to a field and using that. That's probably the way that I'd go.
Here's a quick (i.e. not rigorous) example of the second option:
Private lastActiveControl As Control
Private Sub TextBoxes_Leave(sender As Object, e As EventArgs) Handles TextBox4.Leave,
TextBox3.Leave,
TextBox2.Leave,
TextBox1.Leave
lastActiveControl = DirectCast(sender, Control)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim textBoxes = New Control() {TextBox1, TextBox2, TextBox3, TextBox4}
Dim lastTextBoxIndex = Array.IndexOf(textBoxes, lastActiveControl)
If lastTextBoxIndex <> -1 Then
Dim nextTextBoxIndex = (lastTextBoxIndex + 1) Mod textBoxes.Length
Dim nextTextBox = textBoxes(nextTextBoxIndex)
lastActiveControl.Text = "1"
lastActiveControl.Enabled = False
nextTextBox.Enabled = True
nextTextBox.Select()
End If
End Sub

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

Replacing listview items with another list using combo box

Pardon for being novice at vb.net. I have a combo box and a list view. What I needed is when I change the category in the combo box and pressed 'OK', the old list added before will be replaced by a new list.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As ListViewItem
If ComboBox1.Text = "Terrestrial Ecotoxicity (Freshwater)" Then
i = ListView1.Items.Add("Water")
i.SubItems.Add("2068.030567")
i.SubItems.Add("0")
i.SubItems.Add("0")
ElseIf ComboBox1.Text = "Terrestrial Ecotoxicity (Seawater)" Then
i = ListView1.Items.Add("Dimethylamine")
i.SubItems.Add("1229.539887")
i.SubItems.Add("0.000122731")
i.SubItems.Add("0.15090266")
End if
End Sub
What should I need to add?
What you are doing there is adding a different row on each case. What you ask is to fill the listview with different items depending on the item you pick at the combobox.
Basically, you need to clear the listview on each case. I'd do something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim i As ListViewItem
ListView1.Items.Clear
If ComboBox1.Text = "Terrestrial Ecotoxicity (Freshwater)" Then
i = ListView1.Items.Add("Water")
i.SubItems.Add("2068.030567")
i.SubItems.Add("0")
i.SubItems.Add("0")
ElseIf ComboBox1.Text = "Terrestrial Ecotoxicity (Seawater)" Then
i = ListView1.Items.Add("Dimethylamine")
i.SubItems.Add("1229.539887")
i.SubItems.Add("0.000122731")
i.SubItems.Add("0.15090266")
End if
End Sub

drag and drop to tablelayoutpanel from listview

I am trying to build a control which implements a tablelayoutpanel for the design and placement of other controls within the control - I need to add functionality which will allow the tablelayoutpanel to accept content from a listview (It does not even need to process it in any fashion at this point) - I, however, can not get the tablelayout panel to even display that it will accept data - only displays the circle/slash symbol. These are kept in 2 separate child mdi forms within the same parent.
currently I have in my listview form
Private Sub Jboard_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AllowDrop = True
ListView2.AllowDrop = True
end sub
Private Sub ListView2_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver, ListView2.DragOver
If e.Data.GetDataPresent(GetType(ListViewItem)) Then
e.Effect = DragDropEffects.All
End If
End Sub
Private Sub ListView2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView2.MouseDown
Dim item As ListViewItem = ListView2.HitTest(e.Location).Item
If item IsNot Nothing Then
ListView2.DoDragDrop(item, DragDropEffects.All)
End If
End Sub
on my new tablelayoutpanel control form I have
Me.AllowDrop = True
DboardScheduler1.AllowDrop = True
'dboardscheduler1 is my new control
in the code for the control I have
tablelayoutpanel1.AllowDrop = true
What am I missing?
It looks like you only coded the one side, you also need to tell the TLP(like) control how/what to do. Something like this (not sure of the constraints you want, like JUST LVs and only MOVE).
' NOT mousedown
Private Sub ItemDrag(sender As Object, e As ItemDragEventArgs) Handles ...
If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
' ToDo: Decide what to do with multiples. Singles only assumed
' add the item under the cusor as the first, effect as Move
DoDragDrop(e.Item, DragDropEffects.Move)
End Sub
LV Drag OVer:
' probably:
e.Effect = DragDropEffects.None
' because you cant really drop it here, but the No Action shows that it knows
' a D-D is happening.
TLP Drag OVer:
If (e.Data.GetDataPresent(GetType(ListViewItem)) = False) Then
e.Effect = DragDropEffects.None
Exit Sub
Else
e.Effect = DragDropEffects.Move ' or link maybe
End If
TLP DragDrop:
Dim dragLVI As ListViewItem
' get text and do whatever with it
If (e.Data.GetDataPresent(GetType(ListViewItem)) = False) Then
e.Effect = DragDropEffects.None
Exit Sub
Else
dragLVI = CType(e.Data.GetData(GetType(ListViewItem)), _
ListViewItem)
newTextThing = dragLVI.SubItems(0).Text
End If
Something along those lines. The point is that you have to write code for the piece being dropped on.

ListView ToolTip only in First Cell - VB.NET

I'm adding a ToolTip to a ListViewItem. However, the ToolTip only shows up when the user hovers over the first cell in the row to which the ToolTip has been applied.
MyListViewItem.ToolTipText = "Important Message"
The only other code I have related to ToolTips is this:
MyListView.ShowItemToolTips = True
Any idea how I can make the ToolTip show up on a specific cell in a row, or even the entire row? Thanks.
If you want a non-wrapper answer unlike the dup mentioned, try this:
The listview FullrowSelect property must be true. Next you need to store the tips for each subitem, I do this inside the subitem tag property. What you want to do, is on the listview mousemove event, you grab the item under the mouse, get it's subitem, and use that tip.
This simple example shows you how to get that subitem tooltip, you can just hack this a bit to suit your needs.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lvw.ShowItemToolTips = True
lvw.Columns.Add("Column A")
lvw.Columns.Add("Column B")
lvw.Columns.Add("Column C")
lvw.Items.Add(New ListViewItem(New String() {"Colors", "Green", "Blue"}))
lvw.Items(0).SubItems(0).Tag = "See the other columns"
lvw.Items(0).SubItems(1).Tag = "Like grass"
lvw.Items(0).SubItems(2).Tag = "Like the sky"
End Sub
Function GetItemTip(ByVal list As ListView, ByVal e As System.Windows.Forms.MouseEventArgs) As String
Dim item As ListViewItem = list.GetItemAt(e.X, e.Y)
If Not IsNothing(item) Then
Dim si As ListViewItem.ListViewSubItem
si = item.GetSubItemAt(e.X, e.Y)
If Not IsNothing(si) Then
Return si.Tag.ToString
Else
Return ""
End If
Else
Return ""
End If
End Function
Private Sub lvw_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvw.MouseMove
Me.Text = GetItemTip(CType(sender, ListView), e)
End Sub