Need to use Session State - vb.net

VB student that needs some help with a simple two page application that:
1.) Create a sessions counter to show how many registrations are saved each time the "Submit" button is clicked
CODE FOR SESSION COUNT
If Not IsPostBack Then
If Session("key") Is Nothing Then
Session("key") = 0
Else
Session("key") = Session("key") + 1
End If
Response.Write(Session("key"))
LoadData()
End If
2.) Create a "Display" button that will show the above count on a new page when the user clicks it.
CODE TO DISPLAY SECOND PAGE WITH SESSION COUNT
Response.Redirect("PageTwo.aspx")
But nothing is displaying on the second page when I click the "Display" button. I have tried different ways to adjust my code but it doesn't work. Instead of me listing all of my changes and attempts I made I just listed my core code and need someone to please show me how I can get this to work.

I changed my IF Statement for the "Submit" button to something easier to work with and read.
Protected Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click
'if session is not set then set the session and assign value to 1
If (Session.Item("numberofRegistration") Is Nothing) Then
'setting session variable
Session.Item("numberofRegistration") = 1
Else
'taking value from session and incrementing value by 1
Session.Item("numberofRegistration") = Convert.ToInt32(Session.Item("numberofRegistration").ToString()) + 1
End If
End Sub
Then for my Display Button, a simple redirect to "PageTwo"
Protected Sub DisplayButton_Click(sender As Object, e As EventArgs) Handles DisplayButton.Click
Response.Redirect("PageTwo.aspx")
End Sub
On Page Two I used a Message Label and another IF statement
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'checking if session is set or not
If (Session.Item("numberofRegistration") Is Nothing) Then
'if session variable is null then
'display error message on the lable
MessageLabel.Text = "Please set the session variable"
Else
'display value from session variable on the lable with id=lblMessage
MessageLabel.Text = String.Format("Total Number of Registrations : {0}", Session.Item("numberofRegistration").ToString())
End If
End Sub

Related

Avoid some functions/events to run when one function/events is clicked or executed

im developing a system using vb.net and i have some question about datagridview function. Is it possible in gridview that when i click the rowheader function it will not execute the cell enter function of it?
Because my problem is in my cell enter event/function there is a code that will show some text box if the user enter on the 1st cell. and i want that when i click the row header of my grid view this cell enter event/function will not be executed. is it possible guys? give me some tips or tricks on how to do it.
i've also done trying like this
Private sub gridview_RowHeaderMouseClick . . .
textbox.visible = false
gridview.endEdit(true)
end sub
the result is okay but it is not good for the client view and i want to improve it more.
This is my code.
Private Sub dgCharges_CellEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgCharges.CellEnter
if e.ColumnIdex >= 0 Then
if e.ColumnIndex = 5 Then
'Show Textbox.
End If
End if
End Sub
Private Sub dgCharges_RowHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgCharges.RowHeaderMouseClick
'Textbox.visible = false //this line i've use to hide the textbox when user
clicked row header.
dgCharges.EndEdit(True)
End Sub
Just put a test in the CellEnter function that only executes the restricted part of the code if the column index >= 0. The header has index -1
Private Sub DataGridView1_CellEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
If e.ColumnIndex >= 0 Then
'Do my stuff
End If
End Sub
------ EDIT ----
To prevent the datagridview cell editors from popping up just set the column to readonly. Alternatively for more control use
Private Sub DataGridView1_CellBeginEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridViewMsg.CellBeginEdit
'Test your criteria
If shouldBeReadonly Then
e.Cancel = True
End If
End Sub

vb.net add values from listbox to textbox via drag&drop AND via double click

