Click here to Skip to main content
14,868,570 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am very new to visual studio. I am writing a program which will retrieve the signature of an employee from an API when their name is entered and a "Get user" button is clicked (e.g. if the employee name Jane Doe is entered, the signature "JDOE" will be retrieved from the API string when the name is entered and the button is clicked).

I have been able to read the data successfully, however, I would like to incorporate a combo box so that the combo box suggests names as the user is typing (similar to google).

I have tried connecting the JSON data to the combo box using the combo box task data binding mode, however, while it does retrieve the signature, the combo box does not suggest names. Please see below for my code that I have written so far.

Thank you so much in advance.

What I have tried:

The code for defining variables:

namespace TimeSheet_Try11_Models
{

        
        public class Employeename
        {
            public string signature { get; set; }
            public string firstName { get; set; }
            public string fullName { get; set; }
            public string lastName { get; set; }
        }

        public class Root
        {
            public List<Employeename> Employeename { get; set; }
       
        }
    
    

    }


My code for calling out the JSON

namespace TimeSheets_Try_11.Controllers
{
    class WebAPI
    {
       

        public string Getsignature(string name)
        {

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            var cookies = FullWebBrowserCookie.GetCookieInternal(new Uri(StaticStrings.UrlIora), false);
            WebClient wc = new WebClient();
            wc.Encoding = System.Text.Encoding.UTF8;
            wc.Headers.Add("Cookie:" + cookies);
            wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            wc.UseDefaultCredentials = true;
            string uri = "";

            uri = StaticStrings.UrlIora + name;

            var response = wc.DownloadString(uri);

            var status = JsonConvert.DeserializeObject<List<Employeename>>(response);

            string signame = status.Select(js => js.signature).First();
            return signame;

        }
}


My code for my windows form

namespace TimeSheets_Try_11

{
    public partial class Form1 : Form
    {
        WebAPI WA = new WebAPI();
      

        public Form1()
        {
            InitializeComponent();
            webBrowser1.Url = new Uri(StaticStrings.UrlIora);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {



            comboBox1.DataSource = WA.Getsignature(textBox2.Text);
            comboBox1.DisplayMember = "signature";
            comboBox1.ValueMember = "firstName";

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(AutoCompleteStringCollection combData)
        {
            
            
        }

        /*private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }*/

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form2 p = new Form2();
            p.ShowDialog(); 
        }
    }
}


The API string is:

[{"signature":"JDOE","firstName":"Jane","fullName":"Doe, Jane","lastName":"Doe"}]
Posted
Updated 2-Oct-20 17:53pm
Comments
BillWoodruff 2-Oct-20 23:47pm
   
Is there a security risk associated with providing "clues" ?

You need to set the following properties for autosuggest combobox:
AutoCompleteMode Enum (System.Windows.Forms) | Microsoft Docs[^]
AutoCompleteSource Enum (System.Windows.Forms) | Microsoft Docs[^]

Look here for details:
ComboBox.AutoCompleteSource Property (System.Windows.Forms) | Microsoft Docs[^]
Quote:
Use the AutoCompleteCustomSource, AutoCompleteMode, and AutoCompleteSource properties to create a ComboBox that automatically completes input strings by comparing the prefix being entered to the prefixes of all strings in a maintained source. This is useful for ComboBox controls in which URLs, addresses, file names, or commands will be frequently entered.

The use of the AutoCompleteCustomSource property is optional, but you must set the AutoCompleteSource property to CustomSource in order to use AutoCompleteCustomSource.

You must use the AutoCompleteMode and AutoCompleteSource properties together.
   
"I would like to incorporate a combo box so that the combo box suggests names as the user is typing (similar to google)."

That means you have to pre-load all the possible user JSON strings from the server: imho, that is not secure !
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900