|
It depends on what type your List is. Do you mean that you want to add a blank item to something like a combobox? If so, and you are using Win Forms or ASP.NET, you can bind to your data source, then insert the blank to the top of the list afterwards using something like myCombo.Items.Insert(" ", " "); "WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
yes thats right I want to add a blank item to the combobox. I have tried the
combobox.items.insert("","") but am getting the error "Cannot add items to the combobox when a datasource is assigned". Using WINFORMS
So I thought of creating a generic list of type T and pass a new instance of the item T to the list before the List is bound to the datasource.
private static List<T> CreateList<T>(List<T> data)
{
List<T> list = new List<T>();
list.AddRange(data);
return list;
}
I hope this makes sense.modified on Friday, February 26, 2010 6:26 AM
|
|
|
|
|
If you don't know what is the blank value in advance, you can't do it the way you are trying.
You can, for example, add a "" (or string.Empty) in a list of strings, but this will not work for other types.
A possible solution is to create a generic "wrapper".
For example:
public struct MyWrapper<T>
{
public MyWrapper(T value)
{
_value = value;
_hasValue = value != null;
}
private T _value;
public T Value
{
get
{
return _value;
}
}
private bool _hasValue;
public bool HasValue
{
get
{
return _hasValue;
}
}
public override string ToString()
{
if (!_hasValue)
return "";
return _value.ToString();
}
}
As this wrapper is a struct, it can be initialized by the default constructor, in which case the _hasValue is false, so the ToString will return blank. If you initialize it with null (considering T is a reference type) it will do the same.
So, you can create a List<MyWrapper<T>> and add the first value as new MyWrapper<T> and then add a wrapper value for each value of the original list.
But, if the original list is changed, this list will not be.
*I didn't test the code, so maybe there is an error, but I think you can get the idea.
You can also go even further and create an IList wrapper over another IList. It is a big work, but:
You can always return Count as Count + 1.
When requested for item 0, you always return the empty wrapper value.
When requested for any other item, you return a wrapper over the real list item at (index-1) [after all, requesting item 1 means item 0 in the real list].
The advantage is that changing any item in the real data-source will be reflected in your wrapper data-source also, but it is a much more complex task.
|
|
|
|
|
Here's a quick sample that should work:
private void BindToComboBox()
{
List<string> items = new List<string>();
items.Add("Hello");
items.Add("Hello 1");
items.Add("Hello 2");
items.Add("Hello 3");
items.Add("Hello 4");
cboList.Items.AddRange(items.ToArray());
cboList.Items.Insert(0, " ");
} "WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
"Cannot add items to the combobox when a datasource is assigned"
You need to add the blank row to a datatable before you bind it to the combobox.
This is off the top of my head so will need debugged obviously but you will get the idea:
SqlConnection MyConnection = new SqlConnection(PublicVars.ConnectionString);
MyConnection.Open();
SqlDataAdapter sqlDA1 = new SqlDataAdapter("SELECT ItemID FROM some_items", MyConnection);
DataSet sqlDS = new DataSet();
sqlDA1.Fill(sqlDS, "some_items");
DataTable tblItems= sqlDS.Tables["some_items"];
DataRow NewRow = tblItems.NewRow();
NewRow[0] = "-- Click To Select --";
tblItems.Rows.InsertAt(NewRow, 0);
cboItems.DataSource = tblItems;
cboItems.DisplayMember = "ItemID";
cboItems.ValueMember = "ItemID";
cboItems.SelectedIndex = 0;
MyConnection.Dispose();
You should get the idea.
|
|
|
|
|
Hi everyone.
I'm having serious trouble with method EmitCalli of ILGenerator . I've created a simple class to illustrate what I'd like to do:
class TestClass {
public delegate Object DirectReadAccessor(TestClass firstArgument);
DirectReadAccessor[] directReadAccessors;
public Object directRead(int index) {
return directReadAccessors[index](this);
}
}
As you can see, I have an array of delegates, and by calling directRead(i) I call the the i -th delegate with this as a parameter. Fairly simple.
I'd like to generate the method directRead with Emit . I compile this and see the following IL code:
.method public hidebysig instance object directRead(int32 fieldIndex) cil managed
{
.maxstack 2
.locals init (
[0] object CS$1$0000)
--> L_0000: nop
L_0001: ldarg.0
L_0002: ldfld class TestClass.TestClass/DirectReadAccessor[] TestClass.TestClass::directReadAccessors
L_0007: ldarg.1
L_0008: ldelem.ref
L_0009: ldarg.0
L_000a: callvirt instance object TestClass.TestClass/DirectReadAccessor::Invoke(class TestClass.TestClass)
--> L_000f: stloc.0
--> L_0010: br.s L_0012
--> L_0012: ldloc.0
L_0013: ret
}
(where you can just ignore the lines pointed with --> )
I see that it uses callvirt on method Invoke , instead of using calli as was my first instinct. I have the notion that calli would be more efficient, though I may be wrong (please let me know what you think about that). So I try to use calli instead.
I read that Calli follows this process:
1. Method arguments arg1 through argN are pushed onto the stack.
2. The method entry pointer is pushed onto the stack.
3. Method arguments arg1 through argN and the method entry pointer are popped from the stack; the call to the method is performed. When complete, a return value is generated by the callee method and sent to the caller.
4. The return value is pushed onto the stack.
So here was my first attempt:
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldfld, directReadAccField);
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Ldelem, typeof(DirectReadAccessor));
ilGenerator.EmitCalli(OpCodes.Calli,
CallingConventions.Standard | CallingConventions.HasThis,
typeof(Object),
new Type[] { typeof(TestClass) },
null);
ilGenerator.Emit(OpCodes.Ret);
This generates the following IL, which apparently isn't valid and can't even be translated back to C#:
.method public instance object directRead(int32) cil managed
{
.maxstack 3
L_0000: ldarg.0
L_0001: ldarg.0
L_0002: ldfld class [TestClass]TestClass/DirectReadAccessor[] EXT_IdPoolImpl::directReadAccessors
L_0007: ldarg.1
L_0008: ldelem.any [TestClass]TestClass/DirectReadAccessor
L_000d: calli method instance object *(class [TestClass]TestClass)
L_0012: ret
}
Seeing that the generated code used ldelem.r ef instead of ldelem.any , as I had done with Emit, I changed to ldelem.r ef as well:
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldfld, directReadAccField);
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Ldelem_Ref);
ilGenerator.EmitCalli(OpCodes.Calli,
CallingConventions.Standard | CallingConventions.HasThis,
typeof(Object),
new Type[] { typeof(TestClass) },
null);
ilGenerator.Emit(OpCodes.Ret);
I get this:
.method public instance object directRead(int32) cil managed
{
.maxstack 3
L_0000: ldarg.0
L_0001: ldarg.0
L_0002: ldfld class [TestClass]TestClass/DirectReadAccessor[] EXT_IdPoolImpl::directReadAccessors
L_0007: ldarg.1
L_0008: ldelem.ref
L_0009: calli method instance object *(class [TestClass]TestClass)
L_000e: ret
}
But still no luck.
Then, although it seems to contradict the instructions above (the steps followed by calli ) I tried moving my first Ldarg_0 to just before calli , to mimic what is done with callvirt :
.method public instance object directRead(int32) cil managed
{
.maxstack 2
L_0000: ldarg.0
L_0001: ldfld class [TestClass]TestClass/DirectReadAccessor[] EXT_IdPoolImpl::directReadAccessors
L_0006: ldarg.1
L_0007: ldelem.ref
L_0008: ldarg.0
L_0009: calli method instance object *(class [TestClass]TestClass)
L_000e: ret
}
But nothing.
I can use callvirt and the code works, but I'd like to know how to use calli , if not for performance just for the sake of knowledge
Can anyone explain?
Thanks a lot, and sorry for the lengthy post.modified on Friday, March 5, 2010 3:23 AM
|
|
|
|
|
Reading the ECMA specs, it looks like you might need a ldvirtftn instruction before the calli instruction. The calli instruction expects a native pointer to the method rather than a metadata token.
|
|
|
|
|
Thanks for your answer, Kythen.
However, this is bad news for me...
I need to call a method that is already on the stack. I cannot load it with ldvirtftn because I don't have it at the time when I'm generating the code.
The situation is like this: I'm trying to generate the following class:
public MyClass {
public delegate Object DirectReadAccessor<T>(T firstArgument);
private static DirectReadAccessor<MyClass>[] directReadAccessors;
public Object DirectRead(int i) {
directReadAccessors[i](this);
}
public static void InitializeClass(MethodInfo[] directReadAccessorsMInfo) {
int length = directReadAccessorsMInfo.Length;
Type[] typeArguments = new Type[] { typeof(MyClass) };
directReadAccessors = new DirectReadAccessor<MyClass>[length];
for (int i = 0; i < length; i++) {
directReadAccessors[i] = (DirectReadAccessor<MyClass>)
Delegate.CreateDelegate(
DirectReadAccessor<MyClass>,
null,
directReadAccessorsMInfo[i].MakeGenericMethod(typeArguments)
);
}
}
}
* About open instance methods (and other things).
This has been tricky because MyClass doesn't exist when I'm trying to declare the field directReadAccessors , which is of type DirectReadAccessor<MyClass>[] , or when I emit the method InitalizeClass , which again uses MyClass , that doesn't exist yet (that's what I'm creating). However, I've managed to do all this, but now I'm having trouble with method DirectRead , since I don't know how to call the delegate once I have it on the stack. Apparently what I need is the following emit:
ilGenerator.Emit(OpCodes.Callvirt, invokeMInfo);
where invokeMInfo is the method Invoke on DirectReadAccessor<MyClass> , and which I should obtain like so:
MethodInfo invokeMInfo = typeof(DirectReadAccessor<MyClass>).GetMethod(
"Invoke",
BindingFlags.Instance | BindingFlags.Publ<code></code>ic | BindingFlags.NonPublic,
null,
new Type[] { typeof(MyClass) },
null
);
Again, the problem is that neither MyClass nor DirectReadAccessor<MyClass> exist yet. I have the TypeBuilder for MyClass and the unfinished DirectReadAccessor type, which I've created like this:
directReadAccessorType = typeof(DirectReadAccessor<>).MakeGenericType(typeBuilder);
But if I try to call GetMethod("Invoke", ....) on directReadAccessorType as shown above I get a NotSupportedException , because I cannot obtain the method Invoke for an unfinished type. I've tested this assumption by making the same call after finalizing the type with:
typeBuilder.CreateType();
And indeed I do not get the exception in that case. However, I need to be able to get the Invoke method's MethodInfo before finalizing the type, while I'm emitting the code for InitializeClass .
It's a strange situation: I'll have the delegate when I need it, but I cannot produce the code to invoke it. Can anyone offer any help?modified on Friday, March 5, 2010 3:23 AM
|
|
|
|
|
In case anyone is insterested, they answered this for me at stackoverflow. The following call works:
TypeBuilder.GetMethod(directReadAccessorType, typeof(DirectReadAccessor<>).GetMethod("Invoke"));
|
|
|
|
|
Good morning all,
I am currently working on enslaving the webbrowser control to be an editor the way I like it.
One problem I steped into was the lack of beeing able to supply a target for links.
My first approach was to write my own Link-Selection window (no problem here) and it works like a charm on all text-selections.
But unfortunately not on images...
Is there a way to get the selected image?
If my text-range is empty I at least know that I have to deal with an image, but unfortunately not on which one.
I was able to iterate the link collection of the document before saving it and add a target there via setAttribute, but still I would like to know how to get the selected image.
I noticed that when using the execCommand (CreateLink) the selected image (which got a border with the resizing dots on the edges) gets a selection overlay that I am also not able to reproduce. Maybe changing the selction into that would solve the problem...
Any ideas on this one?
best regards and have a nice day
hoernchenmeister
|
|
|
|
|
Iam very new to this community, i want to read a .doc and display it to console. I tried with the following code but getting some garbage values in addition to actual code.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StreamReader file = new StreamReader("D:\\keywords\\id1.doc");
String st = "";
while (st != null)
{
st = file.ReadLine();
Console.WriteLine(st);
}
file.Close();
}
}
}
Can some body helpme out with the Process for reading a .doc file...
Thanks in Advance....
|
|
|
|
|
Message Removed
modified 23-Nov-14 7:08am.
|
|
|
|
|
|
I suggest looking at that example again. That code is not specific to a Windows Form application. It can be used in both, aybe with a tweak or two to fit specific requirements.
|
|
|
|
|
|
Whell, abouve post all requiers Office installed.
If you want independed application, start reading this: Office Binary Protocol[^]
|
|
|
|
|
You will need to use something like Office Interop or the open source NPOI[^] library.
|
|
|
|
|
I used crystal report XI in my application and include merge module for crystal report XI in setup when I deploy it at client while I user want to see report; following exception is occurred.
System.IO.FileLoadException: Could not load file or assembly 'CrystalDecisions.Shared, Version=12.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'CrystalDecisions.Shared, Version=12.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304'
Can any one help me to resolve this issue.
Thanks in advance.
|
|
|
|
|
You have to reinstall Crystal Report.
Probably it will be useful.
|
|
|
|
|
Thanks Mr. Mohammad.
But I need solution at client end. i think
I need run time for crystal report XI to deploy at Client.
can any one tell me where i will find it. or any other suitable solution for it.
Thanks again.
|
|
|
|
|
I have a medical software and I want to know what's the best control to use and what's the best SQL field type for blood pressure?
The blood pressure as you know is in this format: 999/999
it also has a flag for either right hand (L) or left hand (R), something like radio control
I don't want the traditional way!! I mean two numeric and two radio?!
I thought of the ButtonEdit from DevExpress with a ###/### mask but I faced a problem with the L and R flags..
What is your advise?
|
|
|
|
|
Message Closed
modified 23-Nov-14 7:08am.
|
|
|
|
|
but only one will be used at the time. you know when you are visiting the doctor, he will take the blood pressure sometimes from your right hand and sometimes from your left hand d so why do I have to show both if only one will be shown, well, it's very rare that doctor will take blood pressure from both but i can then consider as new row in the sql table but my issue is what's the best way to do on the form and in the sql server table?
|
|
|
|
|
So, the 99/99 is the systolic/diastolic blood pressure which may be for either the left or right arm, but sometimes for both arms?
I'd use a tab control that contains two tabs. One tab would say "Left Arm" and the other would say "Right Arm". Each tab would have a label that says "Systolic / Diastolic" and a MaskedTextBox with the mask set to "990/990". I believe "9" means "digit is optional" and "0" means "digit is required". That would allow the user to enter a number between 0 and 999 for the systolic value and for the diastolic value. That would also allow the user to enter a blood pressure for the left arm and/or the right arm.
As far as storing it in the database, you could make it complicated, but I'd just keep two varchar fields (one for the left arm and one for the right). One field could be called LeftArmPressure and the other RightArmPressure. You could store each as string like this: "120/80". Then you just serialize/parse as necessary. If you wanted, you could create four fields (LeftSystolic, LeftDiastolic, RightSystolic, RightDiastolic) or you could normalize the field into a new table that points back to your primary table, but that is more complicated than it needs to be. Unless you need indexing on both the systolic and diastolic (unlikely), I'd just go with the two fields.
|
|
|
|
|
but i am trying to reduce number of controls on the form?!
is it possible to have one control, textbox for example with a mask of (999/999 Left) or (999/999 Right)? then save each separately in sql?
|
|
|
|
|