Set the Progress Bar Value according to data entered? - vb.net

How can Set the Progress Bar Value increasing/ decreasing according to data entered in each controls on a form ? I mean, I am working with number of controls, so go for a sample [Suppose I have four textboxes, Progress Bar Maximum 100, when entering txt_1 Progress value have to increase to 100/4= 25, if I remove data from txt_1 the Value should decreased to Zero.
Sorry for my language, I am not good in English.
Can any one help me, please..... Thank You.

The Firs thing you do is set the progressbar maximum in your formload event to the number of textboxes you are using.
Dim TextBoxNumber As Integer = 4
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ProgressBar1.Maximum = TextBoxNumber
End Sub
Then you need to make 2 events.
1. TextBox enter event."Private Sub TextBox_Enter(sender As Object, e As EventArgs)"
2. TextBox TextChanged event"Private Sub TextBox_TextChanged(sender As Object, e As EventArgs)"
Then Link all the textboxes you want to use to the events.
In these events you will use the Sender.
Make a Dim and convert the sender to type Textbox to use its properties." Dim TextControl As TextBox = CType(sender, TextBox)"
In the Enter event you count the text that is entered in the textbox.
If the count is 0 you set the boolean to true and if its more than 0 you set the Boolean to False.
This Boolean you will use in the Text Change event.
Dim CheckTextBox As Boolean
Private Sub TextBox_Enter(sender As Object, e As EventArgs) Handles TextBox4.Enter, TextBox3.Enter, TextBox2.Enter, TextBox1.Enter
Dim TextControl As TextBox = CType(sender, TextBox)
If TextControl.Text.Count > 0 Then
CheckTextBox = False
Else
CheckTextBox = True
End If
End Sub
In the Text Change event you need to do a few things.
1.Use a if statement to check if the textbox is empty."This will only trigger if the user deletes the text in the textbox."
So if empty remove 1 from the progressbar and set CheckTextBox"Boolean" to True
"ProgressValue = ProgressValue - 1"
"CheckTextBox = True"
2.Then use a ElseIf to check if the CheckTextBox"Boolean" is True.
If true add 1 to progressbar and set Boolean to false.
"ProgressValue = ProgressValue + 1"
"CheckTextBox = False"
You need to set the Boolean to false because otherwise you will add 25 for every time you add somthing to the textbox.
Dim ProgressValue As Integer
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged, TextBox3.TextChanged, TextBox2.TextChanged, TextBox1.TextChanged
Dim TextControl As TextBox = CType(sender, TextBox)
If TextControl.Text = "" Then
ProgressValue = ProgressValue - 1
CheckTextBox = True
ElseIf CheckTextBox = True Then
ProgressValue = ProgressValue + 1
CheckTextBox = False
End If
ProgressBar1.Value = ProgressValue
End Sub

Related

How to acces checked status of dynamicaly created Radio Buttons in VB

I create a variable amount of radio buttons in a Tab Page by reading the lines out of an array using:
Public Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rbgen As RadioButton
Dim tab1 = 0
For y As Integer = 0 To Array.GetUpperBound(0) Step 1
If Array(y, 0) = "ABC" Then
rbgen = New RadioButton
rbgen.Name = "RButton" & Convert.ToString(y)
rbgen.Left = 10
rbgen.Top = ((tab1) * 30)
rbgen.Text = Array(y, 2)
rbgen.Size = New System.Drawing.Size(260, 40)
TabPage1.Controls.Add(rbgen)
tab1 = tab1 + 1
End If
Next
End Sub
When i click a "start" Button i need to run different code depending on the checked RadioButton. But how do i access the information of which radio Button is checked?
Thank you very much for your help!
You could try to add a Handler after you add the radio button:
AddHandler rbgen.CheckedChanged, AddressOf RadioBox_CheckedChanged
and then you need to add this sub
Private Sub RadioBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim RadioBox As RadioButton = TryCast(sender, RadioButton)
If RadioBox IsNot Nothing Then
MessageBox.Show(RadioBox.CheckState)
End If
End Sub
and if you want to check the status
For Each RadioBox In TabPage1.Controls.OfType(Of RadioButton)()
if Ctype(TabPage1.controls("RadioButton" & i), radiobutton).checked = true then
'your Code Here
end if
next
I could not test it yet but I hope I already helped you a little bit :)

Labels don't redraw on Form.Refresh()

I have a form on which in the Paint event some labels with some information are drawn inside a panel - this works fine. However I would like to have the text on the labels being changed depending on the value of a Trackbar that is placed on the same form. This is the Trackbar-Scroll Event which should refresh the whole form:
Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
Me.Refresh()
End Sub
And this is the code that draws the labels on the form:
Public Sub Form_Paint(sender As Object, e As PaintEventArgs)
For i = 0 To 10
Dim tb As New Label
tb.Name = "tb" & CStr(i)
If Me.TrackBar1.Value = 1 Then tb.Text = "sometext"
If Me.TrackBar1.Value = 0 Then tb.Text = "anothertext"
tb.Location = New Point(i, i * 2)
Me.Panel1.Controls.Add(tb)
Next
End Sub
However no matter in what state the trackbar is the text displayed in the labels is alaways "anothertext". The Paint event is triggered as far as I can tell when I change the value of the trackbar but how can I also force the labels to update?
Just add the labels once. Separate the creating and changing logic into two methods
Private prefix As String = "tb"
Private factor As Integer = 10
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
addLabels()
changeLabels()
End Sub
Private Sub addLabels()
For i = 0 To 10
Dim tb As New Label()
tb.Name = prefix & CStr(i)
tb.Location = New Point(factor * i, factor * i * 2)
Me.Panel1.Controls.Add(tb)
Next
End Sub
Private Sub changeLabels()
For i = 0 To 10
Dim tb As Label = CType(Panel1.Controls(prefix & CStr(i)), Label)
If Me.TrackBar1.Value = 1 Then tb.Text = "sometext"
If Me.TrackBar1.Value = 0 Then tb.Text = "anothertext"
Next
End Sub
Now, in TrackBar1_Scroll, you can just change them (instead of recreating them)
Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
changeLabels()
End Sub
Since the label value depends on the TrackBar value, there is no reason to update them in Paint, which happens more frequently than the TrackBar is updated.
Adding new Labels and removing old Labels in Paint seems like a lot of extra processing.
I believe you need to set the LargeChange property of the TrackBar as a Scroll Event is considered a large change, but LargeChange defaults to 0, thus when you scroll the Value is only increasing/decreasing by 0, Leaving it at 0

