locked
transformation values form window to another RRS feed

  • Question

  • Hi , 

    My problem is that i have three forms , in the first two form there is textbox and button , in the the last form there is label to show the result of the summation of the value of the textbox in form1 and form2 after converted them to single type. I used me.close to close first form and open the next one and here the problem is exit , the vs studio said that there is probelm of conversion of type of textbox from string to single, but when i delete the me.close the app work successfully. 

    the code is :

    form1:

    form2.Show()
    Me.close()

    form2:

     Dim a, b As Single
            a = Form1.TextBox1.Text
            b = TextBox1.Text
            Form3.Label1.Text = a + b
            Form3.Show()
            Me.Close()

    please help me fix it!!!

    Thursday, July 6, 2017 5:57 PM

All replies

  • Hi , 

    My problem is that i have three forms , in the first two form there is textbox and button , in the the last form there is label to show the result of the summation of the value of the textbox in form1 and form2 after converted them to single type. I used me.close to close first form and open the next one and here the problem is exit , the vs studio said that there is probelm of conversion of type of textbox from string to single, but when i delete the me.close the app work successfully. 

    the code is :

    form1:

    form2.Show()
    Me.close()

    form2:

     Dim a, b As Single
            a = Form1.TextBox1.Text
            b = TextBox1.Text
            Form3.Label1.Text = a + b
            Form3.Show()
            Me.Close()

    please help me fix it!!!

    Hi

    A real round about style is that. Anyway, here is some code for each of Form1, Form2 and Form3 that does what you ask.

    FORM1

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f2 As New Form2 With {.CarryValue = TextBox1.Text}
            f2.Show()
            Hide()
        End Sub
    End Class
    

    FORM2

    Option Strict On
    Option Explicit On
    Public Class Form2
        Property CarryValue As String
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f3 As New Form3 With {.CarryValue1 = CarryValue, .CarryValue2 = TextBox1.Text}
            f3.Show()
            Hide()
        End Sub
    End Class

    FORM3

    Option Strict On
    Option Explicit On
    Public Class Form3
        Property CarryValue1 As String
        Property CarryValue2 As String
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Label1.Text = (GetSingle(CarryValue1) + GetSingle(CarryValue2)).ToString
        End Sub
        Function GetSingle(s As String) As Single
            Dim v As Single
            If Single.TryParse(s, v) Then Return v
            Return 0
        End Function
    End Class
    Notice that I have used variations on variable names around 'CarryValue' you will probably need to use your own so you can keep track of what each is doing. Notice also, I have used Hide rather than Close as closing the Forms like that can End the application.


    Regards Les, Livingston, Scotland

    Thursday, July 6, 2017 6:24 PM
  • So if I understand the question properly, you just need to convert the string value of the textbox to single yes?

    Dim a, b As Single
            a = Convert.ToSingle(Form1.TextBox1.Text)
            b = Convert.ToSingle(TextBox1.Text)
            Form3.Label1.Text = a + b
            Form3.Show()
            Me.Close()

    Thursday, July 6, 2017 6:31 PM
  • My problem is that i have three forms , in the first two form there is textbox and button , in the the last form there is label to show the result of the summation of the value of the textbox in form1 and form2 after converted them to single type.

    That's not the code from the examples provided when you previously asked this question and which you marked as an answer. 

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/a801831f-5de2-4d01-879b-7998f9bfee41/add-values-of-two-text-box-and-show-the-result-in-the-another-form?forum=vbgeneral#a801831f-5de2-4d01-879b-7998f9bfee41

    You should examine those examples and adjust your code to include the information provided.  That will ensure that it is not necessary to start the discussion over again from the beginning.

    Thursday, July 6, 2017 10:08 PM
  • Hello,

    I favor using a special TextBox control that in this case has a property of SingleValue e.g.

    <Browsable(False)>
    Public ReadOnly Property SingleValue As Single
        Get
            If String.IsNullOrWhiteSpace(Text) Then
                Return 0
            Else
                Return CSng(Text)
            End If
        End Get
    End Property

    There are ones for Integer and Double too so to get the Single value of an instance of the control on form 2 we use

    Dim SingleValue As Single = NumericTextbox1.SingleValue

    While in form 2, the following code gets the value (same type of TextBox) from the parent form, Form1 and adds it to the TextBox on the current form, Form2 and display the result in Form3.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim f As New Form3
        f.Owner = Me
        f.Label1.Text = $"{CType(Owner, Form1).NumericTextbox1.SingleValue + NumericTextbox1.SingleValue}"
        f.Show()
        Hide()
    End Sub

    Add this control to your project for the special TextBox used above which has more functionality then you need such as imposed min max values.

    Imports System.ComponentModel
    Public Class numericTextbox
        Inherits TextBox
    
        <Category("Behavior")>
        <Description("Acceptable min value")>
        Public Property Min As Integer
    
        <Category("Behavior")>
        <Description("Acceptable max value")>
        Public Property Max As Integer
    
    
        <Browsable(False)>
        Public ReadOnly Property IsValid As Boolean
            Get
                Return IntegerValue.Between(Min, Max) AndAlso DoubleValue.Between(Min, Max)
            End Get
        End Property
    
        Const WM_PASTE As Integer = &H302
    
        <Browsable(False)>
        Public ReadOnly Property DoubleValue As Double
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CDbl(Text)
                End If
            End Get
        End Property
        <Browsable(False)>
        Public ReadOnly Property IntegerValue As Integer
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CInt(Text)
                End If
            End Get
        End Property
        <Browsable(False)>
        Public ReadOnly Property SingleValue As Single
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CSng(Text)
                End If
            End Get
        End Property
        Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
            Dim Value As String = Text
            Value = Value.Remove(SelectionStart, SelectionLength)
            Value = Value.Insert(SelectionStart, e.KeyChar)
            e.Handled = CBool(Value.LastIndexOf("-") > 0) Or
                Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar) OrElse
                     (e.KeyChar = "."c And
                      Not Text.Contains(".") Or e.KeyChar = "."c And
                      SelectedText.Contains(".")) OrElse (e.KeyChar = "-"c And
                                                             SelectionStart = 0))
    
            MyBase.OnKeyPress(e)
        End Sub
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_PASTE Then
                Dim Value As String = Text
                Value = Value.Remove(SelectionStart, SelectionLength)
                Value = Value.Insert(SelectionStart, Clipboard.GetText)
                Dim result As Decimal = 0
                If Not Decimal.TryParse(Value, result) Then
                    Return
                End If
            End If
            MyBase.WndProc(m)
        End Sub
    End Class
    Public Module GenericExtensions
        <Runtime.CompilerServices.Extension>
        Public Function Between(Of T As IComparable(Of T))(ByVal actual As T, ByVal lower As T, ByVal upper As T) As Boolean
            Return actual.CompareTo(lower) >= 0 AndAlso actual.CompareTo(upper) < 0
        End Function
    End Module

    Form1 code (requires a the control above and a button)

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2
            f.Owner = Me
            f.Show()
            Hide()
        End Sub
    End Class
    

    Form2, the control and button

    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form3
            f.Owner = Me
            f.Label1.Text = $"{CType(Owner, Form1).NumericTextbox1.SingleValue + NumericTextbox1.SingleValue}"
            f.Show()
            Hide()
        End Sub
        Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
            CType(Owner, Form1).Show()
        End Sub
    End Class

    Form3 has a label only

    Public Class Form3
        Private Sub Form3_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
            CType(Owner, Form2).Show()
        End Sub
    End Class

    I place 4 in the control on Form1, 5 in the control on Form2 which yields 9 in the label on Form3. 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, July 6, 2017 10:59 PM
  • Ahmed,

    First of all I would avoid calculation with presentation values. 

    However, that does not lead to what I want to write.

    The development team of Microsoft has still a VB6 complex. They created something great and that was not recognized. 

    In version 15 Option Strict which was made for conversion with VB6 is still default off to get compatibility with that in fact lousyversion of VB. 

    With Option strict off, it acts like VB6, you know absolutely not what happens at runtime and customers are complaining.

    Therefore change in the Options Option Strict to On or put it every time in top of your code. 

    Maybe you think I've to do something more. That is true, but it saves you from calling customers.

    Also avoid the Windows'95 single, even the integer is not the best, but that has less problems.

     


    Success
    Cor


    Thursday, July 6, 2017 11:16 PM