What's new
  • Visit Rebornbuddy
  • Visit Panda Profiles
  • Visit LLamamMagic
  • Visit Resources
  • Visit Downloads
  • Visit Portal

enum Value to string and bind it to Combobox

  • Thread starter Thread starter weischbier
  • Start date Start date
W

weischbier

Guest
So I wanted to convert an enum Value to a String.
Then bind this enum to a combobox.

Binding it to a combobox is simple.

Code:
[LEFT]<code class="csharp plain" style="background-image: none !important; background-attachment: initial !important; background-origin: initial !important; background-clip: initial !important; background-color: initial !important; border-top-width: 0px !important; border-right-width: 0px !important; border-bottom-width: 0px !important; border-left-width: 0px !important; border-style: initial; border-color: initial; border-image: initial !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 0px !important; padding-top: 0px !important; padding-right: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; vertical-align: baseline !important; border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; border-style: initial !important; border-color: initial !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; outline-width: 0px !important; outline-style: initial !important; outline-color: initial !important; overflow-x: visible !important; overflow-y: visible !important; position: static !important; right: auto !important; top: auto !important; width: auto !important; box-sizing: content-box !important; font-size: 13px; direction: ltr !important; display: inline !important; ">public enum Behaviour
        {
            DestroyMyComputer,
            LowSpecComputer
        }

comboBox1.Datasource = Enum.GetValues(typeof(Behaviour));</code>[/LEFT]

I know how to read that from a combobox

Code:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch ((Behaviour)comboBox1.SelectedItem)
            {
                case
                     Behaviour.DestroyMyComputer:
                    //Bla bla here
                    break;
                case
                     Behaviour.LowSpecComputer:
                    //Bla bla here
                    break;
                default:
                    break;
            }
        }

But now I want to contain '
DestroyMyComputer
' for example "Destroy my Computer" as a string.
Does anybody know hot to do so?
Google gave me nothing ... sad day!

greetz

Weischbier
 
edit.. nm
 
Last edited:
Yeah but its still one word. "DestroyMyComputer" shall be "Destroy My Computer".

You need to write a method to convert a string in camel case to a more friendly name. Here's something off the top of my head. (untested, adds a space before every capital letter)

Code:
string ConvertCamelToFriendly(string theString)
{
    for (int i = 0; i < theString.Length; i++)
    {
        if (i != 0 && char.IsUpper(theString[i]))
        {
            string prefix = theString.Substring(0, i);
            string postfix = theString.Substring(i, theString.Length - i);

            theString = prefix + " " + postfix;
            i++;
        }
    }

    return theString;
}

To use this you would do:

Code:
ConvertCamelCaseToFriendly(Behaviours.DestroyMyComputer.ToString());

There's probably a much better way of doing this but string manipulation in C# is not my forte unfortunately.
 
You need to write a method to convert a string in camel case to a more friendly name. Here's something off the top of my head. (untested, adds a space before every capital letter)

Code:
string ConvertCamelToFriendly(string theString)
{
    for (int i = 0; i < theString.Length; i++)
    {
        if (i != 0 && char.IsUpper(theString[i]))
        {
            string prefix = theString.Substring(0, i);
            string postfix = theString.Substring(i, theString.Length - i);

            theString = prefix + " " + postfix;
            i++;
        }
    }

    return theString;
}

To use this you would do:

Code:
ConvertCamelCaseToFriendly(Behaviours.DestroyMyComputer.ToString());

There's probably a much better way of doing this but string manipulation in C# is not my forte unfortunately.

Thanks for your effort!

I made it like this

Code:
public void FillComboBox()
        {
            comboBox1.Items.Add("Default (max TPS is set to 30)");
            comboBox1.Items.Add("Destroy my Computer (max TPS increased to 120)");
            comboBox1.Items.Add("I drive a low specced comp (max TPS is set to 18)");
        }


        private void ComboBox1SelectedIndexChanged1(object sender, EventArgs e)
        {
            if (Equals(comboBox1.SelectedItem, "Default (max TPS is set to 30)"))
            {
                trackBar1.Maximum = 30;
                TrackBar1Scroll(sender, e);
                CombatBotSettings.Instance._behaviour = comboBox1.SelectedItem.ToString();
            }
            else if (Equals(comboBox1.SelectedItem, "Destroy my Computer (max TPS increased to 120)"))
            {
                trackBar1.Maximum = Equals(comboBox1.SelectedItem, "Destroy my Computer (max TPS increased to 120)") ? 120 : 30;
                TrackBar1Scroll(sender, e);
                CombatBotSettings.Instance._behaviour = comboBox1.SelectedItem.ToString();
            }
            else if (Equals(comboBox1.SelectedItem, "I drive a low specced comp (max TPS decreased to 18)"))
            {
                trackBar1.Maximum = Equals(comboBox1.SelectedItem, "I drive a low specced comp (max TPS decreased to 18)") ? 18 : 30;
                TrackBar1Scroll(sender, e);
                CombatBotSettings.Instance._behaviour = comboBox1.SelectedItem.ToString();
            }
        }

(Yeah I know 120TPS is stupid but for testing its absolute good)

greetz

Weischbier
 
Thanks for your effort!

I made it like this

Code:
public void FillComboBox()
        {
            comboBox1.Items.Add("Default (max TPS is set to 30)");
            comboBox1.Items.Add("Destroy my Computer (max TPS increased to 120)");
            comboBox1.Items.Add("I drive a low specced comp (max TPS is set to 18)");
        }


        private void ComboBox1SelectedIndexChanged1(object sender, EventArgs e)
        {
            if (Equals(comboBox1.SelectedItem, "Default (max TPS is set to 30)"))
            {
                trackBar1.Maximum = 30;
                TrackBar1Scroll(sender, e);
                CombatBotSettings.Instance._behaviour = comboBox1.SelectedItem.ToString();
            }
            else if (Equals(comboBox1.SelectedItem, "Destroy my Computer (max TPS increased to 120)"))
            {
                trackBar1.Maximum = Equals(comboBox1.SelectedItem, "Destroy my Computer (max TPS increased to 120)") ? 120 : 30;
                TrackBar1Scroll(sender, e);
                CombatBotSettings.Instance._behaviour = comboBox1.SelectedItem.ToString();
            }
            else if (Equals(comboBox1.SelectedItem, "I drive a low specced comp (max TPS decreased to 18)"))
            {
                trackBar1.Maximum = Equals(comboBox1.SelectedItem, "I drive a low specced comp (max TPS decreased to 18)") ? 18 : 30;
                TrackBar1Scroll(sender, e);
                CombatBotSettings.Instance._behaviour = comboBox1.SelectedItem.ToString();
            }
        }

(Yeah I know 120TPS is stupid but for testing its absolute good)

greetz

Weischbier
do not release this for public use, the last thing we need is people who dont know what that does messing with that value.
 
This could help you...

typetest.cs

So i you set

comboBox1.Datasource = Enum.GetValues(typeof(Bane));

Bane.Agony would display as "Agony". So just make one for your cases and have them convert the strings to whatever you want.

I have a huge St. Patrick's day hangover and looking at your solution makes me want to ralph on my comp screen. Its just soo much code.
 
Last edited:
Back
Top