Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1333
  • Last Modified:

Increment and decrement button.

Experts,

I am creating a asp.net/vb.net form in which one entry called Patch_no will be a numeric value (starting from 0) with text box(readonly).  beside this readonly text box there should be 2 buttons, increase and decrease.  When the increase button is clicked the number in readonly textbox wil increase by 1 and if decrease is clicked it will decrease but not go below 0.

Please give me the code to create this kind of data entry.

S
0
suran78
Asked:
suran78
  • 8
  • 4
  • 3
1 Solution
 
oleggoldCommented:
Hi suran78,
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim i As Integer

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(64, 104)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "TextBox1"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(56, 192)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(192, 192)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Button2"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(56, 160)
        Me.Button3.Name = "Button3"
        Me.Button3.TabIndex = 3
        Me.Button3.Text = "Button3"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
        Me.ClientSize = New System.Drawing.Size(292, 268)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '     Dim s As Date = CDate(Me.TextBox1.Text)
        i += 1
        Me.TextBox1.Text = i
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        i -= 1
        Me.TextBox1.Text = i
    End Sub
End Class
Hope It helps
Cheers!
0
 
oleggoldCommented:
sorry,
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        i -= 1
        If i > 0 Then
            Me.TextBox1.Text = i
        Else
            Me.TextBox1.Text = 0
        End If
    End Sub
0
 