I'm struggling with some functionality I want to use on my Windows form.
( Just for info, this is for an AutoDesk Inventor AddIn. )
This is my form layout.
The current workflow
The top 4 list-boxes are filled with available parameter names. The user chooses the parameter(s) he/she wants to use and drags and drops it into one of the driving parameter text-boxes ( marked with the <1> label ).
The code that relates to the drag and drop operations
Private Sub lstTemp_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles lbModelParameters.MouseDown,
lbUserParameters.MouseDown,
lbReferenceParameters.MouseDown,
lbLinkedParameters.MouseDown
' In order to access a specific item in a listbox.itemcollection, you must think of it
' as an array of data or a collection and access it in the same manner by always
' letting VB know which item you intend to use by identifying it with its index location
' within the collection. And this is better than taking up basket weaving :-)
lbModelParameters.DoDragDrop(sender.Items(sender.SelectedIndex()).ToString, DragDropEffects.Move)
End Sub
Private Sub txtTemp_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles tbParameter1.DragEnter,
tbParameter2.DragEnter,
tbParameter3.DragEnter,
tbParameter4.DragEnter,
tbParameter5.DragEnter
'Check the format of the incoming data and accept it if the destination control is able to handle
' the data format
'Data verification
If e.Data().GetDataPresent(DataFormats.Text) Then
e.Effect() = DragDropEffects.Move
Else
e.Effect() = DragDropEffects.None
End If
End Sub
Private Sub txtTemp_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
Handles tbParameter1.DragDrop,
tbParameter2.DragDrop,
tbParameter3.DragDrop,
tbParameter4.DragDrop,
tbParameter5.DragDrop
'This procedure receives the dragged data once it passes the data verification handled by the DragEnter method
'Drops the data onto the destination control
sender.Text() = e.Data().GetData(DataFormats.Text).ToString()
End Sub
New functionality
Now I would like to decrease the user mouse movement for ergonomic reasons and speed. But I would also like to keep to the drag and drop functionality. As it can overwrite a value that has already been added by the user.
I would like to be able to DoubleClick a item in the listbox, and that item should be added to the first empty textbox. I named my textboxes with a number so it's easy to loop over them all to check if it's empty.
I tried doing it with this code, but my double click event never gets fired. It always goes to the drag and drop. How do you do handle this, that the double click gets fired instead of drag drop?
Private Sub ParameterAddDoubleClick(sender As Object, e As EventArgs) _
Handles lbModelParameters.DoubleClick,
lbUserParameters.DoubleClick,
lbReferenceParameters.DoubleClick,
lbLinkedParameters.DoubleClick
Dim oControl As Windows.Forms.ListBox
oControl = DirectCast(sender, Windows.Forms.ListBox)
' Add line in likedparameters listbox
If oControl.SelectedIndex <> -1 Then
' Loop trough all the controls to see if one is empty
' if it's empty add parameter, else go to next
' if all textboxes are used do nothing.
For i = 1 To 6
Dim oTextbox As Windows.Forms.TextBox =
CType(gbDrivingParameters.Controls("tbParameter" & i),
Windows.Forms.TextBox)
If oTextbox.TextLength = 0 Then
' Add the sender item into the linked listbox
oTextbox.Text = oControl.Items.Item(oControl.SelectedIndex)
End If
Next
End If
End Sub
I hope my question is clear, and well prepared. If there is additional information needed, please let me know in a comment.
Mousedown triggers the DoDragDrop, wich stops the doubleclick-event from firing.
To identify if a user doubleclicks or wants to perform a dragdrop, consider the following:
Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown
' Determine whether we are performing a drag operation OR a double click
If e.Clicks = 1 Then
TextBox1.Text = "mousedown"
Else
TextBox1.Text = "dblclick"
End If
End Sub

Listview Checkbox - vb.net

Ok, i have a listview with checkboxes and a button, how it works is that i have to check the items i want to change the values, then press the button to change the value of those checked item, here's my code on the button.
Try
Dim I As Integer
If lv_id.CheckedItems.Count = 0 Then
For I = 0 To lv_id.Items.Count - 1
lv_id.Items(I).SubItems(1).Text = "Pending"
Next
Else
For I = 0 To lv_id.CheckedItems.Count - 1
lv_id.CheckedItems(I).SubItems(1).Text = "Submitted"
Next
End If
Proc_Items.BackColor = Color.Green
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Now, what i want to do is, to remove the button and then when i'll check the item i want to the code above will do the process without pressing the button, i tried "ItemCheck, ItemChecked" event, but with no luck.
You should be able to use the ItemChecked event for this. With the ItemCheckedEventArgs giving you everything you need.
This example would set up to toggle the 3rd column depending on the checkbox state
Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
If e.Item.Checked Then
e.Item.SubItems(2).Text = "Submitted"
Else
e.Item.SubItems(2).Text = "Pending"
End If
End Sub
If I understood you right, you want to run your code whenever a ckecbox in your ListView gets checked or unchecked. This can be done using
Public Class Form1
'The ListView_SelectedIndexChanged event triggers when a checkbox of the listview gets checked or unchecked
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
'Put your code here, access all checked items with "ListBox1.CheckedItems"
End Sub
End Class
This worked for me, and I think it works for you as well. If not, please tell me in the comments what went wrong.

