Question How to update data in a text box on form 1 from a textbox on another form?

mnhk2006

New member
Joined
May 15, 2011
Messages
2
Programming Experience
5-10
Hello All,
Could someone help me on this situation? I was using 2 forms, the first one has general data capture and the second one is a calendar form. On Form1 I have mulitple windows controls. A Textb ox and beside a Calendar button. When I click on a Calendar Command button, it should open calendar form and once the user selects the date, the date should be placed in the text box on the form1. Identical to this on Form1 I need multiple textboxes that needs date to be captured. I cannot use a global variable for this as I need to validate mulitple text boxes. To explain this, I have a Transaction Date and Date Received. I need to validate whether Transaction date is prior Date Received or not? I dont want to use several global variables b'coz I might need multiple validations as above. Please help me on this.

Thanks in anticipation.
 
Can you not just use DateTimePickers instead of TextBoxes? They incorporate their own drop-down calendar and they let you set MinValue and MaxValue properties to force a valid date range.
 
Can you not just use DateTimePickers instead of TextBoxes? They incorporate their own drop-down calendar and they let you set MinValue and MaxValue properties to force a valid date range.

i agree with jmcilhinney, but if for some reason you cant use the datetimepickers control, heres another suggestion, you can try overloading the showdialog function of a form in order to pass the date from one form to another.

for example

The form that opens the dialog box
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim getDate As Date

        getDate = Form2.showDialog()

        MessageBox.Show(getDate.ToString())
    End Sub
End Class



and this is the form that will be opened as a dialog by form1

Public Class Form2
    Dim selectedDate As Date = New Date(2011, 4, 20)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.DialogResult = DialogResult.Yes
    End Sub

    Overloads Function showDialog() As Date
        If MyBase.ShowDialog() = DialogResult.Yes Then
            Return selectedDate

        Else
            Return Nothing
        End If
    End Function
End Class
 
You can't overload a method simply by changing the return type. You must change the number and/or type of the parameters in order to overload. Also, overloading ShowDialog is a bad idea. If you want to pass data into and/or out of a form then all you need to do is declare the appropriate properties in that form, just like any other type. You can set the properties before showing the form to pass data in and then get the properties after the dialogue is dismissed. Alternatively, you can declare a constructor with parameters in order to pass data in.
 
You can't overload a method simply by changing the return type. You must change the number and/or type of the parameters in order to overload. Also, overloading ShowDialog is a bad idea.

Hey jmcilhinney, i see what your saying, it def is not the best approach. I understand how you cant overload a function by only having different return types and all but the code i wrote above does work, originally i tried to override it, but showdialog is not overridable.

I tested the code again, and it works. Maybe it has something to do with the base class function? :confused: After i overload it, it seems to just basically override the function instead. Any idea why? Like with the code above if i tried
Dim diag As DialogResult = Form2.ShowDialog()

the compiler would tell me it can not implicitly convert from date to a dialogresult because it is returning a date type from the overload/"overrided??" function :confused:



You can set the properties before showing the form to pass data in and then get the properties after the dialogue is dismissed. Alternatively, you can declare a constructor with parameters in order to pass data in.
Yeah i agree with you, thats what i would prefer to do, i was just trying to think of a way to do it without having to deal with properties or public variables or of the sort.


I still consider myself a beginner-intermediate programmer(1-3yrs), im mainly self taught, i love to program, practically obsessed. I'm sorry if some of my suggestions aren't always the best approach or programming practice, just trying to share my ideas to help others and get some feedback on my code. I definitely appreciate any critique or comments on any my code, thanks :D
 
The compiler doesn't complain about that Overloads declaration because Overloads only applies to the current class. You're telling the compiler that that is one overload of the 'showDialog' method in the Form2 class, but there aren't any other overloads of that method. When you actually overload a member, you can access all overloads. What you're actually doing there is "shadowing", i.e. the member in the derived class hides the member in the base class. It's similar to overriding but it is not the same. The difference is that, if you cast a derived type as its base type and then access a member, an overridden member will still invoke the derived implementation, while the shadowed member will invoke the base implementation.
 
The compiler doesn't complain about that Overloads declaration because Overloads only applies to the current class. You're telling the compiler that that is one overload of the 'showDialog' method in the Form2 class, but there aren't any other overloads of that method. When you actually overload a member, you can access all overloads. What you're actually doing there is "shadowing", i.e. the member in the derived class hides the member in the base class. It's similar to overriding but it is not the same. The difference is that, if you cast a derived type as its base type and then access a member, an overridden member will still invoke the derived implementation, while the shadowed member will invoke the base implementation.

Thanks jmcilhinney, I did a little playing with it and i think i got it. So basically in the situation with the code above replacing the overloads with "Shadows" would make no difference because thats what the overloads is doing here.
 
Back
Top