Jump to content

First off I'm using Microsoft Visual c# 2016 studio free for school program.

 

I'm a beginner at programming...well...I basically don't know anything as last year I didn't really attend the classes for reasons and now I'm given a task where I have to list files in a combobox IF they're in the same directory as the application I'm making now. 

 

So I have a form application and I want to do this:

If the user clicks on the combobox the drop down menu will apear empty IF there are no files in the same directory as the application itself BUT if there are files there for example *.txt or *.exe or whatever, make them appear in the dropdown menu. IF they appear there, let the user select one. If the user selected one, let the combobox get disabled (greyed out and still show the selected file name in there - with extension like .txt or whatever) and in a label show a text something like "whatever.txt" selected)

 

I'd be really glad if someone could help as I have no idea how to do this or how to even search to this on google. Thanks in advance.

Link to comment
https://linustechtips.com/topic/610757-quick-question-pls-help/
Share on other sites

Link to post
Share on other sites

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/610757-quick-question-pls-help/#findComment-7898469
Share on other sites

Link to post
Share on other sites

I hope you know how to put controls on the from, and how to handle events.

You will need ComboBox (comboBox1) and Label (label1) on your form. In From load you would need to populate ComboBox.

To populate ComboBox you need create yourself new class, that instance of which you can use as object. The class has to be under main form class and it will look like this:

 

class ComboItem {
    public String name;
    public ComboItem(String name) {
        this.name = name;
    }

    public override string ToString() {
        return this.name;
    }
};

So it has a variable you can store file name, and its constructor takes one argument to set its name.

 

To add new item to combobox you need to do:

comboBox1.Items.Add(new ComboItem("String goes there and will display on ComboBox item list"));

Next thing is to know your exe path, so you can list files of the directory of your executable location.

You can use this one:

AppDomain.CurrentDomain.BaseDirectory

Directory.GetFiles(path) function will return array of full filenames in specified path (eg. "C:\Users\User\file.txt"), if you want to show only the filename with an extension ("file.txt") you would need to use Path.GetFileName(fullfilename) function which takes full filename as argument and returns filename with an extension without path.

 

Edit: To use Directory.GetFiles function you need add "using System.IO" to your imports.

 

Every filename in array that GetFiles function returns would need to be passed to GetFileName function and the result should be added to comboBox1. After this you will end up with combobox with list of files in current directory.

 

Next step would be handle SelectionIndexCanged of comboBox1. So if user changes what is selected in combobox it will fire this event and handles it so combobox gets disabled and label1 will display proper info.

 

To disable combo box you need to use comboBox1.Enabled = false;, then you would want to get what is current selected item of combobox. It will be stored in comboBox1.selectedItem, but you would need to cast it to proper type (ComboItem), like:

ComboItem selectedItem = (ComboItem)comboBox1.selectedItem;

then you can set label1.Text  to "File selected: " + selectedItem.name

 

C# is not language I use daily, so there may be better ways to achieve the above.

Link to comment
https://linustechtips.com/topic/610757-quick-question-pls-help/#findComment-7898570
Share on other sites

Link to post
Share on other sites

@HectorElFather All of your topic names are horrible, Windows XP / OS / Wallpapers....

 

Do something about this, it is not so hard to come up with a GOOD topic name, that will inform the people who can actually help you to take a look.

Instead of tens of people who click on your posts and facepalm when they read the actual question.

Quote or mention me if not feel ignored 

Link to comment
https://linustechtips.com/topic/610757-quick-question-pls-help/#findComment-7902599
Share on other sites

Link to post
Share on other sites

5 hours ago, Cruorzy said:

@HectorElFather All of your topic names are horrible, Windows XP / OS / Wallpapers....

 

Do something about this, it is not so hard to come up with a GOOD topic name, that will inform the people who can actually help you to take a look.

Instead of tens of people who click on your posts and facepalm when they read the actual question.

I don't know what's a "good topic name", pal. 

Link to comment
https://linustechtips.com/topic/610757-quick-question-pls-help/#findComment-7904273
Share on other sites

Link to post
Share on other sites

6 hours ago, HectorElFather said:

I don't know what's a "good topic name", pal. 

Seriously? you see thousands of topics with decent name's and the one you come up with is Windows XP and Quick question need help?

You have a question most of the time, just get a few key words from that question think about it for 1 minute instead of posting it right away like a pleb.

 

LIKE

 

Hey guys i need help choosing a operating system after crashes on windows 7. = Choosing OS after trouble with windows 7

 

Hey guys i need some help with C# because i have this project and i dont get it very well especially the if statements = If statements C#, Dont understand it very well.

Quote or mention me if not feel ignored 

Link to comment
https://linustechtips.com/topic/610757-quick-question-pls-help/#findComment-7905625
Share on other sites

Link to post
Share on other sites

2 hours ago, Cruorzy said:

Seriously? you see thousands of topics with decent name's and the one you come up with is Windows XP and Quick question need help?

You have a question most of the time, just get a few key words from that question think about it for 1 minute instead of posting it right away like a pleb.

 

LIKE

 

Hey guys i need help choosing a operating system after crashes on windows 7. = Choosing OS after trouble with windows 7

 

Hey guys i need some help with C# because i have this project and i dont get it very well especially the if statements = If statements C#, Dont understand it very well.

Is this really the biggest concern of your life? You overreact this way too much, son.

Link to comment
https://linustechtips.com/topic/610757-quick-question-pls-help/#findComment-7905983
Share on other sites

Link to post
Share on other sites

3 minutes ago, HectorElFather said:

Is this really the biggest concern of your life? You overreact this way too much, son.

It's to improve you, it may not have ANYTHING to do with the topic you created also to improve youself.

But yes this can irritate some of the members including me.

 

I am in fear to open up the programming section and see 7 topics PLEASE HELP ME QUICK PLSS,

This will be irritating as hell.

 

Why does topic names exist? just to give a glimpse on what there is inside a topic. 90% of the topics here end up in people needing help, so your logic will be lets remove the topic names and make it a default text saying "Question need help."

Quote or mention me if not feel ignored 

Link to comment
https://linustechtips.com/topic/610757-quick-question-pls-help/#findComment-7905999
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×