Solved

Update by either checkbox or textbox

Posted on 2011-09-05
11
268 Views
Last Modified: 2012-05-12
Greetings again mates.

I have been searching for answers to this one for awhile and I am still struggling.

I have 2 sections, Section 1 and Section 2.

Section one has 5 textboxes, textbox1...textbox5 and 5 checkboxes, checkbox1 through checkbox5.

Then section2 has 5 textboxes, checkAmount1 through checkAmount5.

The way it is supposed to work is that if you enter a number into any of the textobexes, say textbox1 for instance, and you check checkbox1, then the same number you enter into textbox is automatically entered into checKAmount1, textbox2, into checkAmount2, etc.

Here is where the issue comes in.

If you check checkbox1 FIRST FIRST and then enter a number into textbox1, everything works fine as expected.

If,however, you enter a number into textbox1 FIRST before checking a box, then nothing gets entered into the corresponding checkAmount.

I have struggled with this but no luck so far.

How do I tweak this code to make it happen?

Please let me know if you need an additional info.

        If chckamttext.Text <> "0" Then
            Select Case chckded
                Case "chck1"
                    If chckBox1.Checked AndAlso chck1.Text.Trim() <> [String].Empty Then
                        chckamtbot.Text = chckamttext.Text
                        chckfed.Text = fedname1.Text
                    End If
                Case "chck2"
                    If chckBox2.Checked AndAlso chck2.Text.Trim() <> [String].Empty Then
                        chckamtbot.Text = chckamttext.Text
                        chckfed.Text = fedname2.Text
                    End If

                Case "chck3"
                    If chckBox3.Checked AndAlso chck3.Text.Trim() <> [String].Empty Then
                        chckamtbot.Text = chckamttext.Text
                        chckfed.Text = fedname3.Text
                    End If

                Case "chck4"
                    If chckBox4.Checked AndAlso chck4.Text.Trim() <> [String].Empty Then
                        chckamtbot.Text = chckamttext.Text
                        chckfed.Text = fedname4.Text
                    End If

                Case "chck5"
                    If chckBox5.Checked AndAlso chck5.Text.Trim() <> [String].Empty Then
                        chckamtbot.Text = chckamttext.Text
                        chckfed.Text = fedname5.Text
                    End If
            End Select
        ElseIf chckamttext.Text = "0" And chckded <> "" Then
            chckamtbot.Text = "0"
            chckfed.Text = ""
        End If

Open in new window


Thanks
0
Comment
Question by:sammySeltzer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 9

Expert Comment

by:sarabhai
ID: 36485266
where u write the code.
code need to be write/execute after checkbox click and textbox change value.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 36486433
I'm afraid I did not fully understand the problem but... I'll try :)

To solve your task you need some event handlers for checkboxes.CheckedChanged event.

See my code below
Private Sub CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
        CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged

        Select Case DirectCast(sender, CheckBox).Name
            Case "CheckBox1"
                setControlsValues_1()
            Case "CheckBox2"
                setControlsValues_2()
            Case "CheckBox3"
                setControlsValues_3()

        End Select
    End Sub

    Private Sub setControlsValues_1()
        If Not String.IsNullOrEmpty(TextBox1.Text) AndAlso CheckBox1.Checked Then
            Me.chckamtbot1.Text = TextBox1.Text
        Else
            '?????
        End If
    End Sub
    Private Sub setControlsValues_2()
        If Not String.IsNullOrEmpty(TextBox2.Text) AndAlso CheckBox2.Checked Then
            Me.chckamtbot2.Text = TextBox2.Text
        Else
            '?????
        End If
    End Sub
    Private Sub setControlsValues_3()
        If Not String.IsNullOrEmpty(TextBox3.Text) AndAlso CheckBox3.Checked Then
            Me.chckamtbot3.Text = TextBox3.Text
        Else
            '?????
        End If
    End Sub

Open in new window

0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 36488419
anarki_jimbel,

Thankyou very much for your efforts and your response.

But what is it that you don't understand?

