Hack Forums
[C#] Combobox variable - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Combobox variable (/showthread.php?tid=5490543)



Combobox variable - DucaRii - Dec 11, 2016

So, my problem is that I want to get the selected item on the combobox as string and use it in another method. Is that possible ? And how?


* - Zilog80 - Dec 11, 2016

(Dec 11, 2016 04:10 AM)DucaRii Wrote:  So, my problem is that I want to get the selected item on the combobox as string and use it in another method. Is that possible ? And how?

Maybe using Text property?

PHP Code

<?php 
if (combo.Text == "Windows")
{
    
// do something when select item is "Windows"
}
else
{
    
// do something when not "Windows" selected
} 

Side note: switch can be also used


* - Vinnilia - Dec 11, 2016

That my friend is very simple.

PHP Code

<?php 
comboBox1
.SelectedItem.ToString(); 



* - DucaRii - Dec 11, 2016

(Dec 11, 2016 09:07 AM)Vinnilia Wrote:  That my friend is very simple.

PHP Code

<?php 
comboBox1
.SelectedItem.ToString(); 

Ayyy thank you bro. Could you also tell me how to send the string "value" to, for example, the main function


* - Zilog80 - Dec 11, 2016

(Dec 11, 2016 11:12 AM)DucaRii Wrote:  Ayyy thank you bro. Could you also tell me how to send the string "value" to, for example, the main function

PHP Code

<?php 
void mainfunction
()
{
    if (
combo.Text == "Windows")
    {
        
// do something when select item is "Windows"
    
}
    else
    {
        
// do something when not "Windows" selected
    
}
} 

You are welcome


* - DucaRii - Dec 11, 2016

(Dec 11, 2016 11:24 AM)Zilog80 Wrote:  
PHP Code

<?php 
void mainfunction
()
{
    if (
combo.Text == "Windows")
    {
        
// do something when select item is "Windows"
    
}
    else
    {
        
// do something when not "Windows" selected
    
}
} 

You are welcome

Code
void mainfunction()
{
    comboBox1.SelectedItem.ToString(frt);
    Console.WriteLine("bla bla {0}",frt);
}

Would this work ?


* - mrkoolaid - Dec 11, 2016

how about instead of an if statement..

Code
if (combo.Text == "Windows")

you should switch it up :)

Code
switch(combo.Text.ToLower())
{
case "windows":
...
...

it scales better :)


* - ๖ۣۜDiViN3 - Dec 11, 2016

The method:
PHP Code

<?php 
public void CallMethod(string comboBoxSelection)
{
      
// insert code
      
var doSomethingWithString = comboBoxSelection;
} 

Calls the method:
PHP Code

<?php 
CallMethod
(comboBox1.SelectedItem.ToString()); 



* - Ninja - Dec 12, 2016

(Dec 11, 2016 11:40 AM)DucaRii Wrote:  
Code
void mainfunction()
{
    comboBox1.SelectedItem.ToString(frt);
    Console.WriteLine("bla bla {0}",frt);
}

Would this work ?

You obviously dont even know the basics of programming so I suggest you learn those first.


* - Vinnilia - Dec 13, 2016

(Dec 11, 2016 11:40 AM)DucaRii Wrote:  
Code
void mainfunction()
{
    comboBox1.SelectedItem.ToString(frt);
    Console.WriteLine("bla bla {0}",frt);
}

Would this work ?

Meh, replace the two function lines with this:

PHP Code

<?php 
string Selected 
= comboBox1.SelectedItem.ToString();
Console.Writeline(Selected); 



* - newmecha - Dec 13, 2016

PHP Code

<?php 
internal 
class OpeartingSystem
{
    
internal int Id { get; set; }
    
internal string Name { get; set; }

    public 
override string ToString()
    {
        return 
Name;
    }
} 
Id should be unique
PHP Code

<?php 
internal void InitializeDataSource
()
        {
            var 
dataSource = new List<OpeartingSystem>
            {
                new 
OpeartingSystem
                
{
                    
Id = 1,
                    
Name = "Windows"
                
},
                new 
OpeartingSystem
                
{
                    
Id = 2,
                    
Name = "Linux"
                
},
                new 
OpeartingSystem
                
{
                    
Id = 3,
                    
Name = "OSX"
                
}
            };
            
ListOperatingSystems.DataSource = dataSource;
        } 

PHP Code

<?php 
private void ListOperatingSystems_SelectedValueChanged(object sender, EventArgs e)
        {
            var 
opeartingSystem = ListOperatingSystems.SelectedValue as OpeartingSystem;
            if (
opeartingSystem == null) return;
            switch (
opeartingSystem.Id)
            {
                case 
1: //Windows
                         
SomeFunction(operatingSystem.Name);
                    break;
                case 
2: //Linux
                         
SomeFunction(operatingSystem.Name);
                    break;
                case 
3: //OSX
                         
SomeFunction(operatingSystem.Name);
                    break;
                default:
//
                    
break;
            }
        } 

I think the code is pretty self explaintory if not let me know and I can explain it.

Lol and I'm high fuh cause I didn't read he want's the string to use in another method.

Can still do that by calling the .Name lol added that in .