oleggoldCommented:
That if You want the decrease to be 0 if bellow 0,You can always put there a form validator in ASP.net
0
[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

 
suran78Author Commented:
Thanks it working.  But the increment button is incrementing to 1 only, if I click again it should become 2 and if I click for the 3rd time it should become 3, same for decrement, it wil go down by 1 everytime decrement is clicked.  Sorry I did make it clear in teh beginning.  Please give me the code for

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '     Dim s As Date = CDate(Me.TextBox1.Text)
        i += 1
        Me.TextBox1.Text = i
    End Sub
And

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        i -= 1
        If i > 0 Then
            Me.TextBox1.Text = i
        Else
            Me.TextBox1.Text = 0
        End If
    End Sub
0
 
suran78Author Commented:
This is a bit complicated.  The patch no text box should show the value in Patch_no field from Product table.  The default value is 0, this field is never empty( it has value 0, 1, 2, 3, ..).  How do I retrieve and display this value from the tabl and make it readonly in the textbox?  and as I wrote earlier, the incre and decr button will increase and decrease by 1 everytime it is clicked.  Just to make it more clear:

Patch NO    | 0 |  Button increase   Button Decrease ,<-------------textbox shows value from patchno field in Product table, but the user cannot make changes in the Patch textbox directly ( that's why readonly) , he can only increase and decrease the value.
0
 
RejojohnyCommented:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.TextBox1.Text  += 1
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.TextBox1.Text  -= 1
        if Me.TextBox1.Text < 0 then
                  Me.TextBox1.Text  = 0
        end if
    End Sub
0
 
suran78Author Commented:
Rejojohny,

I am getting this error:

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 101:
Line 102:    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Line 103:        Me.TextBox1.Text += 1 <----------------error
Line 104:    End Sub
Line 105:
 
0
 
RejojohnyCommented:
what is the value in the textbox? was it not defaulted to 0?
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
       if isnumeric(TextBox1.Text ) then
           TextBox1.Text += 1  
       end if
End Sub
0
 
suran78Author Commented:
No, that was part of this question,

1.  How do I get the value from field PatchNo in SQL database table - Product.  This value in table can be 0, 1, 2,....
Once the default value is displayed as readonly in the text box,

2.  the user will click increase or decrease button to change the value.  

3. Click save to save the change value in table.

I will increase the points for this question, please give me the solution in ASP.net/vb.net codes.
0
 
RejojohnyCommented:
am sorry for my previous post .. never read ur question completely .. ur question is for ASP.net/Vb.net
>>I am creating a asp.net/vb.net
I just had a look at the previous posts and assumed that u requested solution in windows forms .. here's the code the increment/decrement of the values .. uses javascript .. am just pasting the code for the aspx page .. will come back later with the code for retrieveing and saving to the database .. do remember to increase the points :-)

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="TestApp.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
            <script language="javascript">
                  function Decrease()
                  {
                        //alert(document.Form1
                        document.Form1.txtPatchNo.value = parseInt(document.Form1.txtPatchNo.value) - 1;
                        if (parseInt(document.Form1.txtPatchNo.value) < 0)
                              document.Form1.txtPatchNo.value = 0;
                  }
                  function Increase()
                  {
                        //alert(document.Form1.txtPatchNo.value );
                        document.Form1.txtPatchNo.value = parseInt(document.Form1.txtPatchNo.value) + 1;
                  }
            </script>
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:TextBox id="txtPatchNo" style="Z-INDEX: 100; LEFT: 24px; POSITION: absolute; TOP: 34px"
                        runat="server" Enabled="False">0
                        </asp:TextBox><INPUT id="butDecrease" style="Z-INDEX: 103; LEFT: 295px; WIDTH: 96px; POSITION: absolute; TOP: 33px; HEIGHT: 24px"
                        type="button" value="Decrease" onclick="javascript:Decrease()"> <INPUT id="butIncrease" style="Z-INDEX: 102; LEFT: 193px; WIDTH: 96px; POSITION: absolute; TOP: 33px; HEIGHT: 24px"
                        type="button" value="Increase" onclick="javascript:Increase()">
            </form>
      </body>
</HTML>
0
 
RejojohnyCommented:
ok .. here is code to fetch and update ... i have added a new button in the aspx page
.aspx page .. new button added
<asp:Button id="butSave" style="Z-INDEX: 104; LEFT: 28px; POSITION: absolute; TOP: 82px" runat="server"
                        Text="Save" Width="100px"></asp:Button>


--------------- .VB page
Imports System.Data.SqlClient
Public Class WebForm1
    Inherits System.Web.UI.Page
    Dim myConnection As SqlConnection
    Protected WithEvents butSave As System.Web.UI.WebControls.Button
    Dim myCommand As SqlCommand
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents txtPatchNo As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=urdatabase")
        'you need to provide password for sql server
        Dim mycommand As New SqlCommand("SELECT patchno FROM product", myConnection)
        myConnection.Open()
        Dim objDataReader2 As SqlDataReader
        objDataReader2 = mycommand.ExecuteReader()
        objDataReader2.Read()
        txtPatchNo.Text = objDataReader2(0).ToString()
        myConnection.Close()
    End Sub

    Private Sub butSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSave.Click
        myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=urdatabase")
        'you need to provide password for sql server
        myConnection.Open()
        myCommand = New SqlCommand("Insert into Product(PatchNo) values (" & txtPatchNo.Text & ")", myConnection)
        myCommand.ExecuteNonQuery()

        myConnection.Close()

    End Sub
End Class
0
 
suran78Author Commented:
Thanks a lot.  This is working like wonder.  I have not yet implemented the second part - connecting with database.  I am going to try that now.  just wanted to let you know that the increase and decrease is working wonderfully.  I have increased points to 500 for thsi ques.  I might have some additional question later.  If you agree to answer them here then i will add another 100.  

0
 
suran78Author Commented:
I am getting this error, I executed the sql query, and it is returning result:

Exception Details: System.InvalidOperationException: Invalid attempt to read when no data is present.

Source Error:
Line 99:         objDataReader2 = mycommand.ExecuteReader()
Line 100:        objDataReader2.Read()
Line 101:        txtPatchNo.Text = objDataReader2(0).ToString() <-------- error
Line 102:        Conn.Close()

The values from dropdown1 and 2 are taken and then the patch no is selected from the SQL table with WHERE clause that retrieves patch no with dropdown1 and 2's values.  Here is the code that I am executing when value in dropdown2 is selected.

Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged

        Dim sBlock As String = DropDownList1.SelectedItem.Value.ToString()
        Dim sSeries As String = DropDownList2.SelectedItem.Value.ToString()

        Dim Conn As New SqlConnection(connectionString)
       Dim mycommand As New SqlCommand("SELECT patches FROM cell_series where Block_no = '" & sBlock & "' and   series_no = '" & sSeries & "'", Conn)
        Conn.Open()
        Dim objDataReader2 As SqlDataReader
        objDataReader2 = mycommand.ExecuteReader()
        objDataReader2.Read()
        txtPatchNo.Text = objDataReader2(0).ToString()
        Conn.Close()

    End Sub

 
0
 
suran78Author Commented:
It's working now.  My mistake,  the query returned 2 records.  I have added another where condition and it's working.  I wil close thsi question after trying the save data part.

Thanks

0
 
suran78Author Commented:
I am closing this question.  Thanks for the big help.  I have another question open at http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21340531.html

If you are interested please reply.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now