changing calculate button to restart button after clicked

Is there any way to change a button to another button while running the program, i.e when the user clicks a button called "display", it calculates the results, then "display" turns into "Restart?" and if the user clicks that it restarts the program? I would like to change btnDisplay to btnRestart:
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
' displays a student's grade
Double.TryParse(txtEarned.Text, dblEarned)
For Each minimum As Double In dblMinimumPoints
If dblEarned >= minimum Then
lblGrade.Text = strGrade(gradeIndex)
gradeIndex += 1
End If
Next
txtEarned.ReadOnly = False
btnDisplay.Enabled = False
End Sub
I suppose there are multiple ways to do this. One is to have two buttons, btnDisplay and btnRestart, each with desired Text, that are laid on top of each other in the form designer. Alternate which one is visible when they are clicked.
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
' displays a student's grade
Double.TryParse(txtEarned.Text, dblEarned)
For Each minimum As Double In dblMinimumPoints
If dblEarned >= minimum Then
lblGrade.Text = strGrade(gradeIndex)
gradeIndex += 1
End If
Next
txtEarned.ReadOnly = False
btnDisplay.Visible = False
btnReset.Visible = True
End Sub
I'm not sure what you mean by saying that btnRestart will "restart the program", but presumably in its click event you would likewise hide it and make btnDisplay visible again.
(Also you might want to add some Try...Finally error handling in these events, so that even if something goes wrong, you can be sure the Visible lines will be executed.)
In your handler onclick and add a select case based on the .text attribute, then if the text is display update it it to restart, if it is restart then add a goto to where you want the script to start from on restart.
EDIT, below I have added the codetorun as a function which is probably a better solution than goto:
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Select Case btnDisplay.Text
Case "Submit"
codeiwanttorun()
btnDisplay.Text = "Restart"
Case "Restart"
codeiwanttorun()
End Select
End Sub
Private Function codeiwanttorun()
Double.TryParse(txtEarned.Text, dblEarned)
For Each minimum As Double In dblMinimumPoints
If dblEarned >= minimum Then
lblGrade.Text = strGrade(gradeIndex)
gradeIndex += 1
End If
Next
End Function

Hide a row in DataGridView

I am a new user to vb.net and need to hide a row when a user right clicks on a contextmenu and selects hide. I have googled this but have yet to find a way to do it.
At the moment, when a user clicks on an entry in the grid, the value is entered into a text box which is fine. What I need to do is hide the entry the user right clicked on and hide the selection. As I am new I am finding it hard going to code this as I have just finished my first course which entailed the basics. Any help would be appreciated or if you need anymore code, then please ask.
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub HideToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pnlContextMenuStrip1.ItemClicked
'Get the text of the item that was clicked on.
'Dim text As String = txtCustomerActive.Text
Try
'txtCustomerActive.Visible = False
pnlContextMenuStrip1.Visible = False
MessageBox.Show(txtCustomerActive.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
You could use Rows.Item() to hide specific DataGridViewRow, like:
If (UserDataGridView.Rows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
End If
I am assuming you are using FullRowSelect here.
If you are not using FullRowSelect you could have this alternative code which could catch both Cell being Selected or Row being Selected:
If (UserDataGridView.SelectedRows.Count > 0) Then
For Each row As DataGridViewRow In UserDataGridView.SelectedRows
UserDataGridView.Rows.Item(row.Index).Visible = False
Next
ElseIf (UserDataGridView.SelectedCells.Count > 0) Then
For Each cell As DataGridViewTextBoxCell In UserDataGridView.SelectedCells
UserDataGridView.Rows.Item(cell.RowIndex).Visible = False
Next
End If
To Unhide everything let's say from a Button Click you could have this:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For Each row As DataGridViewRow In UserDataGridView.Rows
If (row.Visible = False) Then
UserDataGridView.Rows.Item(row.Index).Visible = True
End If
Next
End Sub
As far as I know, you can not make a server-side handler for right mouse click (as you did for HideToolStripMenuItem_Click, which works as part of .NET postback mechanism).
However, I believe that such feature could be done with some client-side javascript progamming.
Hope this helps!