Link to home
Start Free TrialLog in
Avatar of suran78
suran78

asked on

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
Avatar of oleggold
oleggold
Flag of United States of America image

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!
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
That if You want the decrease to be 0 if bellow 0,You can always put there a form validator in ASP.net
Avatar of suran78
suran78

ASKER

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
Avatar of suran78

ASKER

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.
  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
Avatar of suran78

ASKER

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:
 
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
Avatar of suran78

ASKER

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.
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>
ASKER CERTIFIED SOLUTION
Avatar of Rejojohny
Rejojohny
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of suran78

ASKER

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.  

Avatar of suran78

ASKER

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

 
Avatar of suran78

ASKER

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

Avatar of suran78

ASKER

I am closing this question.  Thanks for the big help.  I have another question open at https://www.experts-exchange.com/questions/21340531/Datagrid-question.html

If you are interested please reply.