Solved

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

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

 

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB: Convert 2 dates to specific format 24 55
Adding where clause to entity statement c# 1 47
Need help with a query 14 37
Stop Git from being my repository 1 20
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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