Solved

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

Posted on 2006-07-06
8
238 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now