|
Hi Friends, I have an issue regarding components but I could not find out through google or somewhere else.
I'm creating a component extended from a TextBox with extra bindable properties, but to get it perfect, some of these bindable properties would have to be custom designed or runtime designed.
Imagine that besides the bindable property "Text" I created a new one named Key... but to get my component working I need to bind extra properties and they have to be custom. there comes the question.
Is it possible to create bindable properties during runtime or design time for a custom component?
[Bindable(true)]
public int Key { set; get; }
|
|
|
|
|
All properties are bindable, however the usual problem lies in two-way binding, that is getting the textbox properties to update automatically when the object it is bound to changes.
Take as look at the INotifyPropertyChanged[^] interface. To get the binding working you will probably have to implement this interface on your Textbox.
If you have knowledge, let others light their candles at it.
Margaret Fuller (1810 - 1850)
www.JacksonSoft.co.uk
|
|
|
|
|
I understand it, but I have more than one custom bindable attribute defined in the component. I had problems to get the values updated, but I could fix it using Reflection, it's now fine. I just keep with this other problem, I need to design custom bindable atributtes in runtime..
That's an example of what I need:
string[] bindableProperties = {"Text", "EditValue", "Key" }
foreach (string BindablePropertyName in bindableProperties)
{
// command to create custom bindable property named BindablePropertyName.ToString() < --------- (My Needs!)
Object.DataBindings.Add(Binding(BindablePropertyName, Object, "PROPERTY_NAME", true));
}
|
|
|
|
|
You can create new types at run-time but you can't modify an existing one.
In any case, what's this for? What would bind to the properties? Obviously there can be no design-time support or anything like that for properties that don't exist at design-time and aren't known until run-time. And if it's all happening dynamically at run-time and uses reflection, why even use properties? Why not just a name-value collection?
|
|
|
|
|
let's use the following relational example.
Database Structure:
table one: Employee(EmployeeID, Name, ContactID)
table two: Contact(ContactID, ContactName, Email)
Given the information, an Employee can have ONE Contact. My Component has 2 custom bindable properties, KeyID and Text... when the user types the name of the Contact, it binds the Text (ContactName), and the ContactID in the KeyID property as soon as it is validated.
That's fine... the data at the primary table will be filled and if I try to insert the data on the database, it will work.
Now imagine that I want to see more than one information, not only the contact Name, but I want the component to fill other information Like Email, so.. when i type the name and bind it's key, i also want to show the contact email in another field.... then I need another bindable property linked to the ContactEmail.
Form Design presentation:
EmployeeID (textfield), Name (textField)
ContactID and ContactName (customComponent databind (Text and KeyID))
Email = ? (what is going to fill the email data when I type and select the ContactName on my Component?)
I'll have both textfield and CustomComponent binding to the same field (Email) but one will be used ONLY as presentation. Actually
i need a code example of how to create a bindable property, all the others stuffs I can fix
Any doubts about it?
Thanks!
|
|
|
|
|
I still don't understand what this has to do with run-time or why you need a bindable property. Are you going to dynamically generate the UI based on the data model? If so, why not just create another text box for the email field and bind the text property of that text box? Or will the data model itself be user-defineable and thus change at run-time?
I also think putting storage logic into a UI component is a bad idea. It will tightly couple the UI to the storage medium, and worse, make it rather harder to maintain the system long term. What if you need to add a different UI layer for portable devices? Or a web service? Or if you want to have a native Windows Forms interface and an asp.net web app interface? What if you decide to add import/export capabilities so you can create Employee or other records from files?
A far better solution in my opinion is to use an object-relational mapper such as NHibernate. Then you'll have a class representing the Employee and use that as the data source for your UI. If you really need to do these things dynamically, you can still do it this way - it's a lot more work obviously, but you could generate both the classes and the NHibernate mappings.
If you want an "employee" UI control a simple user control would suffice, and it could take an Employee instance as data source.
|
|
|
|
|
I'll try to explain again...
Imagine I have many textboxes on a windows form and they are all binded to their corresponding fields at the bindingsource. If I fill information on the textboxes, than add another record and so on.. there will be lots of records with different data... that's nothing more then a simple windows forms with databinding. Fine.
Now imagine that I have a form with different information and I need to get data from an extra table to fill the corresponding foreign key value while i'm inserting a new record. I would have to do some search engine or use a dropdown whose datasource would select data from the extra table. Alright....
I made a component, and when the guy start typing something, it brings data from the corresponding extra table and when the Enter Key is pressed, it validates the information and updates the foreign key.
However, I have 2 bindable properties, one for the foreign key field and another one for the search value.. or "Text". I wanna customize it, to create different properties during runtime, and not to be stuck only in 2 bindable properties, so the component can bring more information and update (set value) more then 2 fields on the Binding Source.
I understand that I could create like 5 fixed parameters to be instantly linked to the binding field whenever I need more than 2 binds... but that would be sucks, that's the reason I'm asking help to create runtime bindable parameters...
Sorry mates! God please send me a message
|
|
|
|
|
What are you going to do with this "extra information" you want to "bind" to? Display it? How are you going to dispay it? Dynamically created Textboxes? If that's the case, each "new" TextBox would consitute a "new" Binding (and you would not be "stuck" with only 2 Bindings, as you put it).
You could even "hide" these dynamically created TextBoxes or whatever, if it buys you anything.
And besides ".Text", you can also bind to a Control's .Tag property, which can be stuffed with just about anything.
|
|
|
|
|
Yes, it's a textbox only to display information of a binding field, but what the component does is retrieve data from another datasource and sets the information to the binding field.
Therefore, I would have a textbox control binding the field "Total" (display only) and a custom dynamically created property in the component, binding to the same binding field "Total", the only difference is that the component is going to set the information of the Total Field and as BOTH are linked to the SAME bindingfield, the commom textbox will be updated too for display only.
|
|
|
|
|
I still don't get it.
Can't your component just set the Total property, to which the TextBox is bound?
Why does it need to have another property and bind this to the Total property? To do so dynamically it would have to know about (discover) the Total property anyway, so rather than create another property that also represents the total, and then bind this to the "real" Total property, and then set this other property so that the read-write binding causes the Total property to change... would it not be a lot easier to simply assign the Total property directly?
It may very well be that I haven't understood what you're trying to do. But if all you want to do is to automatically load the data from a foreign key into another data source, I cannot see how bindable properties comes into the picture at all. You just need to know what the foreign key value is and to what table it refers, and you could hold this information in any number of simple data structures, regardless of how it is obtained (mapping files, dynamically discovering relations defined in the database, attributes on the entity types, or whatever).
I will simply repeat this: Writing a bunch or relational logic and putting into a user interface component is not a good idea. It is no less work yet has many drawbacks compared to properly separating UI from business logic from data access. The problem you are trying to address, that of having to spend a lot of time on mundane data access code that to a large extent follows automatically from the data model (in most cases, the entities in the domain model largely correspond to the tables in the database), is one that many large and well organized groups of people have tried to tackle before you. Take a look at the Entity Framework, or NHibernate, or even CodeSmith templates that will generate a DAL for you.
|
|
|
|
|
And your problem is ... ?
All I see is one or more TextBoxes that are bound to a "second" BindingSource (the "lookup" table) and a custom component that is used to "position" that BindingSource via a CurrencyManager / BindingManager.
You could even use a relation between your primary and "lookup" tables and bind to that, dispensing with the "custom component" altogether.
Are you binding to Typed DataSets or Entities / Entity Framework, or is everything just custom code / properties and no backing collection types? If the latter, that may be why you are finding things so difficult; you're not using the features that are available in the Framework.
|
|
|
|
|
Hi, I have a word file which contains numbers of shapes. Each shape contain some textboxes. I am copying that file to another location and assigning some values to those textboxes of those shapes. Now I have to assign the values to those textboxes then I have to ungroup those shape first then only I can assign values those text boxes. Now the problem is that when I ungroup those shapes, they change their locations in the document. I do not understand what is the problem. Here is the code
//copy the sourc file to another location
System.IO.File.Copy(Convert.ToString(source), Convert.ToString(destination));
Microsoft.Office.Interop.Word.Document obDoc = new Microsoft.Office.Interop.Word.Document();
object unknown = Type.Missing;
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;
obDoc = varWord.Documents.Add(ref source, ref unknown, ref unknown, ref visible);
obDoc.Activate();
//hiding a shape--working fine
obj = "shape1";
varWord.ActiveDocument.Shapes.get_Item(ref obj).Select(ref unknown);
varWord.ActiveDocument.Shapes.get_Item(ref obj).Visible = MsoTriState.msoFalse;
//ungroup the shape
obj = "Shape2";
varWord.ActiveDocument.Shapes.get_Item(ref obj).Select(ref unknown);
varWord.ActiveDocument.Shapes.get_Item(ref obj).Ungroup();
obj = "txtbox1";
varWord.ActiveDocument.Shapes.get_Item(ref obj).Select(ref unknown);
varWord.Selection.TypeText(txtbox1value);
//show the word file
varWord.Visible = true;
varWord = null;
Pankaj
|
|
|
|
|
Got the solution
I have used following code before ungrouping the shape
System.Threading.Thread.Sleep(1000);
Actually when c# code run its ungroup the shape first, then render the file, that's why shape is showing in the header of the page, not in the proper position. Now using the sleep method it renders the shape in proper place.
Pankaj
|
|
|
|
|
ha ha ... I think the process takes some time to ungroup. So sleeping the Thread cures the problem.
|
|
|
|
|
May be you are right. I have also posted the code for ungrouping. But there are no problem in ungrouping. Do you have any solution without using threading ?
Pankaj
|
|
|
|
|
Is there a way to discover which is the actual exception, without using the catch block?
For example:
Dispose() will be called if there is an exception or not.
I want to know if this Dispose is happening naturally, at the end of the block or by an exception that forced the end of the block.
My idea is simple test something like:
Exception.GetCurrentException() != null
But I don't know if there is somewhere to look for this.
|
|
|
|
|
Paulo Zemek wrote: Is there a way to discover which is the actual exception, without using the catch block?
This is the second question on this subject that makes no sense. The try/catch construct is there for a reason, so make use of it and stop your programs from causing havoc.
|
|
|
|
|
Paulo Zemek wrote: Dispose() will be called if there is an exception or not.
if you have a using statement, yes, Dispose() will be called no matter what.
The functionality of Dispose() is to dispose of managed and/or unmanaged resources, independent of circumstances; so you are not even entitled to know if and which exception has occurred.
Luc Pattyn [Forum Guidelines] [My Articles]
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Don't use 'using' block, try this instead:
bool isOK = true;
try {
..........
..........
}
catch {
isOK = false;
}
finally {
if (!isOK) {
} else {
}
}
|
|
|
|
|
I know I can do this with try/catch/finally.
But I really want to know if there is any way to discover if the method I am is actually called by an exception (is inside a catch/finally) or not.
The purpose is to make the method simpler to use, as the caller of the method will not be required to pass the exception (or the boolean) as parameter.
A very ugly example:
using(var someTransaction = new MyTransaction())
{
... code here ...
someTransaction.Commit();
}
And then, in Dispose, I will check:
If the transaction was committed/rollbacked, do nothing.
If the transaction was not committed or rollbacked, but there is no exception, I will throw an exception telling that Commit or Rollback is required.
If there is already an exception, I will rollback and keep the original exception.
- Note: I know how to use try/catch/finally. But I really want to make the use of the code simpler for the others. using keyword looks nicer than try, catch and finally.
modified on Monday, November 23, 2009 12:38 PM
|
|
|
|
|
One possible solution is to have a global variable which all try/catch code can log their exception to. (Probably best to make that a List<exception> and process stuff on or off that exception stack.) Any code can then test the global exception variable to see if there is an exception outstanding. Once you have acted appropriately to that exception then clear it off the stack.
If you have knowledge, let others light their candles at it.
Margaret Fuller (1810 - 1850)
www.JacksonSoft.co.uk
|
|
|
|
|
But this brings the original problem again:
The developer will need to "add/remove" exceptions manually. My question is simple:
.Net has something like that built in (in any class) or not?
I originally thought that I could find catch using StackTrace, but it has no information about catchs.
Is there any way to discover this, even if this is an expensive operation? After all, this will be used only in Debug code.
-> To be honest, I really think that .Net must register exceptions automatically in a "FILO/LIFO" queue and so, when one exception is thrown inside a catch it could automatically fill the "inner exception" property without manual coding. Of course, such list must be ThreadStatic, as one thread can be executing normal code, while the other is executing a catch inside a catch.
|
|
|
|
|
In ASP.NET, there is something like Server.GetLastError();
|
|
|
|
|
Short answer: No.
There are sometimes ways to get an otherwise unhandled exception without a try-catch. In asp.net apps for example you can handle Application_OnError. But it's not a substitute for exception handling elsewhere in the code, only a last-resort chance to at least do things like log errors.
You can use either delegates or polymorphism if you want to standardize exception handling in certain situations. Something like this would be possible:
public static class DbHelper
{
public static RunInTransaction(SqlConnection cnx, Action method)
{
cnx.Open();
SqlTransaction tx = cnx.BeginTransaction();
try
{
method();
tx.Commit();
}
catch (Exception ex)
{
tx.Rollback();
throw new ApplicationException("Error during database transaction.", ex);
}
finally
{
cnx.Close();
}
}
}
Here I used the predefined Action delegate, which is void and takes no parameters, but you could of course
use any delegate you'd like. Or you could use polymorphism: It could be an abstract method that derived classes must implement (obviously the class then can no longer be static!), or the method could accept an object implementing some interface instead of a delegate.
There are some cool possibilities with IDisposable but it is really intended specifically for early release of non-managed resources. It's not meant to be a general try-finally replacement, and certainly not a try-catch-finally replacement.
modified on Monday, November 23, 2009 2:39 PM
|
|
|
|
|
|