Open frmDataGrid from Form # 1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dorandoran
    New Member
    • Feb 2007
    • 145

    Open frmDataGrid from Form # 1

    This code on my form # 1 button.

    Code:
            If txtDate.MaskCompleted = True Then
                Dim form As New frmDataGrid
                form.Show()
            Else
                MessageBox.Show("You must input a date")
                Exit Sub
            End If
    I added a form "frmDataGri d" and used vb.net toolbox to create datagrid. I did pick data source and it's a Stored Procedure(@tDat e is require parameter). How do I pass a parameter from my form #1 (txtDate) to frmDataGrid?
  • dfarney
    New Member
    • Nov 2007
    • 6

    #2
    Originally posted by dorandoran
    This code on my form # 1 button.

    Code:
            If txtDate.MaskCompleted = True Then
                Dim form As New frmDataGrid
                form.Show()
            Else
                MessageBox.Show("You must input a date")
                Exit Sub
            End If
    I added a form "frmDataGri d" and used vb.net toolbox to create datagrid. I did pick data source and it's a Stored Procedure(@tDat e is require parameter). How do I pass a parameter from my form #1 (txtDate) to frmDataGrid?
    In C#:

    create your frmDataGrid class like so...
    Code:
    public partial class frmDataGrid : Form
    {
        [B]private string date;[/B]
    
        public frmDataGrid()
        {
            InitializeComponent();
        }
    
        [B]public frmDataGrid(string _date)
        {
            InitializeComponent();
            date = _date;
        }[/B]
    }
    Call it like so...
    Code:
    if (txtDate.MaskCompleted)
    {
        frmDataGrid frm = [B]new frmDataGrid(txtDate.Text);[/B]
        frm.Show();
    }
    else
    {
        MessageBox.Show("C# is far superior to VB :)");
    }

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Originally posted by dorandoran
      How do I pass a parameter from my form #1 (txtDate) to frmDataGrid?
      There are two ways to do this. One way to do this is to modify your frmDataGrid's constructor. Another way is to add a Public member to frmDataGrid, and after you instantiate it, set the value.

      I'll show you the first way. Add a parameter to the constructor. If you did not define a constructor, you need to add one.

      So if you wanted to pass a String for example, do this in frmDataGrid:
      Code:
       
      Private paramS as String
      
      Public Sub New(s as String)
      		' This call is required by the Windows Form Designer.
      		InitializeComponent()
      
      		' Add any initialization after the InitializeComponent() call.
      		paramS = s
      End Sub
      And you would have to change the way you call the form:
      Code:
      Dim form as New frmDataGrid("whatever string you want to pass")
      form.Show()

      Comment

      • dorandoran
        New Member
        • Feb 2007
        • 145

        #4
        Thank you guyz for all your help. Really good tips and code. I truly appreciate it.

        Thanks

        Comment

        Working...