Search code examples
textboxradio-buttonclipboard

Get radio button from text in clipboard


I see that in MS Word, we can paste some multiple choice questions and it keeps the radio button. So I know that in the clipboard data, the radio button, checked or unchecked, is still there, but just can't be pasted in Textbox or Rich TextBox, so I think the only simple way (without having to create new kind of textbox) is to handle the radio button when it still in the clipboard data and replace it with some unicode character we like.

My question is: Does MS .Net framework provide the textbox which can keep the radio button? If it doesn't, how to detect the radio button when the text data is in Clipboard.

Thank for reading :)

I'm trying with the function: Clipboard.GetData("HTML Format").ToString() , but I got confuse with the tag, write the html translate is exhausted and maybe not work properly.


Solution

  • I got it, I think that MS word also get the radio button because it detects the html code, so here's how I do it.

    if (Clipboard.ContainsData("HTML Format"))
            {                
                string data = Clipboard.GetData("HTML Format").ToString();
    
                int start, end;
                data = data.Replace(" ", " ");
                //data = data.Replace(Environment.NewLine + "    ", " ");
                string subData;
                while (data.Contains('<') && data.Contains('>'))
                {
                    start = data.IndexOf('<');
                    end = data.IndexOf('>');
                    subData = data.Substring(start, end - start + 1);
                    if (subData.Contains("checked="))
                        data = data.Insert(end+1, "`");
                    data = data.Remove(start, end - start+1);
                }
                foreach(string str in data.Split(Environment.NewLine.ToCharArray()))
                {
                    if (!string.IsNullOrWhiteSpace(str))
                    {                        
                        builder.Append(str);
                        builder.Replace("   ",string.Empty);
                        builder.Replace("  ", string.Empty);
                        builder.Append(Environment.NewLine);
                    }
    
                }
    
                richTextBox1.Text = builder.ToString();
                MessageBox.Show("complete");
                Clipboard.SetText(builder.ToString());
                builder.Clear();
            }
    

    I know this code is not perfect, but it did solve my problem.