Enable or visible textbox by radiobutton

I want to enable or visible a textbox when the user check the radiobutton like a yes\no questions if the answer is no the textbox will be enable or visible.
Before I forget all these in gridview
Thank you
In your Grid's RowDataBound event find your controls first. Then set the visibility of the textbox with the condition you need.
Protected Sub gvTimeSlots_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvTimeSlots.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim RadioButton1 As System.Web.UI.WebControls.RadioButton = e.Row.FindControl("RadioButton1")
Dim TextBox1 As System.Web.UI.WebControls.TextBox = e.Row.FindControl("TextBox1")
If RadioButton1.Value="Yes" Then
TextBox1.Enabled = True
// If you want to set the visibility use
TextBox1.Visible = True
End If
End Sub
I used many solutions I found them in the internet & this one of them
Protected Sub reject_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim rbd As RadioButton = CType(sender, RadioButton)
Dim grdrow As GridViewRow = CType(rbd.NamingContainer, GridViewRow)
Dim reject As RadioButton = CType(grdrow.Cells(9).Controls(0).FindControl("reject"), RadioButton)
Dim t1 As TextBox = CType(grdrow.Cells(9).Controls(0).FindControl("t1"), TextBox)
If reject.Checked = True Then
t1.Visible = True
End Sub
when I check the radiobutton it will unchecked when AutoPostedBack True (for the 2 solutions)
my gridview name gvorders

Change editted cell in datagridview on enter (vb.net)

I have a datagridview table that is populated by using a datatable as a datasource. The datagridview has been set to edittable and I can change the values of cells but of course, the changes do not reflect in the original database table since the grid view is not directly bound. Is it possible to have the datagridview in such a way that when I press enter (when editting a cell), the focus shifts to the cell on the right (rather then selecting the row below)? This would need to keep on going until I reach the right most column, in which case the following edit cell would be the first cell in the following row.
Thanks in advance!
Try this:
define a flag flag_edited that will be raised when an edit occurs (CellEndEdit event)
define a function changeSelectionToNextCell that will undo default behavior's selection change (SelectionChanged event)
Here is a sample:
Private flag_edited As Boolean
Private Sub DataGridView1_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
flag_edited = True
End Sub
Private Sub DataGridView1_SelectionChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.SelectionChanged
changeSelectionToNextCell()
End Sub
Private Sub changeSelectionToNextCell()
If flag_edited Then
flag_edited = False
If DataGridView1.CurrentCell IsNot Nothing Then
Dim row_index As Integer = DataGridView1.CurrentCell.RowIndex - 1
Dim col_index As Integer = DataGridView1.CurrentCell.ColumnIndex + 1
If col_index = DataGridView1.ColumnCount Then
col_index = 0
row_index += 1
End If
DataGridView1.CurrentCell = DataGridView1.Rows(row_index).Cells(col_index)
End If
End If
End Sub

VB.NET ListView Questions

I have Questions?
If I enter a data in a textbox,
I want my listview to select the same data entered in the textbox,
example,
I have a StudentNumber column in my listview and it has data on it(ex. 123456)
I will enter 123456 in the textbox.
The ListView must select 123456 ?
Please Help
THANK YOU,
I think this will do what you want. It will search the first column of the ListView for the text in the TextBox.
Setup the listview:
With ListView1
.MultiSelect = False 'Ensure only one item selected at a time
.HideSelection = False 'Shows the selection when the textbox changes
'Add some items for testing
.Items.Add("1234")
.Items.Add("1122")
.Items.Add("1133")
End With
Then in the textbox TextChanged changed event:
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
ListView1.SelectedItems.Clear()
Dim foundItem As ListViewItem = ListView1.FindItemWithText(TextBox1.Text, False, 0, False)
If (foundItem IsNot Nothing) Then foundItem.Selected = True
End Sub
Alternatively if you want to specify which column of your ListView to search for the text then this function should do the trick:
Private Sub SelectListViewItem(ByRef listviewSource As ListView, ByVal textToFind As String, ByVal column As Integer)
Dim foundItem As ListViewItem = Nothing
Dim startIndex As Integer = 0
listviewSource.SelectedItems.Clear()
Do Until Not foundItem Is Nothing AndAlso foundItem.SubItems(column).Text = TextBox2.Text
If foundItem Is Nothing Then startIndex = 0 Else startIndex = foundItem.Index + 1
If startIndex > listviewSource.Items.Count - 1 Then Exit Sub 'We have reached end of the listview
foundItem = listviewSource.FindItemWithText(textToFind, True, startIndex)
If foundItem Is Nothing Then Exit Sub
Loop
If (foundItem IsNot Nothing) Then foundItem.Selected = True
End Sub
Usage:
Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
SelectListViewItem(ListView1, TextBox2.Text, 1)
End Sub
Warning - In both cases, this may cause your application to perform poorly if you have a lot of items in your listview, in that case you could consider moving the code into a background worker