|
This sounds like a case when you really want a MVVM type separation of concerns. The model is whether the 6 items are 'on'; that can be affected by several things, not just the UI, so putting logic about them in the UI is wrong.
You don't actually need a VM if the model is simple enough to bind to the view, so I think what you need from a code perspective is: a model class which implements INotifyPropertyChanged so you can bind it:
class Input {
private bool on;
public bool On {
get { return on; }
set { on = value; Notify("On"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void Notify(string property) {
var handler = PropertyChanged;
if(null != handler) handler(this, new PropertyChangedEventArgs(property));
}
}
class DataModel {
public IList<Input> Inputs { get; private set; }
public DataModel() {
Inputs = new List<Input>();
for(int i = 0; i < 6; i++) Inputs.Add(new Input());
}
}
Your UI should be given the data model and data bind to it; let's imagine check boxes for the sake of simplicity, but you can use anything that has a boolean property you can bind to, including custom controls:
class MainForm : Form {
public MainForm(DataModel dataModel) {
InitializeComponent();
for(int i = 0; i < 6; i++) {
(CheckBox)(checkBoxGroup.Controls[i]).DataBindings.Add("Checked", dataModel.Inputs[i], "On");
}
}
}
If you have to poll the serial port to find out the real state of these things, then that's unfortunate but it's okay. Just set the On property of the Inputs in the data model (make sure it's the same instance!) and the UI should update.
Don't bind actions to the buttons being clicked. Instead, subscribe to the property notification changes from the inputs. (Or, probably better, have the data model do that and fire a custom event instead.)
|
|
|
|
|
Thanks Bob, your implementation of the processing of the digital inputs is really very efficient using notify. The 6 digital inputs (on/off) can only be on one at a time and they have to be treated as real push-buttons, and in fact, they have to simply replace the 6 push-buttons in the various forms (main and nested child forms) that are clicked with the mouse and are used when the enclosure that contains the pc is closed and then the 'push-buttons' should be operated by the 6 digital inputs instead of the mouse or push-screen. My main problem is how to communicate efficiently the status of the digital input to the opened child forms that can have also many levels of nesting. As you know the thread of a form sleep since it doesn't receive a winform event pushed in its dispatcher pump so I need the best and simple way to concatenate the series of event and relative delegate from child forms to main, but overall from main to childs and from main to children of the childs. The thread that reads the digital inputs is one and this communicate the state of the inputs to the mainForm.
|
|
|
|
|
You can data bind an instance of this Input class to multiple forms. Data binding to something which implements INPC will result in that control being updated when the event's fired.
I think I might have not understood what the 'inputs' are though. I thought you were reading the state of pins on a serial cable or something similar where on and off were both relevant; it seems like you actually want to fire the same handlers when they go to 'on' as when buttons are clicked, and 'off' doesn't matter.
In that case, you should put your processing code in a single place (you can call that class App or StateManager or Processor or other similarly generic things), and have the event handlers on your many forms do nothing except call methods on this central class. This is a classic 3 tier architecture (separation of view, logic and data), except that your data model is probably quite minimal. You can then attach event handlers to whatever mechanism you use to notify changes to the state of the external inputs, which call the same methods. If you need a UI cue to be reflected back to forms, use data binding to classes in the data model which implement INPC.
|
|
|
|
|
Thanks Bob, I wrote my application in perfect agreement with your general information and in fact the application now runs perfectly and quite fast. I get input from a digital serial interface that uses the Modbus protocol. My class that reads data from the bus is named CommInterface and it is the interface that sends data to the mainForm which then makes them available to all the other various children generating the appropriate events (6 of these are treated as mouse clicks on buttons to operate the forms). I have sent a small part of my code in one of my previous answers. Thanks so much for your kindness Bob and to all other peaple of code project. I have used for many years Visual C++ and now I'm switching to using C#, but this is not as easy as many would like to believe.
modified 24-Mar-14 4:36am.
|
|
|
|
|
Hi,
I want to convert HTML content to rtf format or save this html content as .rtf file . I already done this with the help of interop dll. But it will take large time for conversion , if we host it in a server. Is there any free dll for HTML to RTF conversion.Please help if anybody knows it..
Sample HTML Content..
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=Generator content="Microsoft Word 12 (filtered)">
<style>
<!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:serif;
panose-1 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin-top:0in;
margin-right:0in;
margin-bottom:10.0pt;
margin-left:0in;
line-height:115%;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
.MsoPapDefault
{margin-bottom:10.0pt;
line-height:115%;}
<a href="/Members/page">@page</a> WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
-->
</style>
</head>
<body lang=EN-US>
<div class=WordSection1>
<div>
<div>
<p class=MsoNormal align=right style='margin-top:5.0pt;margin-right:0in;
margin-bottom:5.0pt;margin-left:0in;text-align:right;text-autospace:none'><span
style='font-size:54.0pt;line-height:115%;font-family:"serif","serif";
color:black'>TEST</span><span style='font-size:19.5pt;line-height:115%;
font-family:"serif","serif";color:black'> TEST CONTENT</span></p>
<p class=MsoNormal align=right style='margin-top:5.0pt;margin-right:0in;
margin-bottom:5.0pt;margin-left:0in;text-align:right;text-autospace:none'><span
style='font-size:19.5pt;line-height:115%;font-family:"serif","serif";
color:#FF9900'> </span></p>
<p class=MsoNormal align=right style='margin-top:5.0pt;margin-right:0in;
margin-bottom:5.0pt;margin-left:.5in;text-align:right;text-autospace:none'><span
style='font-size:19.5pt;line-height:115%;font-family:"serif","serif";
color:#FF9900'>1. TEST CONTENT:</span></p>
<p class=MsoNormal align=right style='margin-top:5.0pt;margin-right:0in;
margin-bottom:5.0pt;margin-left:.5in;text-align:right;text-autospace:none'><span
style='font-size:19.5pt;line-height:115%;font-family:"serif","serif";
color:#FF9900'>2.  TEST CONTENT.</span></p>
<p class=MsoNormal align=right style='margin-top:5.0pt;margin-right:0in;
margin-bottom:5.0pt;margin-left:.5in;text-align:right;text-autospace:none'><span
style='font-size:19.5pt;line-height:115%;font-family:"serif","serif";
color:#FF9900'>3. TEST CONTENT.</span></p>
<p class=MsoNormal align=right style='margin-top:5.0pt;margin-right:0in;
margin-bottom:5.0pt;margin-left:.5in;text-align:right;text-autospace:none'><span
style='font-size:19.5pt;line-height:115%;font-family:"serif","serif";
color:#FF9900'>4. TEST CONTENT.</span></p>
<p class=MsoNormal align=right style='margin-top:5.0pt;margin-right:0in;
margin-bottom:5.0pt;margin-left:.5in;text-align:right;text-autospace:none'><span
style='font-size:19.5pt;line-height:115%;font-family:"serif","serif";
color:#FF9900'>5. TEST CONTENT.</span></p>
<p class=MsoNormal align=right style='margin-top:5.0pt;margin-right:0in;
margin-bottom:5.0pt;margin-left:.5in;text-align:right;text-autospace:none'><span
style='font-size:19.5pt;line-height:115%;font-family:"serif","serif";
color:#FF9900'>6. TEST CONTENT.</span></p>
<p class=MsoNormal align=right style='margin-top:5.0pt;margin-right:0in;
margin-bottom:5.0pt;margin-left:.5in;text-align:right;text-autospace:none'><span
style='font-size:19.5pt;line-height:115%;font-family:"serif","serif";
color:#FF9900'>8. TEST CONTENT.</span></p>
<p class=MsoNormal align=right style='margin-top:5.0pt;margin-right:0in;
margin-bottom:5.0pt;margin-left:.5in;text-align:right;text-autospace:none'><span
style='font-size:19.5pt;line-height:115%;font-family:"serif","serif";
color:red'>HELO</span></p>
</div>
</div>
</div>
</body>
</html>
Thanks in Advance
|
|
|
|
|
A free, non-interop version can be found here[^], but I don't think that it'll be faster. Another alternative is described in this[^] CodeProject article, using XSL. Third alternative comes from Microsoft and uses WPF[^].
If these three do not fit, then you could search for a webservice that offers this functionality; again, that's a slower option, as it means one has to transmit the data before on can transform it.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Greetings,
I am looking for a way to transform, and in particular rotate, the entire picturebox and not just the image inside of it.
Ig you add a picture to a Word or Excel document the picture is inserted and has grips around it and a rotate handle at the top.
How can I replicate this in a c# windows form?
Thanks,
Ron Wensley
|
|
|
|
|
Rotating the image is quite easy.
Rotating the PictureBox control is a huge pain in the ass. Chances are REALLY good you don't even need the PictureBox control at all.
Stuff like this is easily done in WPF. Not very easy in WinForms.
|
|
|
|
|
Dave...I see that rotate controls in WPF is very easy. My problem is that I am way down the road in the WinForm app and it uses several custom controls that are not compatible with WPF.
|
|
|
|
|
You're better off skipping the PictureBox and just painting on the surface that's holding your stuff.
The reason being is you'd have to paint the PictureBox yourself anyway to get it to rotate, so skip it and just paint the rotated image.
The underlying Windows controls don't support being rotated.
|
|
|
|
|
I'm trying to capture the output of a Dell executable command, racadm.exe. I can go to the cmd prompt and type in "racadm.exe getsysinfo" and get an output, but when I try to do this programmatically, it is not working. I can also programmatically use the code block below to get a directory listing, but with this specific exe, I cannot capture the standardoutput.
Any ideas on what I'm doing wrong?
System.Diagnostics.Process si = new System.Diagnostics.Process();
si.StartInfo.WorkingDirectory = "c:\\";
si.StartInfo.UseShellExecute = false;
si.StartInfo.FileName = "cmd.exe";
si.StartInfo.Arguments = "/c" + " racadm.exe" + " getsysinfo";
si.StartInfo.CreateNoWindow = true;
si.StartInfo.RedirectStandardInput = true;
si.StartInfo.RedirectStandardOutput = true;
si.StartInfo.RedirectStandardError = true;
si.Start();
string output = si.StandardOutput.ReadToEnd();
si.Close();
MessageBox.Show(output);
|
|
|
|
|
Try executing the process directly, rather than piping it through the command prompt.
si.StartInfo.FileName = "racadm.exe";
si.StartInfo.Arguments = "getsysinfo";
Also, you might want to wait for the process to exit before closing it:
si.Start();
si.WaitForExit();
string output = si.StandardOutput.ReadToEnd();
si.Close();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi, thanks for the reply.
I tried your suggestions and string output is empty, just as it is with the first code block?
Have you any other ideas?
|
|
|
|
|
Try using the full path to racadm.exe , rather than leaving cmd to find it.
|
|
|
|
|
Hi, I have tried the full path as well as relying on the environment variable as I did in my example, either way string output is blank?
Thanks for replying, have you any other ideas?
|
|
|
|
|
turbosupramk3 wrote: have you any other ideas? No, but having had a quick look at the documentation I suspect this may not work, owing to the type of program it is. You may need to talk to Dell about it.
|
|
|
|
|
turbosupramk3 wrote: I'm trying to capture the output of a Dell executable command The most simple way is to redirect the output to a file. From a console;
C:\> racadm getsysinfo > "C:\myoutput.txt"
turbosupramk3 wrote: // this works below The only difference between the code-blocks are the arguments you're passing. Means that the redirect works, and that something else is wrong.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Agreed, which is why I posted the topic.
I tried:
racadm getsysinfo > "C:\myoutput.txt"
in the command prompt and produced an empty text file. I think it is the way racadm is written, I'm just trying to get help to figure out a work around?
Thanks for replying.
|
|
|
|
|
turbosupramk3 wrote: Agreed, which is why I posted the topic. That means that the code is correct. Changing it will not help.
turbosupramk3 wrote: in the command prompt and produced an empty text file. That means that there was no output. The code is not only correct, it is showing the correct result.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
There is an output, I just don't believe that the racadm output method is the same as the dir command which I why I was trying to figure out a work around and referenced that the exe might have not been written well.
I don't have the output here at home, but in the command window it will output parameters and values or it will output an error, it does not return empty in the command window.
If the command window can receive an output, so can my code, I'm just not sure how to catch it.
|
|
|
|
|
turbosupramk3 wrote: There is an output The computer proved that nothing is written to stdout. The command on the console does what you're trying to do from code, and the results will be the same.
turbosupramk3 wrote: I don't have the output here at home, but in the command window it will output parameters and values or it will output an error, it does not return empty in the command window. Instead of reading "StandardOutput", try reading from "StandardError".
turbosupramk3 wrote: the exe might have not been written well. Writing things to stdout is something that's shown in each Hello-World example. It's default behaviour that's hard to change.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Try reading stderr as well as (or instead of) stdout.
|
|
|
|
|
Hi Bob,
I did try that and still received an empty output
|
|
|
|
|
I want divide the data of my datagrid in pages. For example I have 30 rows I would have 3 pages of 10 rows then have 2 buttons, one for "next page" and one for "previous page".
|
|
|
|
|
Im have little or no experience with WPF but this is something that I got from Googling
Paging in Datagrid WPF[^]
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|