Forum guidelines
AuthorQuestion
ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 12 Dec 2012
I have a form in visual studio 2008. one button and two text box1 and 2 and 1 combobox

The user selects a horsename from combo which updates textbox1. 
i have the following array which is in php and will like to convert to fit visual.
$ar = array('A' => 1,
'B' => 2,
'C' => 3,
'D' => 4,
'E' => 5);

when the user selects a horsename "DAB" WHICH updates textbox1. i want the form to work out that "DAB" is "4+1+2=7" and show in textbox3 as 7

please help as i have downloaded vs.php and phalenger3 but i am still stuck as i cannot activate it with my hkey.





AuthorReply
Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 12 Dec 2012  
I've no idea how you'd do this in phalanger but in C# Windows Forms you could do it as follows.

Note that since array indexing starts at 0 in C# you have to add 1 to get the value of the corresponding character:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        char[] ar = {'A', 'B', 'C', 'D', 'E'};

        public Form1()
        {
            InitializeComponent();
        }

        // add this handler by double clicking on form in VS designer
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] items = { "DAB", "BED", "CAD" }; // or whatever
            comboBox1.DataSource = items;
        }

        // add this handler by double clicking on combobox in VS designer
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex > -1)
            {
                textBox1.Text = comboBox1.SelectedItem.ToString();
            }
        }

        // add this handler by double clicking on textbox1 in VS designer
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string text = textBox1.Text;

            if (text != "")
            {
                int total = 0;
                for (int i = 0; i < text.Length; i++)
                {
                    int index = Array.IndexOf(ar, text[i]); // gets index of letter in array
                    if (index > -1) total += index + 1;
                }
                textBox2.Text = total.ToString();
            }
        }
    }
}


Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 12 Dec 2012  
wow. Thank you for such a quick reply.
i have tried this and it works. There is two problems.
first i gave an example of array. the true array has numbers recurring 
A=1; B=2;C=3;D=4;E=1;F=2;G=4;H=4ETC ALL the way to z.

second : my combobox has a list of 150 names to choose from. it changes every 2nd day.

can i not call it from sql database. otherwise i have to manually input. 

honestly i dont see the need for textbox1 as we can use the combox and result to textbox2.

thank you if you can help with that

Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 12 Dec 2012  
If the numbers of the letters are not consecutive, then I'd use a Dictionary rather than an array.

Yes, you can load the combobox from a SQL database rather than adding the items manually.

As you say there doesn't seem any need for the first textbox.

The changes to the code will be as follows:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Dictionary<char, int> dict = new Dictionary<char, int>();

        public Form1()
        {
            InitializeComponent();
        }

        // add this handler by double clicking on form in VS designer
        private void Form1_Load(object sender, EventArgs e)
        {
            dict.Add('A', 1);
            dict.Add('B', 2);
            dict.Add('C', 3);
            dict.Add('D', 4);
            dict.Add('E', 1);
            dict.Add('F', 2);
            dict.Add('G', 4);
            dict.Add('H', 4); 
            // etc to 'Z' 

            string[] items = { "DAB", "HEAD", "FAD", "GAG" }; // or whatever
            comboBox1.DataSource = items;
        }

        // add this handler by double clicking on combobox in VS designer
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex > -1)
            {
                string text = comboBox1.SelectedItem.ToString();             
                int total = 0;
                for (int i = 0; i < text.Length; i++)
                {
                   total += dict[text[i]];
                }
                textBox2.Text = total.ToString();
            }
        }
      
    }
}

If you check this thread, there's some code which shows how you can load the combobox from a SQL Server database:

http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/4c346770-0319-4d74-b3a9-879152398579

Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 13 Dec 2012  
the dict works well. If there is a word like "arabian princess" it throws a exception error
he given key was not present in the dictionary. Is this because of the space between the two words. Otherwise it works fine with just single words.

i tried the trim(). it does not work. if i can probally join the two words in a hidden box it might work.

please help with that

Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 13 Dec 2012  
The best thing to do with spaces (and non-letters generally) seems to be to ignore them.

I've made a change to deal with that and also to make sure that there no problems with lower case letters as well:

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex > -1)
            {
                string text = comboBox1.SelectedItem.ToString().ToUpper();             
                int total = 0;
                for (int i = 0; i < text.Length; i++)
                {
                   if (dict.ContainsKey(text[i]))
                   {
                      total += dict[text[i]];
                   }
                }
                textBox2.Text = total.ToString();
            }
        }

Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 13 Dec 2012  
Hi there . I am not getting the error now but it is not giving the total in textbox2. does this remain the same below.



Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 13 Dec 2012  
Have you changed the Dictionary's keys to lower case (i.e. 'a', 'b' etc) by any chance?

If you have, then ToUpper() should be ToLower().

Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 13 Dec 2012  
yes i did. it now works perfectly. i want to know if i want it to automatically work out the total and display as a grid is this possible.

At the moment this is fine where the member has to select to get a total. i want it to take the form load and do a grid.

If u dont mind me taking advantage.

Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 13 Dec 2012  
Do you mean that you want the horses' names to be loaded into one column of a DataGridView (rather than a ComboBox) and then the numeric values to be automatically calculated and used to populate a second column in the DGV?
Vulpes
  • 0
  • 0
accepted
Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 14 Dec 2012   Accepted Answer
Your SELECT query should bring in the PREDNUM column from Table B even though it's empty to start with. 

Suppose 'dtB' is the DataTable representing Table B in your database. Then the following code will populate the PREDNUM column before you bind the DataTable to the DataGridView (code is off top of my head and so may not be quite right):

foreach(DataRow dr in dtB.Rows)
{
   string name = dr["HORSENAME"].ToString();
   int total = 0;
   foreach(char c in name)
   {
      total += dict[c]; // assuming Dictionary already loaded from Table A
   }
   dr["PREDNUM"] = total; // if it's an int column - use total.Tostring() if it's varchar 
}

Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 15 Dec 2012  
Sorry, I'd forgotten that some of the characters (i.e. spaces) won't be in the Dictionary.

Try it like this:

Databases db = new Databases(); 
DataTable dtResults = db.PopulateDataTableSP( "GETHNUM", "Racedate", a); 

if (dtResults != null) 
    foreach (DataRow dr in dtResults.Rows) 
    { 
      string name = dr["horse"].ToString().ToLower(); 
      int total = 0; 
      foreach (char c in name) 
      { 
        if (dict.ContainsKey(c))
        {
           total += dict[c]; // assuming Dictionary already loaded from Table A
        } 
      } 
      dr["HNUM"] = total.ToString(); 
    }

    dg1.DataSource = dtResults;
}



Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 16 Dec 2012  
Thank you so much for your help. i managed to do it the way you said. i put the dgv in the same form. it updates automatically. How do i now get the database to update "HNUM".
}
    dr["HNUM"] = total.ToString();
}
   dg1 DataSource = dtResults;

im sure the update comes somewhere here.

Re: ARRAYS THAT WILL CONCATTENATE TEXT AND NUMBERS
Posted on: 16 Dec 2012  
If you've used a DataAdapter ('adapter' say) to fill the DataTable (or associated DataSet) in the first place, then you need to call its Update method to write the values for the HNUM column back to the database. Something like:

adapter.Update(dtB);

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.