Link to home
Start Free TrialLog in
Avatar of hmcgeehan
hmcgeehan

asked on

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
Avatar of WinterMuteUK
WinterMuteUK
Flag of United Kingdom of Great Britain and Northern Ireland image

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.
Avatar of hmcgeehan
hmcgeehan

ASKER

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!
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.
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


increasing points
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.
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; } }

    }
ASKER CERTIFIED SOLUTION
Avatar of WinterMuteUK
WinterMuteUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial