|
Hi, I am collaborating/taking over a project from another developer in my office. One part is a Windows form with a CheckBoxList controls on top and rows of ComboBoxes on bottom like this:
cbo1_1 cbo1_2 cbo1_3 cbo1_4
cbo2_1 cbo2_2 cbo2_3 cbo2_4
etc...
If there is anything selected in the CheckListBox then row 1 is enabled. More rows could be enabled depending on what exactly is checked.
The problem is that so much code is duplicated. For instance, each cbo is bound to a column in a dataset. For each cbo this code exists:
-------------------
private void Populatecbo1(System.Windows.Forms.ComboBox s)<br />
{<br />
s.Items.Clear();<br />
int Count = _aColumn.Length;<br />
for(int i =0; i < Count; i++)<br />
{<br />
s.Items.Add(_aColumn[i].ToString());<br />
}<br />
} ---------------------
and one for cbo2 and so on. Theres also literally thousands of lines of switch statements for enabling/disabling cbo's.
Isn't there a better way to do this without duplicating all of this code?
Thanks,
Robert
|
|
|
|
|
RblEdwards wrote:
Isn't there a better way to do this without duplicating all of this code?
Without seeing the full code it's not going to be possible to supply a working example, but the answer to your question given the information here is a resounding YES.
I would suggest creating an array of ComboBox controls, and iterating over them to populate them and set the Enabled property, there's a good chance you will be able to reduce it down to 3 or so methods.
e.g.
int cols = 10;
int rows = 10;
System.Windows.Forms.ComboBox[,] comboArray;
comboArray = new System.Windows.Forms.ComboBox[cols, rows];
Another possible option would be using a DataGrid to display the data and combo boxes (there's plenty of examples online for putting ComboBoxes in DataGrid cells), and simply manipulating the DataGrid properties to enable/disable item editing.
|
|
|
|
|
In addition to Furty's response...
If you have a collection of controls you can either populate them at
design-time (which leads to code like you've got) or you can programmatically add them yourself.
Try this:
Create a new project and put a GroupBox on the form
Add a single control to that GroupBox
Look at the Windows Generated code.
Now you have a template for what YOU would do, since the logic is adding a single control to the Control collection. Rip out the Windows generated code and make it a programmatic loop to add each control. Now --- if you have an event like having an item selected, point it to a single routine in your build code. If it is necessary to know which control from top to bottom was selected, you can interate through the Control collection until you find that control, and that becomes the index for populating the corresponding control in the other groupbox.
The beauty of reusability!
_____________________________________________
The world is a dangerous place. Not because of those that do evil, but because of those who look on and do nothing.
|
|
|
|
|
I have Thread.Sleep in my program, and to fastest I can get it to sleep is 1 milla second...
I need it to be faster...
1 Micro second....
and way to do this?
/\ |_ E X E GG
|
|
|
|
|
When Thread.Sleep is executed the .NET framework makes a system call that immediately causes a context switch. (AFAIK) Could you give a little more info about what you are trying to do?
|
|
|
|
|
I just need my program to to pause... or delay... or wait.. for 1 micro second...
the Thread.Sleep can only go as fast as 1 milla second...
/\ |_ E X E GG
|
|
|
|
|
If you just want a short "delay" then you do not want to use Thread.Sleep. Because even a Thread.Sleep(0) causes a context switch and it may take "a long time" for the OS to hand control back to your program, depending on how many threads/tasks are scheduled.
Perhaps there is a way to inline some assembly in a C# app (I don't know about this though) and you could execute a no-op or something that would fill your requirements for a delay.
But you will still have that context switching problem since windows is preemptive multitasking. Consider this:
1 Executing code
2 Executing code
3 Your Delay
4 Executing code
5 Executing code
Even if there were some (atomic) delay statement that you could use at step 3 a context switch could take place between 3 and 4. This would make your delay appear to be even longer.
Good luck though, maybe there is a workaround.
|
|
|
|
|
ok, sleeping for (1) is one milla second right?
well what would (0) be? that's crazy... how long is 0? could that possibly be considered a micro second almost?
/\ |_ E X E GG
|
|
|
|
|
Eggie,
You need to read about the Thread.Sleep method and more importantly what a context switch is.
Sleep( X ) means the thread will not be scheduled for execution by the operating system for X milliseconds. Sleep(0) causes the thread to be suspended (a context switch) and immediately rescheduled for another time slice. If there are no other threads at your thread’s priority level then it is executed.
The bottom line is you will not be able to create a delay to the precision you are looking for. (Well, you might be able to with in-line assembly but a context switch could occur and make the “delay” longer. And, now that I think about it, you will probably have to boost your thread’s priority. ) Sleep(0) is the shortest delay that I can think of but there is no control over its duration. Since there will be other threads waiting, they will all execute their time slices before yours gets the CPU back. I don’t recall what Microsoft has a time slice set at its probably around 50ms so if there are n threads scheduled before yours the delay will be n * ~50ms
In the post above Daniel wrote “More time resolution is only available with multimedia timers or DirectX.” He is right. What I’m speaking about is similar to you listening to music or watching a dvd on your computer. If you do anything that uses the CPU a lot, the music or movie becomes "choppy." That is, indirectly, related to the context switches that I’m talking about. Besides these context switches there are other things like interrupts going on(e.g. moving the mouse).
Anyway, there is always a workaround for coding problems.
Good luck.
BTW: You got me curious. Why do you need such a short delay for?
Context switch:
(this is just a summary, Google around for more details)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/context_switches.asp
Sleep Method:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingthreadclasssleeptopic1.asp
|
|
|
|
|
I'm making a program... well I have a program that sends data though the parallel port and stores it in the memory on my board...
The data is just a 50,000 line .xml file with a bunch of Hexidecmial.
Everything works fine... except I need that delay between bytes to be more precise...
I just downloaded the DirectX SDK, now I have to figure that out...
uh... anymore suggestions?
/\ |_ E X E GG
|
|
|
|
|
why not do a delay like the old way we'd do it in DOS?
DoProcess()<br />
DoProcess()<br />
DelayCode()<br />
DoProcess()<br />
<br />
<br />
<br />
public void DelayCode()<br />
{<br />
for (int i=0; 1<1000; i++)<br />
continue;<br />
}
_____________________________________________
The world is a dangerous place. Not because of those that do evil, but because of those who look on and do nothing.
|
|
|
|
|
Maybe Thread.SpinWait could help you.
More time resolution is only available with multimedia timers or DirectX.
Acting as a substitute for God, he becomes a dispenser of justice. - Alexandre Dumas
|
|
|
|
|
Can it sleep from 1 micro second?
/\ |_ E X E GG
|
|
|
|
|
|
I have a simple console app I created in C#.net. Can I just distribute the exe ?
We use to be able to this ..
When I give it to some people it says "NTVDM CPU has encountered an illegal instruction". I gave the exe to people running WIN2K and WINXP. Do I need to distribute files with a console app. ( yes, they do have .NET 1.1 running on their machine )
It does work on developers machines though ..
ANYONE ??
|
|
|
|
|
Dato wrote:
When I give it to some people it says "NTVDM CPU has encountered an illegal instruction".
I've seen this kind of behavior with some viruses which doesn't understand the format of a .EXE .NET assembly and corrupt it. See if your customer's machines aren't infected.
Acting as a substitute for God, he becomes a dispenser of justice. - Alexandre Dumas
|
|
|
|
|
For some reason ..
You can't throw around Console exe anymore. Even if the .NET framwork is on the target machine. I had to create a setup project and it worked fine..
I need to read more on distribution really... I don't understand all the ins/outs...
|
|
|
|
|
This behavior does not have anything to do with .NET framework but with restrictions with the cmd under those machines. Look into:
http://www.derkeiler.com/Mailing-Lists/securityfocus/pen-test/2003-05/0115.html
To bypass these restrictions.
|
|
|
|
|
Thks anonymous ..
Will look into it..
|
|
|
|
|
O.K. this is just out of interest as I've decided to use GDI+ but I was wondering if anyone knows how to double buffer a direct draw window as part of a standard windows application. The problem as I understand it is that for the double buffering to work the device needs to be exclusive which means it needs to be full screen and I've tried and failed to create a back buffer and then draw the data to the back buffer before drawing the back buffer to a primary buffer. Note though that you can't set up the back buffer as a true back buffer because of the exclusive problem so it was set up up as an offscreen surface.
So anyone with any ideas that might just be stupid enough to work?
pseudonym67
Neural Dot Net Articles 1-11 Start Here[^]
|
|
|
|
|
I'm pretty sure that there's an article or two here on CP that shows how to do this (maybe as just a side-light to the real article, though).
John
"We want to be alone when we hear too many words and we feel alone when it has been a while since anyone has spoken to us." Paul David Tripp -- War of Words
|
|
|
|
|
Steps:
1) Create a bitmap the size of the area to be painted.
2) Create a Graphics from the bitmap, using Graphics.FromImage.
3) Use the Graphics object's drawing functions to draw on the bitmap.
4) Use Graphics.DrawImageUnScaled to draw the bitmap onto the screen.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
Cheers I'll have a play with that when I get chance. The GDI+ stuff didn't take that long to get working so it's just out of curiosity to see if it works.
pseudonym67
Neural Dot Net Articles 1-11 Start Here[^]
|
|
|
|
|
I've always passed around a reference to an instance of a Logger in most apps that I've developed. There has to be a better way. What do you guys do? I'd like to make a single instance that can be used anywhere in the program. Should I be considering a Singleton design pattern.... ???
I don't need anything fancy. All I need to do is:
MyLogger.Log("Message");
Is it possible to make an instance global to a namespace?
|
|
|
|
|