HelpProvider in checbox list specific item (C#) - c#

I'm making an app which uses a checkbox list. Each item has to have a "description", so I decided to make it with a HelpProvider. But the problem is that when I make a loop that should fill all the checkboxes in checkbox list with a helprovider, Visual tells me that it is an object which cannot be converted into System.Windows.Forms.Control
Any ideas for a workaround?
for (int i = 0; i < CheckedListBox.Items.Count; i++)
{
this.AdditionalInfos.SetShowHelp(CheckedListBox.Items[i], true);
this.AdditionalInfos.SetHelpString(CheckedListBox.Items[i], "example description");
}

So, the reason you get that error is because and SetShowHelp and SetHelpString expect a Control object (a Button, a Label, etc...) but you're giving it a string which can't be handled by the HelpProvider.
I believe that in order to do what you want to do, you'll have to write a custom thing (since the elements of the CheckBoxList aren't Controls). You can either write a "complex" thing that can show descriptions on hover or you can just listen to the SelectedIndexChanged and show a description on a label or something.

Related

List of CheckBox in DataGridViewComboBoxColumn [Windows Form]

I am trying to show a dropdown composed of several CheckBoxes (a list of CheckBox) in a DataGridView. Specifically, the DataGridView provide a DataGridViewComboBoxColumn that allows to have a dropdown with several items. However, if you try to add as a datasource of the column a list of checkboxes you will find out that the solution is not working (the checkboxes are not shown).
In the following link there is a solution on "regular" ComboBox: https://www.codeproject.com/Articles/31105/A-ComboBox-with-a-CheckedListBox-as-a-Dropdown
However, I need it for DataGridViewComboBoxColumn. Someone has an idea of what can be done to reach the goal? Thanks for answering (I link an example of the code below)
for (int i = 0; i < dataGridView.Rows.Count; i++)
{
// Put List of Checkboxes in DataGridViewComboBoxCell for each row of the Grid
((DataGridViewComboBoxCell) dataGridView.Rows[i].Cells[1]).DataSource = new List<CheckBox> { new CheckBox(), new CheckBox(), new CheckBox(), new CheckBox() };
}
I have modified DataGridViewColumns in past (OK, not entirely myself, according to some CodeProject cook book too :-) ), so I have some idea.
However this can't probably work in this case, there's a problem with this idea. You'd not only need to change how the cell looks, but also think how to store the data. How would you assign a separate datasource to each single DataGridViewComboboxCell? If possible, that would be probably quite extensive work, probably a class handling the structure and the events.
If I can offer a solution, you can have another object (i.e. another DataGridViw at the side with CheckBoxColumn and TextColumn which would load on DataGridView1.SelectionChanged), or you can populate it in a modal form on CellClick of that cell and feed back a list of selected items strings on close, etc. I did that in past many times and I don't think there's an easy way around it.

c# - ListBox + type item name to select it?

I am a beginner learning c# and i am playing around with windows forms.
I am using Microsoft.visualbasic.Compatibility.FileListbox and i want to be able to type a name of an item to go to it. i.e select it.
I enabled "KeyPreview" on the form but this doesn't work for me.
can you please assist.
You have to add a bit of code to do that.
Just go to the ListBox's Events panel, double click on the KeyUp event and add your search code in the function that will be automatically generated.
Normally you will be searching over the items in the ListBox's Items collection.
Not sure i understand your question. Do you want to add an element to the list?
If so you can try this:
String name = "MyName";
listBox.Items.Add(name);
I'm assuming you want to select an item in a listBox based on a string you got as an input.
I would do something like this:
String name;
//Code....
for (int i = 0; i < listBox.Items.Count; i++)
{
if(name.Equals(listBox.Items[i].Text)){
listbox.setSelected(i,true);
}
}

Event Handler is not getting called for dynamically created checkboxes

I am kind of new to C# and Asp.Net, so this question might sound repetitive but I am not able to find a solution for this particular problem.
I have two buttons on my HTML page and a single class in .cs file. On one of the button clicks, I create a table programmatically (dynamically).
The table contains some checkboxes which are also created dynamically.
Table creation is one of the last tasks that I perform. Before that, I read several files and extract data from them to create the table. After the table is drawn, the user can select one or more checkboxes.
Now, how on second button click, can I know that which of the checkboxes were checked before the page reload? Currently I have made all these checkboxes member variables of the the only class that I have in the .cs file.
I tried adding checkbox event handler through C# code. But the handler is not getting called when the checkbox is checked. I don't want to set the 'autopostback' property of the checkbox to true since if thats set true, the page reloads after checking one of the checkboxes. User should be able to select multiple checkboxes.
Add your checkboxes dynamically and set a unique name for each checkbox. Checkboxes are only posted back to the server if the checkbox is checked, so you can test to see if it is checked by checking Request.Form to see if the name exists. For example, lets say you named your check boxes chk_[0-9] (i.e. chk_0, chk_1 etc till 9), you could check if they ticked by doing:
for(int i=0; i < 10; i++)
{
string chk_name = "chk_" + i.ToString();
if (Request.Form[chk_name] != null)
{
//checkbox is checked
}
else
{
//checkbox is not checked
}
}

