How to add a text item to a combobox in C# - c#

I am new to C# from Delphi. In Delphi, TCombobox.Add takes a single, string parameter. Thus is it very simple to add a string item to a combobox.
C# confused me because Combobox.Items.Add takes a single, object parameter. There are some circumstances in which I cannot add a string var to the list of items. (I haven't identified a pattern yet; it might be if it's a property).
Googling was fruitless until...

I had the same problem in an WPF application, using VB.Net I got Component.Items.Add("String") to work, my guess is the string will automatically be cast to an suitable object, try to explicitly cast string in your case(not sure to what).
You can also try to assign your string to be the caption of a label and add that label to the control.

I have asked several times for this question to be deleted because it is of no value to anyone. I wrote it when I was confused and bewildered and didn't know what I was doing.

... I found http://www.codeproject.com/Questions/416859/Csharp-Filling-ComboBox-with-an-SQL-table-column, which said
comboBox1.Items.Add((string) problematicVar); //problematicVar is a string
So I have to cast to string.
I've added this self-answered question because I wish it had existed half an hour.

Related

Filter elements in Revit and set parameter

Hello everybody,
around two or three month ago I started to learn Dynamo for Revit... finally :)
After learning and testing a lot, I got a few own scripts working. Then I learned Python, because I couldn't create the next script only with Dynamo-Nodes.
Then I thought "Let's see how difficult it is to get something done as a PlugIn".
I watched some Videos and read a lot of stuff.
Finally I got the Revit-AddIn-Wizard installed and made my first small Test-PlugIn.
Great...
Now I have a few problems which I do not understand... so I thought I will try my luck here... because I got so much information and help, reading through this site.
My goal was/is the following: (I tell you what I have now)
A form with a few buttons, comboboxes and a DataGridView.
I can load an Excelfile, click on "Show" to show it in the DataGridView.
The header of each row will be automatically put into 3 comboboxes.
In the first combobox you select the first search-parameter, in the second you CAN select another search-parameter and in the third combobox you select the parameter you want to set.
I have a checkbox to switch from type- to instance-parameter for the search- and the set-operation.
There is also a button which shows another small form with a list of categories (I won't search for ALL, only nearly all modelcategories).
PlugIn
I took me a lot of "watching Videos, reading through the internet, testing, testing and testing".
Thanks to this site here and a few others... I managed to get this whole PlugIn nearly 100% working.
But now I have a few strange issues and I have absolutely no clue on how to fix them or if it is possible. And I really hope that someone can help me.
First... I just tell you my problems and perhaps someone can say "this really IS an issue!" or that it is possible to get it done. Then I would post some code.
So... what do I do?!
1. I have a FilteredElementCollector which filters ALL elements.
2. Depending on my "Type/Instance-Checkboxes" I do .WhereElementIsElementType OR .WhereElementIsNotElementType.
3. Then it passes a MultiCategoryFilter to get the big list down to only the modelcategories.
4. Next, the collection passes one of ten different "methods" depending on all settings. There I filter this collection depending on the searchlists-comboboxes. When the combobox says "Familie" or "Typ" then it filters for ".BuiltInParameter.SymbolFamilyName" or ".Name" otherwise it just uses ".LookupParameter".
After that I have a collection with only the elements of selected categories which contains the values from the Excellist.
5. Depending on what my search- and set-settings are (e.g. search for type and set instance) I have to get the instances from the collected types or the other way around.
6. Then I pass it down to another method where I finally set the parameter.
So... Excelheader goes into comboboxes, depending on what you select in there it creates lists with the values of the selected rows.
I hope you all understand.
Now... where are my problems?
When I search for type-familynames or instance-parameter and set a typeparameter it works for ALL categories without any error.
1. When I try to set an instanceparameter (doesn't matter what my search-setting are) it works for all "normal" families but not for the systemfamilies (e.g. walls, floors, pipes etc.). No error, just nothing happens WHY? It seems that I cannot set an instance-parameter for system-families.
2. Roofs, Stairs, CurtainPanels and GenericModel make problems when I search for a typeparameter Error is something like "The object reference was not set to an object instance". Only with these 4 categories and it doesn't matter what I want to set... but when I search for family-/typeNAME or Instance-Parameter, then I can set type or instance and it works (except instance for sysfam).
3. When I try to search AND set an instance-parameter it works for ALL categories EXCEPT if one wall does not contain a search value... it really is enough that ONE wall does not have a search-param-value that everything will be cancelled.
I have a few other small problems... but I hope someone can help me with these problems... I would be extremely thankfull
greetings and have a nice day or night :)
Philipp
Tl; dr.
The three problems you describe sound like your own. I have no heard anybody else runAsk three separate questions and provide three separate minimal code snippets describing how they arise,. into those. I suggest that you create three separate independent minimal reproducible cases to demonstrate all three issues. Chances are, when you simplify and minimalise your code, the problem will go away. If it does not, it might just possibly be in a small and manageable enough state for other people to help you take a look at it. Given the long-winded description above, nobody in the world can help you.
Thank you for your answer Jeremy,
as I said, as a first start it is ok for me if you don't say "With theses categories, there are indeed some issues!"
I think I've managed to create 3 small examples of my problems.
For each problem I made a zip-file containing the complete visual-studio folder, a small exampleproject and a readme.txt with (I hope) enough information to understand everything in detail.
Problem1
Problem3
You only need to compile them or copy the .addin and .ddl files into the Revit AddIn folder. Then you get the new ribbons.
Short problem summary = I get problems when searching for parametervalues and setting values to another parameter.
Edit: I just solved the 2. problem when searching for familynames and setting system-families-parameter.
I used:
ElementClassFilter ecf = new ElementClassFilter(typeof(FamilyInstance));
FilteredElementColletor colle2 = new FilteredElementCollector(doc);
colle2.WherePasses(ecf);
I simply deleted the ClassFilter and do it now like in the other cases where I need instances.
FilteredElementCollector colle2 = new FilteredElementCollector(doc);
colle2.WhereElementIsNotElementType();
The 1. and 3. problem still exist :/
I would be thankful for any help someone can provide :)

