Solved

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

Posted on 2006-07-06
8
240 Views
Last Modified: 2010-05-19
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
Comment
Question by:hmcgeehan
  • 4
  • 4
8 Comments
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 17049112
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
 

Author Comment

by:hmcgeehan
ID: 17049413
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
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 17049515
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:hmcgeehan
ID: 17057189
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
 

Author Comment

by:hmcgeehan
ID: 17057190
increasing points
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 17057226
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
 

Author Comment

by:hmcgeehan
ID: 17057322
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
 
LVL 9

Accepted Solution

by:
WinterMuteUK earned 50 total points
ID: 17057522
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run time Error 4 48
Exit the loop 4 43
What is "public object" in C#? 3 47
use of unassigned local variable 'id' 6 40
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question