Creating dynamically named textBoxes

I need to create a new textbox every time I click a button. I see how it will work once, but not multiple times.
TextBox NewTB = new TextBox();
NewTB...// set textbox properties
this.Controls.Add(NewTB);
I need NewTB to be different everytime I click the button (NewTB1, NewTB2, etc), I tried a List<> that contained the names I wanted, then assigned the name as the List<> member, but that didn't work. Can I use some type of List<> that contains TextBoxes? I'm not really sure how to implement that.
The name "NewTB" in your example is just a variable name. It is not assigned to that textbox in any way. The "list" of textboxes resides within the control structure. In other words, when you say this.Controls.Add(NewTB), you are adding that textbox to the list of controls.
If the code that you show us happens as part of a click handler, it will be run each time the button is clicked, and therefore a new textbox will be added each time. this.Controls is essentially the List that keeps track of the controls (including textboxes) on your form.
If you want to maintain the ability to reference the textbox, you will need to you a list (which will give you the ability to create an indefinite number of them), and then add a reference to a new textbox the list every time you add the same reference to the control. Every time you click the button, it should set your textbox to a new instance of textbox, and then add the reference of that instance to both the control and the list.
EXAMPLE:
To make the TextBoxen:
boolean iNeedMoreTextBoxes = true; //A simple boolean to be changed when you want
//want to stop adding TextBoxes
List<TextBox> textBoxes = new List<TextBox>(); //This makes a list of TextBoxes
TextBox tb = new TextBox(); //tb is nothing more than a pointer to the new TextBox
while(iNeedMoreTextBoxes){
textBoxes.add(tb); //The pointer is added to the List
control.add(tb); //The same pointer is added to the control
tb = new TextBox(); //Make another TextBox
iNeedMoreTextBoxes = checkToSeeIfINeedMoreTextBoxes();
}
Ok, so what that did was make a bunch of objects that and give a reference of each of those objects to the List and the control. Now, whenever you use the List's reference to change the object, the control will change as well, because it has a reference to the same object. Thus,
To retrieve the TextBoxen:
for(int i = 0; i < testBoxes.length(); i++){
textBoxes.get(i).makeChanges(); //makeChanges isn't really a function, just an example
}
Now, if you need to be able to make a change to one specific TextBox, the TextBox class MUST have some identifying field, and then you would likely use this:
int id = getTheIDofTheTextBoxINeed(); //this can be any identifier that you can check
for(int i = 0; i < testBoxes.length(); i++){
if(textBoxes.get(i).getID() == id) //Yay we found the right one, now make the changes
textBoxes.get(i).makeChanges();
}
And that's more or less how you do it.
EDIT: This is java code, because I am not very familiar with C#. However, I do know that underlying principles apply in both languages, just some of the syntax is different.

Problem with ComboBox autocomplete when adding values dynamically

In WinForms application, I have a combobox which I am trying to populate with values based on user input. For example if the user types m it should show him all the values that starts with the letter m, but I dont want to add all the values in the beginning because there are a lot of values.
To achieve this, I created an event textchanged and when a user inputs for example the letter m my program goes to my database and adds all the values with the letter m to the combobox.
The problem that I think that the combobox first sees if it should autocomplete (suggest) values and only after that it adds the values.
How can I make it add the values first or make the combobox check again if it should suggest values?
Here is my code:
private void comboBox1_TextChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
for (int i = 0; i < MilkProducts.Length; i++)
{
if (MilkProducts[i].StartsWith(comboBox1.Text))
{
comboBox1.Items.Add(MilkProducts[i]);
}
}
}
It seems you may have to use Win32 API (using PInvoke) here by sending appropriate message to the Combo box to show the search result "after" the event handling is done
Please refer to the below URL and you may find the what you are looking for:
http://msdn.microsoft.com/en-us/library/bb775792(VS.85).aspx
i think problem is you are clearing all the items in ComboBox at comboBox1.Items.Clear() and then accessing its contents at comboBox1.Text may be you should try doing it differently. or clear it at the end.

Categories