textbox dynamic autocomplete - c#

My form has a textBox and i want to add autocomplete while typing.
My autocomplete values are loaded dynamically via a json api.
I applied the "update" function on the "TextChanged" Event of the textBox.
Everytime it is triggered, the autocomplete opens for 0.5 sec and the textBox's value changes to the first autocomplete entry. After that the autocomplete menu disappears.
I cannot choose any suggestions manuelly...
How to fix?
onload Event:
AutoCompleteStringCollection colValues = new AutoCompleteStringCollection();
private void StellenUebersicht_Load(object sender, EventArgs e)
{
TextBox textBoxExample = textBox1;
textBoxExample.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBoxExample.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBoxExample.AutoCompleteCustomSource = colValues;
doAutoCompleteListExample();
}
doAutoCompleteListExample():
private void doAutoCompleteListExample()
{
if (textBox1.Text.Length >= 1)
{
string w = Web.get("MY JSON API URL");
JObject o = JObject.Parse(w);
List<string> ac = new List<string>();
foreach (JObject item in o["items"])
{
string name = item["name"].ToString();
ac.Add(name);
}
colValues.AddRange(ac.ToArray());
}
}

i fixed it.
Solution:
change
textBoxExample.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
to
textBoxExample.AutoCompleteMode = AutoCompleteMode.Suggest;

just delete your If,no need If... do this if you not sure
// if (textBox1.Text.Length >= 1)
// {
string w = Web.get("MY JSON API URL");
JObject o = JObject.Parse(w);
List<string> ac = new List<string>();
foreach (JObject item in o["items"])
{
string name = item["name"].ToString();
ac.Add(name);
}
colValues.AddRange(ac.ToArray());
// }
maybe this help you

The properties like AutoCompleteCustomSource, AutoCompleteMode and AutoCompleteSource to perform a TextBox that automatically completes user input strings by comparing the prefix letters being entered to the prefixes of all strings in a data source.
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
addItems("Add your Data here");
textBox1.AutoCompleteCustomSource = DataCollection;
Full source here.

Related

How to pass value from datagridview to textbox

