none
Is there a C# equivalent of VB's My.Forms? RRS feed

  • Question

  • Is there a C# equivalent of VB's My.Forms? In VB I used the following line:

    My.Forms.frmGCalc.txtInput.Text = HistoryListBox.Text

    to send the text of the selected item in the listbox (on form 2) to the text of the txtInput textbox (on form 1).

    I'm trying to figure out how to do the same in C#.

    Any help appreciated,

    thanks.

    Tuesday, December 17, 2019 3:06 PM

Answers

  • Hello,

    If we take My.Forms and Application.OpenForms out of the picture and consider changing the Text property of a TextBox in another form e.g.

    frmGCalc.txtInput.Text = "Something";

    This is not possible as by default the Modifiers property of controls on a form are private. You can expose the Text property say like this.

    public string TextInputText 
    {
        get => txtInput.Text;
        set => txtInput.Text = value;
    }

    Then 

    var f2 = (Form2)Application.OpenForms["Form2"];
    f2.TextInputText = HistoryListBox.Text;


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Integrater Wednesday, December 18, 2019 11:04 PM
    Wednesday, December 18, 2019 11:16 AM
    Moderator

All replies

  • If you haven't read this (old) blog you might want to - but be sure to read
    all of the comments.

    VB’s *My* for C# Developers
    https://blogs.msdn.microsoft.com/danielfe/2005/06/14/vbs-my-for-c-developers/

    - Wayne

    Tuesday, December 17, 2019 7:04 PM
  • Also see:

    Magic Forms Access in Visual Basic and C#
    https://visualstudiomagazine.com/articles/2016/10/01/magic-forms-access.aspx

    - Wayne

    Tuesday, December 17, 2019 7:07 PM
  • Hi Integrater,
    The My.Forms provides properties for accessing an instance of each Windows form declared in the current project.
    To achieve the same function in C#, you can use Application.OpenForms property that gets a collection of open forms owned by the application.

    Form2 f2=(Form2)Application.OpenForms["Form2"];

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 18, 2019 2:56 AM
  • Hi Integrater,
    The My.Forms provides properties for accessing an instance of each Windows form declared in the current project.
    To achieve the same function in C#, you can use Application.OpenForms property that gets a collection of open forms owned by the application.

    Form2 f2=(Form2)Application.OpenForms["Form2"];

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thanks for that information Daniel, do you know how I can apply this to my first form? Since I don't manually open it I can't figure out how I would write that for my first form.
    Wednesday, December 18, 2019 10:54 AM
  • Hello,

    If we take My.Forms and Application.OpenForms out of the picture and consider changing the Text property of a TextBox in another form e.g.

    frmGCalc.txtInput.Text = "Something";

    This is not possible as by default the Modifiers property of controls on a form are private. You can expose the Text property say like this.

    public string TextInputText 
    {
        get => txtInput.Text;
        set => txtInput.Text = value;
    }

    Then 

    var f2 = (Form2)Application.OpenForms["Form2"];
    f2.TextInputText = HistoryListBox.Text;


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Integrater Wednesday, December 18, 2019 11:04 PM
    Wednesday, December 18, 2019 11:16 AM
    Moderator
  • That's very interesting indeed. My textbox is on form1 though, and I'm not experienced enough to know how to modify your code to deal with a textbox on form1 instead.

    Could you help me out?

    Wednesday, December 18, 2019 3:35 PM
  • Hello, 

    You have a namespace in C# code - it need to perceed a form calss name if you if different than namespace of current form.

    Instance of form type variable you may get by using

    Application.OpenForms[formname]

    If you use this - I would recoment to have FormName as a string constant in from code and use 

    Application.OpenForms[TFormType.FormName]



    Sincerely, Highly skilled coding monkey.

    Wednesday, December 18, 2019 4:03 PM
  • > to send the text of the selected item in the listbox (on form 2) to the text of the txtInput textbox (on form 1).

    Why not simply add a frmGCalc as a parameter on construction of Form2, store it locally and than use it when needed?


    Sincerely, Highly skilled coding monkey.

    Wednesday, December 18, 2019 4:07 PM
  • That's very interesting indeed. My textbox is on form1 though, and I'm not experienced enough to know how to modify your code to deal with a textbox on form1 instead.

    Could you help me out?

    Like this

    var f1 = (Form1)Application.OpenForms["Form1"];
    f1.TextInputText = HistoryListBox.Text;


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, December 18, 2019 4:22 PM
    Moderator