Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Selecting an option (like selecting a file from hard disk)

Hello,
I'd like to have a button and textbox on my C# application.
When I click the button it shows a window (much like the one where you can select a file off a hard disk)
But this window will be populated by some string options.
So when I select

'text 1'

It will populate the textbox with 'text 1'

(for example if the options are 'text 1', 'text 2', 'text 3'

Tks
H
0
hmcgeehan
Asked:
hmcgeehan
  • 4
  • 4
1 Solution
 
WinterMuteUKCommented:
Hi hmcgeehan,

So what you want to do is:
  1. Click button on Form1
  2. Select an option from a second form (Form2)
  3. Close Form2 and set a text box on Form1 to be the option selected on Form2.
?

How is this option list populated?
Does it need to look like an Explorer window?

Thanks

Wint.
0
 
hmcgeehanAuthor Commented:
Hey Wint,

The option list is a set of text values from a database but don't worry about that bit.
Maybe just assume its an ArrayList.

It should look like the rest of the application
=)

Thanks!
0
 
WinterMuteUKCommented:
All you're really looking to do then is create a little form with an ok/cancel button, and the 'options' selector.

When you press ok, set a var on the form to be the option selected, and provide a property to get it.

private string mSelected;
public string Selected { get { return mSelected; } }

make sure the ok and cancel buttons have the appropriate 'DialogResults' and use like:

  Form2 f2 = new Form2();
  if(f2.ShowDialog() == DialogResult.OK)
  {
     textBox1.Text = f2.Selected;
  }

Wint.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hmcgeehanAuthor Commented:
Ok great

I created a Form2 with a ListBox and a button on it.

And it opens when I click on my button on Form1

private void button1_Click_1(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            if (f2.ShowDialog() == DialogResult.OK)
            {
                //textBox1.Text = f2.Selected;
            }
        }


But I tried to modify Form2 and it gives me an error

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProcessMonitor
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private string mSelected;
        public string Selected { get { return mSelected; } }

    }
}

Warning      1      Field 'ProcessMonitor.Form2.mSelected' is never assigned to, and will always have its default value null      C:\code\c#\ProcessMonitor\ProcessMonitor\Form2.cs      18      24      ProcessMonitor


0
 
hmcgeehanAuthor Commented:
increasing points
0
 
WinterMuteUKCommented:
You'll need to put something on Form2 to assign 'mSelected'

Two ways I can think of:

1. += to the 'SelectedIndexChanged' event of the ListBox and do something like:

    private void listBox1_SelectedIndexChanged( object sender, EventArgs e )
    {
        mSelected = listBox1.SelectedItem.ToString();
    }

2. On closing the form (so in the code for your 'OK' button)

    {
        mSelected = listBox1.SelectedItem.ToString();
    }

Wint.
0
 
hmcgeehanAuthor Commented:
Thanks Wint

One thing.

Should Form2 have an OK button on it?

When I created Form2 I just put my own button in.

I now do this but clicking on the button doesn't 'ok' it. i.e. the form doesnt close.

Thanks!




public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
           
            ArrayList computers = NetworkTools.getComputers();
           
            foreach (string computer in computers)
            {
                listBox1.Items.Add(computer);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            mSelected = listBox1.SelectedItem.ToString();

        }

        private string mSelected;
        public string Selected { get { return mSelected; } }

    }
0
 
WinterMuteUKCommented:
You should set the button to have a DialogResult of OK (it's one of the properties of a button).
Or you can add:

DialogResult = DialogResult.OK;

in the button1_Click method:

        private void button1_Click(object sender, EventArgs e)
        {
            mSelected = listBox1.SelectedItem.ToString();
            DialogResult = DialogResult.OK;
        }

Wint.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now