Hi Guys!,I have a question,I have 2 text boxes and one text box has the name of a song and the other has the name of the artist. I would like to take the value of each text in the text box,for example

song-only girl
artist-rihanna

after i inserted this the program will take what iinserted and put the valuse like this

http://www.youtube.com/results?search_query=only+girl++rihanna&aq=f


thank you!

sorry for asking but where and what should i do with this

This might help. I put the code into a button click event, you can put it anywhere you want to:

private void button1_Click(object sender, EventArgs e)
        {
            //test code:
            textBox1.Text = "only girl";
            textBox2.Text = "rihanna";

            //your code:
            string webAddress = "http://www.youtube.com/results?search_query=";
            TextBox[] tb = new TextBox[] { textBox1, textBox2 };
            for (int i = 0; i < tb.Length; i++)
            {
                string[] songArray = tb[i].Text.Split(' ');
                for (int j = 0; j < songArray.Length; j++)
                {
                    if (i == 1 && j == 0)
                        webAddress += "+";
                    webAddress += songArray[j] + "+";
                }
            }
            webAddress = webAddress.Substring(0, webAddress.Length - 1);
            webAddress += webAddress + "&aq=f";
        }

Hope it helps,
Mitja

With using StringBuilder object like this:

private void button1_Click(object sender, EventArgs e)
        {
            //test code:
            textBox1.Text = "only girl";
            textBox2.Text = "rihanna";
            string webAddress = "http://www.youtube.com/results?search_query=";
           
            StringBuilder sb = new StringBuilder();
            sb.Append(webAddress);

            TextBox[] tb = new TextBox[] { textBox1, textBox2 };
            for (int i = 0; i < tb.Length; i++)
            {
                string[] songArray = tb[i].Text.Split(' ');
                for (int j = 0; j < songArray.Length; j++)
                {
                    if (i == 1 && j == 0)
                        sb.Append("+");
                    sb.Append(songArray[j] + "+");
                }
            }
            sb.Remove(sb.Lenght - 1, 1):            
            sb.Append(&aq=f");
        }

Edited 5 Years Ago by Mitja Bonca: n/a

getting this

Error 1 'System.Text.StringBuilder' does not contain a definition for 'Lenght' and no extension method 'Lenght' accepting a first argument of type 'System.Text.StringBuilder' could be found (are you missing a using directive or an assembly reference?) C:\Users\david\Documents\Visual Studio 2010\Projects\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 54 14 WindowsFormsApplication2

Where, on which line of code this error occures?
Because it works fine for me, thats why Iam askin.

Mitja

Edited 5 Years Ago by Mitja Bonca: n/a

Damn, i think I know wheres the problem. You know, I wrote this code by heart, in notpad, so no debugging, except my eyes :)

this line: sb.Remove(sb.Lenght - 1, 1):

repair to. sb.Remove(sb.Length - 1, 1):

It should do it.
mitja

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;




namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

private void button1_Click(object sender, EventArgs e)
{
string webAddress = "http://www.youtube.com/results?search_query=";
 
StringBuilder sb = new StringBuilder();
sb.Append(webAddress);
 
TextBox[] tb = new TextBox[] { textBox1, textBox2 };
for (int i = 0; i < tb.Length; i++)
{
string[] songArray = tb[i].Text.Split(' ');
for (int j = 0; j < songArray.Length; j++)
{
if (i == 1 && j == 0)
sb.Append("+");
sb.Append(songArray[j] + "+");
}
}
sb.Remove(sb.Length - 1, 1);
sb.Append("&aq=f");
}
    }
}

not working my friend i enter the song and artist and youtube doesn't open with the search results

This is the whole code I wrote:

private void button1_Click(object sender, EventArgs e)
        {
            //test code:
            textBox1.Text = "only girl";
            textBox2.Text = "rihanna";
            string webAddress = "http://www.youtube.com/results?search_query=";
           
            StringBuilder sb = new StringBuilder();
            sb.Append(webAddress);

            TextBox[] tb = new TextBox[] { textBox1, textBox2 };
            for (int i = 0; i < tb.Length; i++)
            {
                string[] songArray = tb[i].Text.Split(' ');
                for (int j = 0; j < songArray.Length; j++)
                {
                    if (i == 1 && j == 0)
                        sb.Append("+");
                    sb.Append(songArray[j] + "+");
                }
            }
            sb.Remove(sb.Length - 1, 1):            
            sb.Append(&aq=f");
        }

well It does not work!
like i said i click search and nothing opens

well It does not work!
like i said i click search and nothing opens

LOL...
1st, you have to put your own button of the form, and create an even (click) for it.
Inside of this even put the my code (but not all, exclude 1st 5 lines - starting with:
"string web...." line and down.

In the line "TextBox[] tb...." you have to rename "textBox1, textBox2" with yout textBoxes name - OK? thats very impotant, otherwise, th code doesnt know from wheer to read - but anyway, the complier would thrown an error in 1st place.
And thats actually it.
It has to work now.
mitja

OH... damn, I only did the code to combine strings...
you have to append this fianal string to the url a browser (you have to open a browser and use that string as an url address).
You got this? Can you do it?

PS: I will show you:

private void button1_Click(object sender, EventArgs e)
        {
           string webAddress = "http://www.youtube.com/results?search_query=";
           
            StringBuilder sb = new StringBuilder();
            sb.Append(webAddress);

            TextBox[] tb = new TextBox[] { textBox1, textBox2 };
            for (int i = 0; i < tb.Length; i++)
            {
                string[] songArray = tb[i].Text.Split(' ');
                for (int j = 0; j < songArray.Length; j++)
                {
                    if (i == 1 && j == 0)
                        sb.Append("+");
                    sb.Append(songArray[j] + "+");
                }
            }
            sb.Remove(sb.Length - 1, 1):            
            sb.Append(&aq=f");

           //open browser with this url:
           System.Diagnostics.Process.Start(sb.ToString());
        }

Edited 5 Years Ago by Mitja Bonca: Added browser opening with url

This article has been dead for over six months. Start a new discussion instead.