I am currently working on a system. I have a datagridview with a contextmenu and an edit and delete button on it. I want to pass the value of the selected rows to a textbox when I click the edit on contextmenu.
I have successfully passed the value to the textbox but the only values that show are from the last inputted data to whatever row I click. I don't know how to get the id, can someone please help me fix my problem? :(
Here is my code:
private void BtnEdit_Click(object sender, EventArgs e)
{
frmAddEditStudent frm = new frmAddEditStudent(this);
cn.Open();
cm = new SqlCommand("SELECT s.studentID, s.studentNo, s.Lname, s.Fname, s.MI, s.gender, s.yearLevel, s.section, s.studImage, g.name, g.contactNo, g.address FROM Student s INNER JOIN Guardian g ON g.studentNo = s.studentNo WHERE g.studentNo = s.studentNo AND s.isActive = 'true' AND s.studentID = studentID", cn);
cm.Parameters.AddWithValue("studentID", lblID.Text);
for (int i = 0; i < guna2DataGridView1.Rows.Count; i += 1)
{
frm.btnSave.Enabled = false;
frm.lblTitle.Text = "Edit Student Details";
frm.lblID.Text = guna2DataGridView1.Rows[i].Cells[1].Value.ToString();
frm.txtStudentNo.Text = guna2DataGridView1.Rows[i].Cells[2].Value.ToString();
frm.txtLname.Text = guna2DataGridView1.Rows[i].Cells[3].Value.ToString();
frm.txtFname.Text = guna2DataGridView1.Rows[i].Cells[4].Value.ToString();
frm.txtMI.Text = guna2DataGridView1.Rows[i].Cells[5].Value.ToString();
frm.cboGradeLevel.Text = guna2DataGridView1.Rows[i].Cells[7].Value.ToString();
frm.cboSection.Text = guna2DataGridView1.Rows[i].Cells[8].Value.ToString();
frm.txtGuardianName.Text = guna2DataGridView1.Rows[i].Cells[9].Value.ToString();
frm.txtContactNo.Text = guna2DataGridView1.Rows[i].Cells[10].Value.ToString();
frm.txtAddress.Text = guna2DataGridView1.Rows[i].Cells[11].Value.ToString();
//Load Image
byte[] bytes = (byte[])guna2DataGridView1.Rows[i].Cells[12].Value;
MemoryStream ms = new MemoryStream(bytes);
frm.studImage.Image = Image.FromStream(ms);
//Retrieve gender value to radio button
if (guna2DataGridView1.Rows[i].Cells[6].Value.ToString() == "Male")
{
frm.rbMale.Checked = true;
}
else
{
frm.rbFemale.Checked = true;
}
}
cn.Close();
frm.ShowDialog();
It does not show up the data in the row that I selected, instead it only shows the last row in my database table.
You can get the current row or the selected rows from a datagridview in the following way (I think ID is cell with Index 1):
Console.WriteLine(guna2DataGridView1.CurrentRow.Cells[1].Value.ToString());
foreach (DataGridViewRow loRow in guna2DataGridView1.CurrentRow.SelectedRows)
{
Console.WriteLine(loRow.Cells[1].Value.ToString());
}
But you overwrite the form values in your loop every time.
It seems that your form can only display one row and not a collection.
And what about the command cm??

Filtering of combobox - weird behaviour

I have a combobox in my WinForms application in which you can search for a string and results are filtered accordingly, but it seems to behave very strangely. I am searching for "Coo" and I get a very long list of products that do not contain the string "coo" (http://prntscr.com/pr54rs). However, when I type in "coor" and then press backspace, leaving me with "coo" again, the list of results is different (http://prntscr.com/pr55ef)
I am using the "KeyPress" event handler to search in the combobox. The code looks like this
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
comboBox1.Items.Clear();
listNew.Clear();
var source = new AutoCompleteStringCollection();
foreach (var item in listOnit)
{
if (item.ToLower().Contains(this.comboBox1.Text.ToLower()))
{
listNew.Add(item);
}
}
comboBox1.Items.AddRange(listNew.ToArray());
comboBox1.SelectionStart = this.comboBox1.Text.Length;
Cursor = Cursors.Default;
comboBox1.DroppedDown = true;
}
Am I doing something wrong? The list is populated from a database
string sqlProducts = "SELECT Description FROM stocktake_products WHERE stocktake_id = '" + stocktakeID.stocktake_id + "' AND (PLUProdCode IS NULL OR PLUProdCode = '');";
MySqlCommand cmdProduct = new MySqlCommand(sqlProducts, conn);
cmdProduct.CommandTimeout = 10000;
MySqlDataReader rdrProduct = cmdProduct.ExecuteReader();
AutoCompleteStringCollection myCollectionSales1 = new AutoCompleteStringCollection();
if (rdrProduct.HasRows == true)
{
while (rdrProduct.Read())
{
// myCollectionSales1.Add(rdrProduct[0].ToString());
listOnit.Add(rdrProduct[0].ToString());
}
rdrProduct.Close();
//textBox1.AutoCompleteCustomSource = myCollectionSales1;
comboBox1.Items.AddRange(listOnit.ToArray());
}
I want the combobox to only display results that contain the whole string that has been typed in, not every phrase that has typed letters in any position as it seems like that's what it is doing.
I have tried to include the BeginIvoke method (not sure if done correctly thought as I have never used it before)
this.BeginInvoke((MethodInvoker)delegate
{
comboBox1.Items.Clear();
listNew.Clear();
var source = new AutoCompleteStringCollection();
foreach (var item in listOnit)
{
if (item.ToLower().Contains(this.comboBox1.Text.ToLower()))
{
listNew.Add(item);
}
}
comboBox1.Items.AddRange(listNew.ToArray());
comboBox1.SelectionStart = this.comboBox1.Text.Length;
Cursor = Cursors.Default;
comboBox1.DroppedDown = true;
});
Now when I start typing the first character, the combobox selects to top result right away (http://prntscr.com/pr5f6i). I have to press the first character again and then type the rest of the string to be able to actually search through the list.

ASP.NET - AutoComplete TextBox when choose dropDownList Item

everyone. I have a web service with all cars in company with data:
CarNumber and CarBrand.
I want, where I choose CarNumber from DropDownList, CarBrand autocomplete in textBox.
This is my Web controls declaration;
ddCarNumber = new DropDownList();
ddCarNumber.Items.Add("-- Choose Car Number --");
ddCarNumber.SelectedIndexChanged += new EventHandler(ddCarNumber_SelectedIndexChanged);
GetCars();
this.Controls.Add(ddCarNumber);
lblCarBrand.Text = "Car Brand";
txtCarBrand = new TextBox();
txtCarBrand.MaxLength = 255;
this.Controls.Add(txtCarBrand);
Whit this method i get CarNumbers from Web Service:
private void GetCars()
{
Service1SoapClient client = new Service1SoapClient();
UserDetails details = new UserDetails();
details.userName = "Weber";
details.password = "!Q2w#4r";
DataTable dt = client.GetCars(details);
foreach (DataRow row in dt.Rows)
{
ddCarNumber.Items.Add(row[0].ToString());
}
}
I want where i choose CarNumber from DropDown, TextBox autoComplete with CarBrand.
in Dropdown selectedIndex Change Event
ddCarNumber_SelectedIndexChanged
Write below code
this.Controls.findcontrol("txtCarBrand").Text = ddCarNumber.SelectedItem.Text/Value //Based on your Data DataTextField/DataValueField
Hope this helps.
This is : my ddCarNumber_SelectedIndexChanged Method, This is working, but problem is with Event, not refresh a page... When I push F5 is Okay...
void ddCarNumber_SelectedIndexChanged(object sender, EventArgs e)
{
Service1SoapClient client = new Service1SoapClient();
UserDetails details = new UserDetails();
details.userName = "Weber";
details.password = "!Q2w#4r";
DataTable dt = client.GetCars(details);
foreach (DataRow row in dt.Rows)
{
if (ddCarNumber.SelectedValue == Convert.ToString(row[0]))
{
txtCarBrand.Text = row[1].ToString();
}
}
}

Dynamically creating a CheckBoxList

I am trying to create a dynamic menu with a title and group of checkboxes. So the output would be something like this: (pseudocode-ly)
Title 1
-checkbox1 -checkbox2 -checkbox3
Title 2
-checkbox1 -checkbox2 -checkbox3
I can get the Title back just fine, but my checkboxes are not. (See below)
Care
System.Web.UI.WebControls.CheckBoxList
Corporate & Enterprise Solutions
System.Web.UI.WebControls.CheckBoxList
I realize I am returning a DataSet, I just don't know how to handle it.
BusinessUnit bu = new BusinessUnit();
DataSet businessNames = bu.ListBusinessUnitNames();
ArrayList buNames = new ArrayList();
if (businessNames.Tables.Count > 0 && businessNames.Tables[0].Rows.Count > 0)
{
foreach (DataRow row in businessNames.Tables[0].Rows)
{
buNames.Add(row["BSUN_NAME"].ToString());
}
}
int counter = 1;
foreach (string name in buNames)
{
Label lblName = new Label();
lblName.ID = "unitName_" + counter;
lblName.Text = name;
CheckBoxList chkBoxes = new CheckBoxList();
chkBoxes.ID = name + "Programs_" + counter;
foreach (string item in buNames)
{
DataSet buPrograms = bu.ListBusinessUnitPrograms(item);
foreach (DataRow row in buPrograms.Tables[0].Rows)
{
chkBoxes.DataTextField = row[0].ToString();
chkBoxes.Text = chkBoxes.DataTextField;
}
}
programs.InnerHtml += lblName.Text + chkBoxes;
counter++;
}
Here are the mechanics for doing it in code:
ListItem LI1 = new ListItem("aaa");
ListItem LI2 = new ListItem("bbb");
LI1.Selected = true;
LI2.Selected = false;
chkBoxes.Items.Add(LI1);
chkBoxes.Items.Add(LI2);
(Assuming you're using WebForms [aspx])
In your code example, the statement programs.InnerHtml += lblName.Text + chkBoxes; is appending the value of the default .ToString() implementation of the chkBoxes object. To actually add the checkboxes to the page, you will need some sort of container control (such as a Placeholder) on the page, and append your dynamically created control to the container's Controls collection via phPlaceholder.Controls.Add(chkBoxes)

How to implement autocomplete combobox that can search substring for my windows form app?

Here In image as I press i it is showing all books from i. but i want to search
from sub string like i want all books that contain india anywhere in title
In my program data is fetching from data base.It is already serching from the beginning but now i want to search any substring from it.Code is given below please help me fit it.
sRetVal = GlobalFuncs.GetPersonListWithCondition(ref dtPerson, Convert.ToString(cboPersonCategory.SelectedValue));
if (sRetVal == GlobalFuncs.SUCCESS)
{
var source = new AutoCompleteStringCollection();
source.AddRange(dtPerson.AsEnumerable().Select<System.Data.DataRow, String>(x => x.Field<String>("PersonName")).ToArray());
cboPerson.AutoCompleteCustomSource = source;
cboPerson.DataSource = dtPerson;
cboPerson.AutoCompleteMode = AutoCompleteMode.Suggest;
cboPerson.DisplayMember = "PersonName";
cboPerson.ValueMember = "PersonID";
}
else
{
MessageBox.Show(sRetVal);
return;
}
sRetVal = GlobalFuncs.GetPersonListWithCondition(ref dtPerson, Convert.ToString(cboPersonCategory.SelectedValue));
if (sRetVal == GlobalFuncs.SUCCESS)
{
var source = new AutoCompleteStringCollection();
source.AddRange(dtPerson.AsEnumerable().Select<System.Data.DataRow, String>(x => x.Field<String>("PersonName")).ToArray());
cboPerson.AutoCompleteCustomSource = source;
cboPerson.DataSource = dtPerson;
cboPerson.AutoCompleteMode = AutoCompleteMode.Suggest;
cboPerson.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
cboPerson.DisplayMember = "PersonName";
cboPerson.ValueMember = "PersonID";
}
else
{
MessageBox.Show(sRetVal);
return;
}
Keep coding!

Categories