I will be more than glad to try to explain again.

I have 2 sections, Section1 and Section2.

On Section1, I have 5 textboxes and 5 checkboxes.

Let's call the textboxes, textbox1, textbox2, textbox3, textbox4, textbox5.

Then 5 checkboxes, checkbox1, checkbox2, checkbox3, checkbox4, checkbox5.

Then on Section2, we have another 5 textboxes - checkAmount1, checkAmount2, checkAmount3, checkAmount4, checkAmount5.

The way it is *supposed* to work is you enter a number into a textbox and then check the appropriate checkbox in Section1, then that amount goes in the corresponding textbox in Section2.

For instance, let's say we enter 2 box in textbox1 and check checkbox1 in Section1, that number - 2, will automatically be populated into CheckAmount1 in Section1.

So, whether you check a box FIRST before entering a number, or you enter a number FIRST before checking a box, as long as a number is entered into a textbox and the associated checkbox is checked as described above, that number should be automatically populated into the corresponding checkAmount in Section2.

If you enter a number but don't check a checkbox, that number should NOT be populated into the corresponding checkAmount in Section2

The way it is working so far is you MUST check a checkbox FIRST before entering a number.

If you do that, then that number goes into the corresponding CheckAmount in Section2.

We want it to work whether you check a checkbox first or you enter a number firs as long as a number is entered and the associated checkbox is checked, then that amount shouold go to the corresponding checkAmount in section2.

I hope this is clearer.

Again, thanks for your assistance.

I thought the code I posted above would do the trick but this one got me so stumped.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 36492086
OK, try this code. For simplicity I have only 3 controls of each type :)

I tested and believe that is what you want
Public Class Form1

    Private Sub CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
                CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, _
                TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged

        Select Case DirectCast(sender, Control).Name
            Case "CheckBox1", "TextBox1"
                setControlsValues_1()
            Case "CheckBox2", "TextBox2"
                setControlsValues_2()
            Case "CheckBox3", "TextBox3"
                setControlsValues_3()
        End Select
    End Sub

    Private Sub setControlsValues_1()
        If Not String.IsNullOrEmpty(TextBox1.Text) AndAlso CheckBox1.Checked Then
            Me.CheckAmount1.Text = TextBox1.Text
        Else
            '?????
        End If
    End Sub
    Private Sub setControlsValues_2()
        If Not String.IsNullOrEmpty(TextBox2.Text) AndAlso CheckBox2.Checked Then
            Me.CheckAmount2.Text = TextBox2.Text
        Else
            '?????
        End If
    End Sub
    Private Sub setControlsValues_3()
        If Not String.IsNullOrEmpty(TextBox3.Text) AndAlso CheckBox3.Checked Then
            Me.CheckAmount3.Text = TextBox3.Text
        Else
            '?????
        End If
    End Sub
End Class

Open in new window

0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 36492100
Probably Private Sub CheckedChanged not the best name. May be call it like ControlStateChanged or something else. Does not really matter :)
0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 36492932
Please help me understand; what is the difference between your code and mine?
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 36493324
OK, I didn't see your whole code :)

The main trick in my code is that the event handler handles BOTH text box and check box changes:

=====================================
 Sub CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _

                CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, _

                TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
=====================================

So, does not matter if you change text in a text box or check-uncheck checkbox - the system checks checkbox state and if allowed copies textbox content.

That's all!
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 36493330
Ask questions if not clear.
Unfortunately, it looks, we have some time shift. I live on Pacific time :)
0
 
LVL 29

Author Comment

by:sammySeltzer
ID: 36497553
thanks a lot.

However, I am sure there is more to this code.

I am running into many errors, like:

Handles clause requires a WithEvents variable defined in the containing type or one of its base types

or

'Name' is not a member of system.web.ui.control
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 36499121
OK, disregard my code. I missed it is ASP. The code is for forms...
My apologies
0
 
LVL 29

Author Closing Comment

by:sammySeltzer
ID: 36519660
It didn't solve my problem. but credit for efforts.

Thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question