Check List elements in C# and Python

I have worked on Python and I am new in C#. I am trying to get familier with syntaxes and concepts. I have a problem with list.
For example, I will take a word from user. I will add it to a list and I will check if it is entered before because user can't give the same word second time. If it is not entered before add it to the list. If is is already in the list pass it.
In python:
check_list = list()
if word not in check_list:
check_list.append(word)
else:
pass
How can I do that in C# ???
List<string> list;
if(!list.Contains(word)){
list.Add(word)
}
I was trying to follow your example as closely as possible so I didn't add confusion, however if you new-up the list every time !list.Contains(word) will always be true.

Allowing null in int text box with non nullable int

So there are very similar questions about this, but none seem to solve what I'm trying to do, and maybe it's not possible. I have a form with a number of text boxes bound to int properties in my model. We allow the user to clear the field, and it should remain cleared. CUrrently if they do this they get a validation error that the it can't convert "". I understand why this is, however, due to the design I can't use int?, which I know would solve my problem. Is this possible to do, without having to define a string property to bind to? I'm trying not to duplicate all my int properties as string properties, but maybe that's the road I need to take.
What you would like to do when the string is empty?
I mean do you want to have some int value in prop if textbox is empty? If yes, simply create a converter that converts null value to that value and assign to Binding
You need to be able to represent an empty int.
This is a dirty hack but if you chose a number say -999 that will never be valid you could use a custom converter to convert and convert back between "" and -999.
It will probably achieve what you want but I would not recommend it.

Why does the Find() method return an array?

I want to locate a TextBox named "textBoxQH_N" where "_N" is a number from 1..96.
So, I've got this code:
String sTextBoxToFind = String.Format("textBoxQH{0}", QuarterHour);
TextBox tb = (TextBox)this.Controls.Find(sTextBoxToFind, true);
...but it gave me, "Cannot convert type 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.TextBox'"
So I changed the second line to grab just the first returned val:
TextBox tb = (TextBox)this.Controls.Find(sTextBoxToFind, true)[0];
Which seems to work, but shouldn't a Control's Name property be unique to its owner? IOW, Find() should only return 0..1 controls, right?
Find, with the second property set to true, is recursive. 'Name' is unique to that parent, but you're searching through lots of different parents. 'Name' isn't globally unique.
As suggested by Justin in another answer, First or FirstOrDefault is probably better than using [0] on the array. It does a better job of conveying your intentions to future readers.
The Find method will find any matches, so even though it is only one match in your case, it could be numerous in other cases. You could probably use LINQ First here if you wanted something more semantically meaningful?
The MSDN on this is pretty clear on this method
It returns an array of Controls.
try it like:
Control[] tb = this.Controls.Find("textBox1", true); //change the name of the control
or:
TextBox[] tbs = (TextBox[])this.Controls.Find("tb1", true);
As you can see, you must CAST to the correct type of object (in your case to array of TextBoxes).
Hope it helps,
bye
Form API design point of view 0 and 1 choices are better to be returned as IEnumerable/collection/array to avoid need for null checks.
As pointed by other answers, Name of control does not have to be globally unique and Find actually may return more than 1 item.
Link to MSDN - Controls.Find

Adding blank space as one of my variable

I want to write some data about the person's in a file. And while adding those data i want to add blank space if a particular data for that person is not available.
ex: I want to have " " in place of SSN if person doesn't have one.
Thanks,
Uhm... sorry for this kind of answer, but I really recommend you read up on file I/O. As Brad said, it's kinda unclear what your problem is.
I, for one, think your problem is not actually a problem: if you know how to print to the file all the other information, you surely must know how to add some blanks.
Again, sorry I'm not giving a straight answer, but trust me: I could tell you ten different ways of handling record-keeping when certain data is undefined for a record, and you would still learn much more (and much more solidly) reading a tutorial or two.
If all you're doing is writing out variables from an object, you could just use the String.IsNullOrEmpty method before you return the value you want to write.
Something like
return !String.IsNullOrEmpty(SSN) ? SSN : " ";

Categories