Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 59235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:58:24+00:00 2026-05-10T17:58:24+00:00

I am creating a department picker form that is going to serve as a

  • 0

I am creating a ‘department picker’ form that is going to serve as a modal popup form with many of my ‘primary’ forms of a Winforms application. Ideally the user is going to click on an icon next to a text box that will pop up the form, they will select the department they need, and when they click OK, the dialog will close and I will have the value selected for me to update the textbox with.

I’ve already done the route with passing the owner of the dialog box into the dialog form and having the OK button click event do the proper update, but this forces me to do a DirectCast to the form type and I can then only reuse the picker on the current form.

I have been able to use a ByRef variable in the constructor and successfully update a value, but it works only in the constructor. If I attempt to assign the ByRef value to some internal variable in the Department Picker class, I lose the reference aspect of it. This is my basic code attached to my form:

Public Class DeptPicker     Private m_TargetResult As String     Public Sub New(ByRef TargetResult As String)        InitializeComponent()        ' This works just fine, my 'parent' form has the reference value properly updated.       TargetResult = 'Booyah!'        ' Once I leave the constructor, m_TargetResult is a simple string value that won't update the parent       m_TargetResult = TargetResult     End Sub     Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click        DialogResult = Windows.Forms.DialogResult.OK        ' I get no love here. m_TargetResult is just a string and doesn't push the value back to the referenced variable I want.       m_TargetResult = 'That department I selected.'       Me.Close()     End Sub     Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click        DialogResult = Windows.Forms.DialogResult.Cancel       Me.Close()     End Sub  End Class 

Can somebody tell me what I’m missing here or a different approach to make this happen?

Note: Code sample is in VB.NET, but I’ll take any C# answers too. 8^D

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:58:25+00:00Added an answer on May 10, 2026 at 5:58 pm

    In such cases, I usually either

    • Write a ShowDialog function that does what I want (e.g. return the value) or
    • Just let the result be a property in the dialog. This is how the common file dialogs do it in the BCL. The caller must then read the property to get the result. That’s fine in my opinion.

    You can also combine these methods, by making the result value a property in the dialog and creating a ShowDialog method that returns that property value, either as ByRef as you want or as a return value, depending on your needs.

    I’ll add this as a usage instruction, for example (sorry, no VB over here, and you said C# is welcome):

    using (var dlg = new DeptPicker()) {     if (dlg.ShowDialog() == DialogResult.OK) {         myTextBoxOrWhatEver.Text = dlg.TargetResult;     } } 

    In the dialog itself, just do this:

    void okButton_Click(object sender, EventArgs e) {     TargetResult = whatever; // can also do this when the selection changes     DialogResult = DialogResult.OK;     Close(); } 

    I didn’t use the new ShowDialog implementation in this sample though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 58k
  • Answers 59k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Have you tried NDepend? It'll shows you the dependencies and… May 11, 2026 at 8:55 am
  • added an answer Traverse the graph building a set of reversed edges and… May 11, 2026 at 8:55 am
  • added an answer I didn't test this: // config.h #define NUMBER_OF_POTS 2 extern… May 11, 2026 at 8:55 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.