CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Aug 2007
    Posts
    448

    [RESOLVED] Write method in short way for my textbox

    Hi guys

    I have a problem with method. I have trackbar with maximum value number up to 10000. When I write the value numbers from 0 to 999 on the textbox, the trackbar value moved as it works ok so far. On 1.0 where I want the trackbar value must be target on 1000, if I go over the limit like 1.1, I want the warning message pop up. But on mine I have to write the longest code from 1.0 (the trackbar maximum target 1000) to 100.0 (trackbar maximum target 2000). I don't want to write that crazy numbers to make my form slowing down.



    Here it the code:
    Code:
         If Form2.Textbox.Text = 1.1 Then
                    Form2.TrackBar2.Value = 1000
                    Textbox.ShowDialog()
                End If
            End If
    
    
                If Form2.Textbox.Text = 1.2 Then
                    Form2.TrackBar2.Value = 1000
                    Textbox.ShowDialog()
                End If
            End If
    
    
                If Form2.Textbox.Text = 1.3 Then
                    Form2.TrackBar2.Value = 1000
                    Textbox.ShowDialog()
                End If
            End If
    
                If Form2.Textbox.Text = 1.4 Then
                    Form2.TrackBar2.Value = 1000
                    Textbox.ShowDialog()
                End If
            End If
    
    
                If Form2.Textbox.Text = 1.5 Then
                    Form2.TrackBar2.Value = 1000
                    Textbox.ShowDialog()
                End If
            End If
    
    
                If Form2.Textbox.Text = 1.6 Then
                    Form2.TrackBar2.Value = 1000
                    Textbox.ShowDialog()
                End If
            End If
    
                If Form2.Textbox.Text = 1.7 Then
                    Form2.TrackBar2.Value = 1000
                    Textbox.ShowDialog()
                End If
            End If
    
                If Form2.Textbox.Text = 1.8 Then
                    Form2.TrackBar2.Value = 1000
                    Textbox.ShowDialog()
                End If
            End If
    
                If Form2.Textbox.Text = 1.9 Then
                    Form2.TrackBar2.Value = 1000
                    Textbox.ShowDialog()
                End If
            End If
    
    
                If Form2.Textbox.Text = 2.0 Then
                    Form2.TrackBar2.Value = 1000
                    Textbox.ShowDialog()
                End If
            End If
    
    ETC.....

    What is the short way of writing the code in proper way??



    Thanks,
    Mark
    Last edited by mark103; May 2nd, 2008 at 05:52 PM.

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Write method in short way for my textbox

    How about restricting the user to enter only upto 1? You don't have to write long code like this then.

  3. #3
    Join Date
    Aug 2007
    Posts
    448

    Re: Write method in short way for my textbox

    Do you know how to write the short code in one line if i type on over the limit from 1.0 to 100.0??




    Thanks,
    Mark

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Write method in short way for my textbox

    Have you tried writing it yourself? Here is a sample
    Code:
    'This is just a pseudo code, you will have to write the actual
    If value > 1 And Value <= 100 Then
          'This part of code will be executed when the value is between 1 and 100
    End If
    I would suggest you should read about If conditions and the logical operators.

  5. #5
    Join Date
    Aug 2007
    Posts
    448

    Re: Write method in short way for my textbox

    Thanks, we are nearly done!!!!! When I type 2,3,4,5,6,7,8,9 and 10 on the textbox without the dot (.) at the middle between with the two numbers from left and the right side, I get messagebox appears to pop up everytime.



    Code:
    If Form2.Textbox.Text >= 1.1 And Form2.Textbox.Text <= 10.0 Then
                    MessageBox.ShowDialog()
                End If
            End If

    I tried the code on below to work together with the method on above but it didn't work...


    Code:
    If Form2.Textbox.Text = "2" Then
           Trackbar2.Value = 2
    End If
    
    
    If Form2.Textbox.Text = "3" Then
           Trackbar2.Value = 3
    End If
    
    
    If Form2.Textbox.Text = "4" Then
           Trackbar2.Value = 4
    End If
    
    
    If Form2.Textbox.Text = "5" Then
           Trackbar2.Value = 5
    End If
    
    
    If Form2.Textbox.Text = "6" Then
           Trackbar2.Value = 6
    End If
    
    
    If Form2.Textbox.Text = "7" Then
           Trackbar2.Value = 7
    End If
    
    
    If Form2.Textbox.Text = "8" Then
           Trackbar2.Value = 8
    End If
    
    
    If Form2.Textbox.Text = "9" Then
           Trackbar2.Value = 9
    End If
    
    
    If Form2.Textbox.Text = "10" Then
           Trackbar2.Value = 10
    End If



    There is nothing wrong with 1.0, 1.1......etc........ When I type higher than 1.0, the messagebox appear to come up which it works great!!!!!! I need to fix with number from 2 to number 10. Then the problem will be solve it!



    Thanks,
    Mark

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Write method in short way for my textbox

    Ok. I am getting confused now. What exactly is the requirement? Can you write it down without the code? What exactly do you want in the text box and when should the message appear?

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Write method in short way for my textbox

    This should help mark103 explain himself:

    Mark103, PLEASE READ THIS
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Aug 2007
    Posts
    448

    Re: Write method in short way for my textbox

    The problem is when I type from 2 to 10 without the dot, I get message appear everytime. I am not type the number like 2.0, 3.0 or anything, just only put 2, 3 or any greater than 3 in the textbox. I want the message to appear if I type greater than 1.0. From what your code gave me works if I type greater than 1.0. The problem with 2 to 10 I get message appear.


    How can we fix that??



    Thanks,
    Mark


    Quote Originally Posted by Shuja Ali
    Ok. I am getting confused now. What exactly is the requirement? Can you write it down without the code? What exactly do you want in the text box and when should the message appear?

  9. #9
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Write method in short way for my textbox

    You didn't read the link I posted. That was not for anybody else here, who all know that. If you read that, you will be able to supply the correct TERM for what you want.

    Once you see it, you can then probably figure it out.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Write method in short way for my textbox

    Quote Originally Posted by mark103
    The problem is when I type from 2 to 10 without the dot, I get message appear everytime. I am not type the number like 2.0, 3.0 or anything, just only put 2, 3 or any greater than 3 in the textbox. I want the message to appear if I type greater than 1.0. From what your code gave me works if I type greater than 1.0. The problem with 2 to 10 I get message appear.


    How can we fix that??



    Thanks,
    Mark
    No Idea what you want, but see this:
    Code:
    Select Case Form2.Textbox.Text
    	Case Is = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    		'This code runs only when Text = "1", or Text="2", or ... Text="10"
    	Case Else
    		'This code runs in any other case, like Text="2.56", or .Text="$45,2" or .Text="word"
    End Select
    Also see:
    Code:
    If InStr(Form2.Textbox.Text, ".") > 0 Then
    	'This code runs only when Text contains a dot ( "." ) 
    	'Even if it is not a number
    	'Example: .Text="1.0" or .Text="as. is"
    else
    	'This code runs in any other case, like .text="3" or .Text="word"
    End If
    Also see:
    Code:
    Try
    	If Form2.Textbox.Text = Int(Form2.Textbox.Text) Then
    		 'This code runs only when .Text contains an Integer, with or without dot ( "." )
    		 'But never runs if it is not a number
    		'(Then runs the code after the "Catch e" line
    	else
    		 'This code runs in any other case in wich .Text is a number
    	End If
    Catch e as exception
    	'This code runs only when .Text is not a number
    end Try

  11. #11
    Join Date
    Aug 2007
    Posts
    448

    Re: Write method in short way for my textbox

    I don't think you did get it. I want to entered the numeric value into the textbox from 0 to 999 which it will control the trackbar value. And I want to entered the numeric value from 1.0 to 1.4 which it greater than 999. But the numeric value must not go greater than 1.4 in order to receive the message. On my code shows that I cannot entered the numeric value from 2 to 10 because it keep showing the message, I have no idea how I could change the code's behavior in order to get this job successful.





    If you are not 100% sure what I am trying to do, it would be good for you to check out on my following attachment. Take a look from there and don't do anything, just run the project and try to enter from 2 to 10 then you will see what happens. I did removed If Form2.Textbox.Text >= 1.1 And Form2.Textbox.Text <= 10.0 Then which it works but I need to keep them because they shows the message when I enter greater than 1.0 up to 10.0.



    Hope you realise what has been going on around.



    Thanks,
    Mark
    Attached Files Attached Files

  12. #12
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Write method in short way for my textbox

    Mark,

    Why don't you try this Link.

    But you never tried it the first time so why would you try it now.

    OK so now the parameters are changing. At least you are trying to get somewhere now.

    Code:
            If Form2.Textbox.Text.Contains(".") Then
                Dim TestDbl as double = CDBL(Form2.Textbox.text, Double)
                Form2.TrackBar2.Value = 1000
                If TestDbl > 1.0 AND TestDbl<1.41 then
                    Textbox.ShowDialog()
                End If
            Else
                Try
                    Me.TrackBar2.Value = Cint(Form2.Textbox.Text)
                Catch ex As Exception
                    MsgBox("Invalid value.")
                End Try
            End IfEnd Select
    Last edited by sotoasty; May 5th, 2008 at 02:50 PM.

  13. #13
    Join Date
    Aug 2007
    Posts
    448

    Re: Write method in short way for my textbox

    Ahhhhhh, thank you very much!!!!! This is what I want




    You have now save me and other guys's time without spent more time around by continue to try and fix the problem, I did not realise that there was old post at the old topic




    If I were retrieve at your old post, I wouldn't have post it again would I? Thank you very much for the help fella, and thanks for the help guys. Keep up the good work! You have been awesome



    Anyway solved



    Thanks,
    Mark

  14. #14
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Write method in short way for my textbox

    Quote Originally Posted by mark103
    I don't think you did get it.
    I agree on that
    Quote Originally Posted by mark103
    I want to entered the numeric value into the textbox from 0 to 999 which it will control the trackbar value.
    Let's see If I understand:
    You have a ComboBox control named ComboBox1. It have two states:
    State 1: "0 to 1.0"
    State 2: "0 to 10.0"

    I guess that you want to limit the TrackBar1 between "0 to 1.0", or "0 to 10", in agreement with your ComboBox1 strings (State 1 or State 2).

    the problem is that your TrackBar1 can take values between 0 and 1000, AND, it only can have INTEGER values.

    Your TrackBar1 can take those values: 0, 1, 2, 3, ... ,1000.

    a-You want to write a value in your TextBox, then push the Button1, and the program must set the TrackBar1 value between "0 to 1" or "0 to 10". But since the trackBar really varies between 0 to 1000, you want to work it some way.
    b-If the text on TextBox.Text is invalid, then you want to Warning the user that his value is not allowed.

    That is what I guess.

    Warning 1: Never name a TextBox "TextBox". Name it "TextBox1", "MyTextBox", or "TheTextBoxThatSetsTheTrackBar".
    If you name it "TextBox", VB.NET does not know if you want to use THAT TextBox, or ANY TextBox. It would do weird things.

    Now, let's go:

    0-Double click your "TextBox". You gonna go to
    Code:
    Private Sub Textbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textbox.TextChanged
    	Test5()
    End Sub
    Right click on test5() line, and choose "Go to Definition", You gonna get "Sub Test5()", you Sub routine that runs each time you change the text of "TextBox".

    1-The first thing you need to do is check If that TextBox.Text is a number, because the bratty User may have written anything (like ":-P").
    2-If is a number, then, you need to check if it is limited in agreement with the bounds on ComboBox1.
    4-If there are a problem, then you need to gently kick the user asss.
    5-If not was a problem, then you gonna set the TrackBar1.Value
    __5.1-Calculating the value to set:
    ____5.1.1 Convert between "0 to 1" (OR "0 to 10") to "0 to 1000".
    ____5.1.2 Update that value on TrackBar1.Value

    Now, lets see what you have writen:
    Code:
    		If Form2.Combobox1.SelectedItem = "0 to 1.0" Then
    			If Form2.Textbox.Text >= 1.1 And Form2.Textbox.Text <= 10.0 Then
    				MessageBox.ShowDialog()
    			End If
    
    			If Val(Form2.Textbox.Text) <= 999 Then
    				Form2.TrackBar1.Value = Val(Form2.Textbox.Text)
    			End If
    
    			If Form2.TrackBar1.Value = 1000 Then
    				Form2.Textbox.Text = "1.0"
    			End If
    		End If
    1-it did not check if it is a number
    2-Then, it check if the value is licitous, and Kick the poor User when 1.1<=TextBox.Text<=1.0!... Take it!, User
    3-After that, it updates TrackBar1 if TextBoxtext1.Text is <=999. So, if TextBoxtext1.Text="-1000000"....
    4-Then, if TrackBar1.Value = 1000 , then, it writes againt the TextBox, but that is writting to a TextBox; TATH runs again "Private Sub Textbox_TextChanged", so it run again "Sub Test5()"...

    I are short on time, so, I give you:
    Code:
    	Sub Test5()
    		If Not IsNumeric(Form2.Textbox.Text) Then
    			'Kick User here
    		Else
    			Select Case Form2.Combobox1.SelectedItem.ToString
    
    				Case "0 to 1.0"
    					If Val(Form2.Textbox.Text) >= 0 And Val(Form2.Textbox.Text) <= 1 Then 'All rigth? Update trackBar1
    						Form2.TrackBar1.Value = 1000 * Val(Form2.Textbox.Text)
    					Else
    						MessageBox.ShowDialog() 'Not Right?. User Kick
    					End If
    
    				Case "0 to 10.0"
    					If Val(Form2.Textbox.Text) >= 0 And Val(Form2.Textbox.Text) <= 10 Then 'All rigth? Update trackBar1
    						Form2.TrackBar1.Value = 100 * Val(Form2.Textbox.Text)
    					Else
    						MessageBox.ShowDialog() 'Not Right?. User Kick
    					End If
    
    			End Select 'Form2.Combobox1.SelectedItem.ToString
    		End If 'Not IsNumeric
    	End Sub 'Test5
    I hope you go crazy like Seal.
    Quote Originally Posted by mark103
    And I want to entered the numeric value from 1.0 to 1.4 which it greater than 999. But the numeric value must not go greater than 1.4 in order to receive the message. On my code shows that I cannot entered the numeric value from 2 to 10 because it keep showing the message, I have no idea how I could change the code's behavior in order to get this job successful.





    If you are not 100% sure what I am trying to do, it would be good for you to check out on my following attachment. Take a look from there and don't do anything, just run the project and try to enter from 2 to 10 then you will see what happens. I did removed If Form2.Textbox.Text >= 1.1 And Form2.Textbox.Text <= 10.0 Then which it works but I need to keep them because they shows the message when I enter greater than 1.0 up to 10.0.



    Hope you realise what has been going on around.



    Thanks,
    Mark
    Last edited by Marraco; May 5th, 2008 at 04:59 PM.

  15. #15
    Join Date
    Nov 2004
    Location
    Slough, UK
    Posts
    184

    Re: Write method in short way for my textbox

    Quote Originally Posted by Marraco
    I agree on thatLet's see If I understand:
    You have a ComboBox control named ComboBox1. It have two states:
    State 1: "0 to 1.0"
    State 2: "0 to 10.0"

    I guess that you want to limit the TrackBar1 between "0 to 1.0", or "0 to 10", in agreement with your ComboBox1 strings (State 1 or State 2).

    the problem is that your TrackBar1 can take values between 0 and 1000, AND, it only can have INTEGER values.

    Your TrackBar1 can take those values: 0, 1, 2, 3, ... ,1000.

    a-You want to write a value in your TextBox, then push the Button1, and the program must set the TrackBar1 value between "0 to 1" or "0 to 10". But since the trackBar really varies between 0 to 1000, you want to work it some way.
    b-If the text on TextBox.Text is invalid, then you want to Warning the user that his value is not allowed.

    That is what I guess.

    Warning 1: Never name a TextBox "TextBox". Name it "TextBox1", "MyTextBox", or "TheTextBoxThatSetsTheTrackBar".
    If you name it "TextBox", VB.NET does not know if you want to use THAT TextBox, or ANY TextBox. It would do weird things.

    Now, let's go:

    0-Double click your "TextBox". You gonna go to
    Code:
    Private Sub Textbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textbox.TextChanged
    	Test5()
    End Sub
    Right click on test5() line, and choose "Go to Definition", You gonna get "Sub Test5()", you Sub routine that runs each time you change the text of "TextBox".

    1-The first thing you need to do is check If that TextBox.Text is a number, because the bratty User may have written anything (like ":-P").
    2-If is a number, then, you need to check if it is limited in agreement with the bounds on ComboBox1.
    4-If there are a problem, then you need to gently kick the user asss.
    5-If not was a problem, then you gonna set the TrackBar1.Value
    __5.1-Calculating the value to set:
    ____5.1.1 Convert between "0 to 1" (OR "0 to 10") to "0 to 1000".
    ____5.1.2 Update that value on TrackBar1.Value

    Now, lets see what you have writen:
    Code:
    		If Form2.Combobox1.SelectedItem = "0 to 1.0" Then
    			If Form2.Textbox.Text >= 1.1 And Form2.Textbox.Text <= 10.0 Then
    				MessageBox.ShowDialog()
    			End If
    
    			If Val(Form2.Textbox.Text) <= 999 Then
    				Form2.TrackBar1.Value = Val(Form2.Textbox.Text)
    			End If
    
    			If Form2.TrackBar1.Value = 1000 Then
    				Form2.Textbox.Text = "1.0"
    			End If
    		End If
    1-it did not check if it is a number
    2-Then, it check if the value is licitous, and Kick the poor User when 1.1<=TextBox.Text<=1.0!... Take it!, User
    3-After that, it updates TrackBar1 if TextBoxtext1.Text is <=999. So, if TextBoxtext1.Text="-1000000"....
    4-Then, if TrackBar1.Value = 1000 , then, it writes againt the TextBox, but that is writting to a TextBox; TATH runs again "Private Sub Textbox_TextChanged", so it run again "Sub Test5()"...

    I are short on time, so, I give you:
    Code:
    	Sub Test5()
    		If Not IsNumeric(Form2.Textbox.Text) Then
    			'Kick User here
    		Else
    			Select Case Form2.Combobox1.SelectedItem.ToString
    
    				Case "0 to 1.0"
    					If Val(Form2.Textbox.Text) >= 0 And Val(Form2.Textbox.Text) <= 1 Then 'All rigth? Update trackBar1
    						Form2.TrackBar1.Value = 1000 * Val(Form2.Textbox.Text)
    					Else
    						MessageBox.ShowDialog() 'Not Right?. User Kick
    					End If
    
    				Case "0 to 10.0"
    					If Val(Form2.Textbox.Text) >= 0 And Val(Form2.Textbox.Text) <= 10 Then 'All rigth? Update trackBar1
    						Form2.TrackBar1.Value = 100 * Val(Form2.Textbox.Text)
    					Else
    						MessageBox.ShowDialog() 'Not Right?. User Kick
    					End If
    
    			End Select 'Form2.Combobox1.SelectedItem.ToString
    		End If 'Not IsNumeric
    	End Sub 'Test5
    I hope you go crazy like Seal.
    All he needed was some code to copy and paste into his program. He simply wasn't converting the string to a double using Double.Parse.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || ClickOnline ||

    Did I ever say I was an expert?

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Please mark threads resolved by going to the thread tools menu and clicking the Mark Thread Resolved button.

    Has a post really helped you? Please Rate it.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured