|
I don't think you will be able to share the data source like that. I believe datasources have a single "current record", which all the combo boxes are going to show. So if you change one, then the others will reflect that change also. This is typically the desired behavior.
What is the type of your data source? You may be able to Clone it to get around this problem.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Ah! Thank god I included the DataSource in the sample code, I didn't expect that to be the source of the problem. I would have never checked that.
The DataSource is an ArrayList with string to show in the drop down. It's not dynamic, so I can keep it anywhere, and it's short, so I can actually just generate it on the fly and assign it. That's inefficient, I know, and if I have 100 comboboxes, I'll have 100 arrays of the same 10 strings around, but I can live with that.
Let me go test your theory. Great catch, there.
|
|
|
|
|
Yep, that was it. I should have figured this out, since none of the other controls which were sharing handlers were showing similar behavior (buttons, checkboxes, etc), EXCEPT the other controls that had DataSources.
A couple of the comboboxes do have lengthy lists attached to them. You're saying I should CLONE my original array, and bind to that? Is that any more efficient that just looping through the original array, using combo.items.add to add it to each combo as I create it?
|
|
|
|
|
I'm not sure it matters. Any way you look at it, you will have to have three (assuming there are three ComboBoxes) lists. There are either three DataSources or three ComboBox.Items.
If the lists are long you may want to create a wrapper around the ArrayList. The wrapper would take a reference to an ArrayList. It would also implement the IList (and possibly the IList<T>) interface. Then you could just forward the IList method calls to the wrapped ArrayList. This way you have a small (in terms of memory) object that "points" to the large ArrayList. Then you would create three instances of your wrapper (all pointing to the same ArrayList). I believe this will get around the DataSource issue. This is really only useful if your ArrayList is large, since it would reduce memory usage.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hi,
AFAIK you can share an array, ArrayList, List, ... between multiple ComboBoxes;
these collections don't have a "current selection" that they would impose on the ComboBoxes.
So I see no need to Clone() anything.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I'm not sure what you mean by "sharing" the Collection here. Aside from binding the Collection to the Combo as a DataSource (which I was doing earlier, and that was causing the problem), or copying the individual items from the Collection to the combo one at a time, what other method are you talking about to attach to the Collection to the combo?
|
|
|
|
|
Hi,
if you assign the same array/ArrayList/List as the DataSource of several ComboBoxes,
how would they exchange current selection info They don't!
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I think the BindingContext[^] on the form maintains it for each DataSource. Since each ComboBox has the same data source (e.g. ReferenceEquals would return true), then they all share the same "current record" (which is maintained by the BindingContext. Apparently, you can "group" data bound controls using Panels (or GroupBox, etc) but you have to set the BindingContext property on the Panel to override the Form's BindingContext.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Well, Luc, try it out .
The whole point of binding DataSources is to share the Binding Context, so that all of the controls point at the same "record" in the DataSource. That way you can have several controls on the form (like, say, Back/Forward/Edit/Delete, etc) that all deal with the same "row" or data in the source.
I had completely forgotten about this when I used "combo.DataSource = myArray". I was just looking for a cheap way to make all of the items in my array show up in the combo box.
TJoe is right, if I want each combo to have its own context to the data in my array, I need to either Clone each array and bind to it, or just copy the items from the array to the combo.Items collection manually.
Since there is no real advantage to making the array a DataSource here (I'm not really using the BindingContext for what its intended), I'm just doing the latter now.
|
|
|
|
|
JoeRip wrote: try it out
OK, I'll add it on the todo list (not at the top though).
[added] checked. You're right, ComboBoxes sharing a DataSource, even a simple array,
become fully synchronized. I didn't know that. Thanks.[/added]
-- modified at 12:24 Monday 1st October, 2007
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
The collection object themselves do not maintain the current record. I believe the ComboBox and other controls maintain it. Specifically, the BindingContext[^]. It looks like if you add the ComboBoxes to a Panel, then that would create a new BindingContext.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hi all,
I am working with Visual studio Macros.
I created one macro.and i assigned one keyboard shortcut to run that macro in VS 2005 Applications.its wrkng fine.
But my problem is when i taken new application when i wrk wth this shortcut macro is not firing.
So,to make macros global to all applications what to do?
krishna Prasad
fttyhtrhyfytrytrysetyetytesystryrty
|
|
|
|
|
I believe you need to restart VS after assigning the shortcut. Basically, VS saves changes when it exits. So if you open VS, assign the shortcut, then open another instance of VS then the second instance will not have the assigned shortcut. This is typical of most settings in VS.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hi all,
I am new to C#....
I thought that implementing a simple calculator would help me understand the language... I have come up with a code....
It would be great if the gurus here give me feedback on this...
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
<br />
namespace Calculator<br />
{<br />
class compute<br />
{<br />
private string[] inputNumbers;<br />
public string computeResult(string inputExpression)<br />
{<br />
string result = "There was an error while processing!";<br />
inputExpression = inputExpression.Replace("+", "+A");<br />
inputExpression = inputExpression.Replace("-", "-S");<br />
inputExpression = inputExpression.Replace("*", "-M");<br />
inputExpression = inputExpression.Replace("/", "/D");<br />
<br />
if (inputExpression.Substring(0, 1) != "-" && inputExpression.Substring(0, 1) != "+")<br />
inputExpression = "A" + inputExpression;<br />
else<br />
inputExpression = inputExpression.Substring(1);<br />
inputNumbers = inputExpression.Split('+', '-', '*', '/');<br />
<br />
decimal output=0;<br />
int i;<br />
int count = inputNumbers.Length;<br />
for (i = 0; i < count; i++)<br />
{<br />
string calc = "";<br />
inputNumbers[i] = inputNumbers[i].Trim();<br />
decimal temp;<br />
if (!decimal.TryParse(inputNumbers[i].Substring(1),out temp))<br />
break;<br />
calc = inputNumbers[i].Substring(0,1);<br />
<br />
switch(calc)<br />
{<br />
case "A":<br />
output = output + temp;<br />
break;<br />
case "S":<br />
output = output - temp;<br />
break;<br />
case "M":<br />
output = output * temp;<br />
break;<br />
case "D":<br />
output = output / temp;<br />
break;<br />
}<br />
<br />
<br />
}<br />
<br />
if(i==count)<br />
result = output.ToString();<br />
return result;<br />
}<br />
}<br />
}<br />
<br />
You don't have to be AFRAID!
|
|
|
|
|
Minhaj.Net wrote: I have come up with a code....
It would be great if the gurus here give me feedback on this...
Yes that does appear to be code
Did you read the first message in the forum titled, "How to get an answer to your question"?
|
|
|
|
|
Hi,
I think this will work most of the time; I have several comments though:
1.
please post multi-line code in PRE tags instead of CODE tags; that would preserve
indentation.
2.
your error handling is insufficient; at the bare minimum you should put all code inside
a try-catch construct.
3.
your handling of operators is quite creative; replacing them to keep a trace when
they tend to disappear by the Split()...
the line Replace("*", "-M"); is probably not what you intended, but it does
nof affect the outcome.
You don't really need the letters ASMD, you could have duplkcated the operators instead.
4.
there are some functional shortcomings:
- you only handle diadic operators (that's having two operands); what if you need
a monadic minus, as in "-4 * -3" ?
- you fail to obey the operator priorities: "2+3*4" should give 14, not 20.
the right way to fix this is by using a stack machine, i.e. postpone the operator (+)
and push the operands, until another operator of same or lower priority is seen (or end
of expression), then pop operands, perform operation and push result.
5.
programming details:
- you could replace for (i = 0; i < count; i++) by foreach(string inputNumber in inputNumbers)
- to get some character from a string, you don't need Substring(x,1); you can index as
in inputNumbers[i][0] which gives the leading char (unless length is 0)
Keep up the good work! May I suggest you read several CodeProject articles...
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Thank you very much Luc Pattyn!
that will really help me....
I would like to go through the articles here.... But with so many of them, I find it a bit difficult to chose....
Thanks for the advice
You don't have to be AFRAID!
|
|
|
|
|
Hi,
In my C# application, I'm getting a dataset from the database for transport to excel. In the data set there is a date column. While writing to excel it is giving an entire different format, whereas in dataset it is coming correctly. For eg:
if in the dataset suppose the date is 03/12/2006, while writing to excel it is writing as 2006-12-03T00:00:00.0000000+05:30. I don't know how this is happening. I'm using RKLib dll for exporting the data to excel. Anybody who knows will you please help? This is urgent.
Thanks,
Meeram395
|
|
|
|
|
hi friend use this format
CONVERT(VARCHAR(15),AC.CGO_RVD_DAT ,103)
|
|
|
|
|
I have been searching google for an answer for this, and maybe i just dont know how to proeprly word my search, but I have come back with nothing useful. My issue:
I have been playing around with the System.IO.Compression namespace, and have noticed an interesting little behavior:
Compressing files that are already in a compressed format (example: jpg, mpg, zip) or have been encrypted with System.Security.Cryptography, come out of the compression BIGGER than when they went in.
I was wondering if anyone A. knows why this happens, and B. knows a way around it
If there is no way around it, then my code has to be modified to exclude formats that might have this issue, but it would be swell to be able to just compress the buggers.
______________________
Mr Griffin, eleventy billion is not a number...
|
|
|
|
|
Since you are compressing something that is already compressed, then you won't be able to make it smaller. But while compressing the second time, the code still needs to add a header to decompress it. So if the original data has X bytes, then the compressed version would (approximately) have X + Y, where Y is the header.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Great! I know it seems weird that I would be excited, but as long as I understand what is going on under the hood, I can work around it.
That is a huge help. So I have to specify which files will be compressed and which wont, Oh Well.
if i didnt like to code, i wouldnt do this for a living
______________________
Mr Griffin, eleventy billion is not a number...
|
|
|
|
|
Assuming the compression is not expensive (e.g. you are only working with small streams), then you could simply code the method such that it tries to compress the data. Then it would compare the original size to the compressed size and if the compressed size is larger, then it saves the original data and trashes the compressed data. You would probably need to maintain a flag with the file that specifies whether you compressed it though.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
I have a pretty good idea of which filetypes are compressed or not, so i will probably integrate what you suggested with an enum or something that will look at the extension. The code i am writing will possible handle hundreds of files at a time, and any memory overhead i can save will be good.
Thanks again for your help, you have saved me a headache.
______________________
Mr Griffin, eleventy billion is not a number...
|
|
|
|
|
This is fairly typical. Once a file has been compressed, compressing it again very rarely makes it smaller still. As Tom pointed out, the second compression adds additional header information to the file, which ends up making it bigger. There really isn't a way around this.
|
|
|
|
|