KTV karaoke system digital karaoke function

Hello everyone! I'm new here. This is my first article in CSDN. If there are any bad things, please point them out in time. I will listen to your suggestions and actively correct them!

I recently used the knowledge I learned from Winform to work on a KTV project, which required front-end and back-end management systems. During the process, I found that the back-end system was slightly simpler than the front-end system, so I didn't encounter any problems. Everything went smoothly. After writing the backend, I encountered my first problem when writing the frontend. The frontend has a digital song request function. You need to enter the number of initials of the song name, and then find the corresponding song in the database and finally display it.

 

When I encountered this problem, I first divided it into two steps. The first step was to display the songs in the song display bar on the left when the form was loaded. The second step was to click search and search in the database according to the number in the search box. Check out the songs found and displayed in the left bar.

First, the first step is to display the songs in the database in the song display column on the left

 

I designed it like this. First drag a large Panel, and then put five identical small Panels inside. Each small Panel has a Label control and a Button control. The Label control is used to display the song name. The Button control is used for the subsequent song request function. (Note: The order of dragging each small Panel is from bottom to top, otherwise an error will be reported)

First of all, what I set here is that every five songs are one page, so we must first find out all the data and then display five pieces of data on each page.

It should be noted here that our five songs are one page, so we have to write a judgment. If there are more than five songs, one page will be added.

        //Query the total number of items
        public int QuerySongCount()
        {
            DBHelper.OpenConnection();
            string sql = "select count(*) from song_info";
            SqlCommand command = new SqlCommand(sql, DBHelper.Connection);
            int count = (int)command.ExecuteScalar();
            DBHelper.CloseConnection();
            return count;
            //Number of pages, 5 items per page
            int page = 0;
            //int page = count%5 == 0 ? count/5 : count / 5 + 1;
            if (count % 5 == 0)
            {
                page = count / 5;
            }
            else if (count % 5 != 0)
            {
                page = count / 5 + 1;
            }    
        }
//How to query five songs  
public void QuerySongList(int page)
        {
            DataSet ds = new DataSet(); //Temporary warehouse, data set
            string sql = "select top 5 * from song_info";
            string a =  this.textBox1.Text;
            if (page > 1 && a != null)
            {
                int start = (page - 1) * 5;
                sql += " where " + a + " = song_word_count and song_id not in (select top " + start + " song_id from song_info)";
            }
            else 
            {
                int start = (page - 1) * 5;
                sql += " where song_id not in (select top " + start + " song_id from song_info)";
            }        
SqlDataAdapter adapter = new SqlDataAdapter(sql, DBHelper.Connection);
        //Insert into result set
        adapter.Fill(ds, "song");
        //Get the value of the dataset temporary warehouse
        DataTable dt = ds.Tables["song"];
        int i = 0;
        foreach (DataRow row in dt.Rows)
        {
            string name = row["song_name"].ToString();
            string url = row["song_url"].ToString();
            //One row of panel
            Panel p = this.panel1.Controls[i] as Panel;
            //Find the first small control label in the panel of a row
            Label l = p.Controls[1] as Label;
            l.Text = name; //Song name assignment
            l.Tag = url;//Tag: store data
            i++;
        }
    }

There is a small rule here: the first page checks the 1-5th data in the database, the second page checks the 6-10th data, and the nth page starts from the (n-1)*5th data.

After writing these two methods, you need to adjust the method and write it directly in the form loading event. This completes the first step.

Next, implement the second step function, search the database according to the number in the search box and display it in the song display bar on the left.

The required controls are: Label, TextBox, and Button controls.

Here I chose a clumsier method, which is to write the same click event in each Button button.

 

That's right, every time you click a number button, its corresponding text will be added to the text box.

Then write the filtering code and write it into the click event of the search button.

  private void button17_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();//Data set temporary warehouse
            string sql = "select top 5 * from song_info where song_word_count=" + this.textBox1.Text;
            SqlDataAdapter adapter = new SqlDataAdapter(sql, DBHelper.Connection);
            //Insert into result set
            adapter.Fill(ds, "song_info");
            //Get the value of the dataset temporary warehouse
            DataTable dt = ds.Tables["song_info"];
            int i = 0;
            foreach (DataRow row in dt.Rows)
            {
                string name = row["song_name"].ToString();
                //one line
                Panel p = this.panel1.Controls[i] as Panel;
                //Find the first small control label in the panel of a row
                Label l = p.Controls[1] as Label;
                l.Text = name;
                i++;
            }
        }

The code here is similar to the above, except that a condition is added.

 

Then this function was implemented.

Thank you for watching!

Guess you like

Origin blog.csdn.net/m0_66403070/article/details/126822761