|
Post your question in the WCF forum.
|
|
|
|
|
Hi everyone,
I have a problem with my project.
I must to do a calendar with days and hours.
On the first's datagridview row I want to write this hour (07:00:00).
At this hour I want to add 30 minutes automatically.
I want that my output is like this:
07:00:00
07:30:00
08:00:00
08:30:00
...
But, it didn't works correctly.
This is my code:
TimeSpan ora = TimeSpan.FromHours(7);
TimeSpan mezzora = TimeSpan.FromMinutes(0);
TimeSpan orario;
for (int i = 0; i < 20; i++)
{
dataGridView2.Rows.Add("");
orario = ora + mezzora;
dataGridView2[0, i].Value = orario;
mezzora = TimeSpan.FromMinutes(30);
}
In output I have this:
07:00:00
07:30:00
07:30:00
07:30:00
Can you help me?
|
|
|
|
|
The issue that you have is that you never allocate the new time increment to ora, and mezzora never changes. Fortunately, you can update mezzora with no problem using the following:
mezzora = TimeSpan.FromMinutes(30 * (i + 1)); What you're doing here is multiplying the minutes by the increment of i (you need to add 1 to the count because you have a zero offset here.
|
|
|
|
|
It works !
Thanks so much Pete O'Hanlon !
|
|
|
|
|
You're welcome. I'm glad that I could help.
|
|
|
|
|
I have a datagrid and I set the ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible" in file XAML. The Horizontal it works but the Vertical not works.
I tryed to drag the bottom of the window upward and the vertical scrollbar it doesn't work.
|
|
|
|
|
Can we see your XAML code?
|
|
|
|
|
Please use the WPF forum for questions on WPF.
|
|
|
|
|
Sorry!!! I move the question in WPF forum!
|
|
|
|
|
How could I change my code to use in every form. At the moment it only works on the main one and I need to send the main form's instance.
Could I change my code so that it can be used in every form?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SefelecTester
{
public class WC
{
private MainWin _main;
public WC(MainWin _main)
{
this._main=_main;
}
public void ConWrite(string text, int select=0)
{
Console.WriteLine(text);
if (select>0)
{
if (select == 1)
{
InvokeC.SynchronizedInvoke(_main, () => _main.OutputConsole.Items.Add(text));
InvokeC.SynchronizedInvoke(_main, () => _main.OutputConsole.SelectedIndex = -1);
}
else if (select == 2)
{
InvokeC.SynchronizedInvoke(_main, () => _main.ErrorConsole.Items.Add(text));
InvokeC.SynchronizedInvoke(_main, () => _main.ErrorConsole.SelectedIndex = -1);
}
}
}
}
}
modified 21-Mar-14 7:45am.
|
|
|
|
|
If you want it to be available to all forms, and you are using windows forms, you could always change the constructor to accept a Form instead (and store this in a field of type Form obviously):
public class WC
{
private Form _form;
public WC(Form form)
{
_form = form;
}
}
|
|
|
|
|
Pete's right - but the other way to do it is to derive your new class from Form, and then derive your forms from your new class. That way, you don't need to construct an instance of your class separately, and the code you add is available to operate on the form immediately, without passing a form reference to it.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
There is an implicit interface here: this code can never work in a form which doesn't have an OutputConsole and an ErrorConsole, as well as working with InvokeC (I don't know what that requires but presumably BeginInvoke).
So the most general you can get would be to make the thing passed to the constructor be an instance of that interface:
interface IMessageLogger {
void LogToOutput(string message);
void LogToError(string message);
void InvokeAsync(Action<string> method, string method);
}
class WC {
private IMessageLogger logger;
public WC(IMessageLogger logger) { this.logger = logger; }
public void ConWrite(string text, int target = 0) {
Console.WriteLine(text);
switch(target) {
case 1: logger.InvokeAsync(logger.LogToOutput, text); break;
case 2: logger.InvokeAsync(logger.LogToError, text); break;
}
}
There's a design decision here about whether that asynchroneity should be hidden inside the implementation of LogToOutput; I'd say it probably should, but I wanted to leave this code recognisably derived from yours.
Your MainWin would then need to implement IMessageLogger (writing items to the OutputConsole and ErrorConsole in the LogToOutput and LogToError methods). InvokeAsync would be a thin cover on BeginInvoke or Invoke:
public void InvokeAsync(Action<string> method, string text) {
BeginInvoke(delegate() { method(text) });
}
|
|
|
|
|
Good idea but I couldn't get interface to work.
Managed to solve it with events.
|
|
|
|
|
Message Removed
modified 20-Mar-14 10:11am.
|
|
|
|
|
Hi
how to use Response.Clear() in c#
|
|
|
|
|
Context?
Regards,
Rob Philpott.
|
|
|
|
|
Hi,
I have a pivot table in excel sheet 1. I need to write a C# code to copy and paste whole pivot table in sheet 2 of same excel file. While copying, I need to expand all columns data also.
Please help me in doing this.
Thanks
|
|
|
|
|
rajnknit07 wrote: Please help me in doing this. Help with what exactly?
As is, you only specified what the finished solution should do. Divide your problem in smaller things, like reading an Excel file using C#, writing a file, and then move on to modifying data.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi all,
I would like to know what is the best way to manage the passage of events between WinForms.
I have to click on the MainForm 6 buttons with either the mouse or using 6 corresponding digital inputs.
Obviously with the mouse there are no problems, but to use the digital inputs as mouse-clicks, I have written a class that runs a background timer-thread that reads the digital inputs and uses delegates to notify the MainForm on the changes of the digital inputs (from 0 to 1) so that his handlers can perform the function:
key-x.PerformClick()
to call the method linked to the x button.
My problem is: how can I do it as fast, clean and safe using MainForm modal-forms childs ? And overall, what about with the children of the children that can be up to the 4-th level depth ? The 6 buttons have form by form always to perform different operations.
OK, I can use a chain of delegates among the various modal-form-childs but it is ugly and not so simple, fast and very safe to realize.
Any idea ?
modified 24-Mar-14 12:20pm.
|
|
|
|
|
What do you mean with a "digital input"? I'd assume you'd loop through your forms, and hook up any buttons using reflection.
Am I right that you want to see an event raised in the mainform when one of the buttons on the child-forms is clicked?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thank you for the answer Eddy, I have 6 on/off digital-inputs pins on an electronic interface connected to the pc that 'clicks' the 6 buttons on the frames as a mouse. Pin 1 for button 1, . . . pin 6 for button 6 just like a mouse. The mainform received the 6 inputs 'buttons' states from a communicationInterface timer-thread that reads the input pins and I need to send these input states also to the last open child-form that many times it can be a child of a child of a child of the mainform.
|
|
|
|
|
Mario 56 wrote: The mainform received the 6 inputs 'buttons' states from a communicationInterface timer-thread that reads the input pins and I need to send these input states also to the last open child-form that many times it can be a
child of a child of a child of the mainform. So, there's never more than 6 buttons? Acting on one of the six buttons of the currently active form?
You don't need to send those states back and forth between the forms; put them in a separate class and share it between the forms.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
No Eddy, I cannot simply share the inputs using a separate class, WinForm threads can be awakened only using events or with a timer loop to read the digital inputs in every opened child form. I have solved the problem using delegate:
public delegate void Key1();
public Key1 extKey1Delegate;
public delegate void Key2();
public Key2 extKey2Delegate;
public delegate void Key3();
public Key3 extKey3Delegate;
public delegate void Key4();
public Key4 extKey4Delegate;
public delegate void Key5();
public Key5 extKey5Delegate;
public delegate void Key6();
public Key6 extKey6Delegate;
public mainForm
{
extKey1Delegate = new Key1(Key1Method);
extKey2Delegate = new Key2(Key2Method);
extKey3Delegate = new Key3(Key3Method);
extKey4Delegate = new Key4(Key4Method);
extKey5Delegate = new Key5(Key5Method);
extKey6Delegate = new Key6(Key6Method);
}
and in the commInterface class that read the digital inputs:
_interfaceInputStatus = modbus.ReadInputs(1);
if (_interfaceInputStatus[(int)keyButtons.KEY1]) { extFormControl.Invoke(extFormControl.extKey1Delegate); }
if (_interfaceInputStatus[(int)keyButtons.KEY2]) { extFormControl.Invoke(extFormControl.extKey2Delegate); }
if (_interfaceInputStatus[(int)keyButtons.KEY3]) { extFormControl.Invoke(extFormControl.extKey3Delegate); }
if (_interfaceInputStatus[(int)keyButtons.KEY4]) { extFormControl.Invoke(extFormControl.extKey4Delegate); }
if (_interfaceInputStatus[(int)keyButtons.KEY5]) { extFormControl.Invoke(extFormControl.extKey5Delegate); }
if (_interfaceInputStatus[(int)keyButtons.KEY6]) { extFormControl.Invoke(extFormControl.extKey6Delegate); }
and in the mainForm class using them with the code:
private void key1_Click(object sender, EventArgs e)
{
. . . .
}
. . . . .
private void key6_Click(object sender, EventArgs e)
{
. . . .
}
void Key1Method()
{
if (mainFlag) key1.PerformClick();
else KeyCallback(1);
}
void Key2Method()
{
if (mainFlag) key2.PerformClick();
else KeyCallback(2);
}
void Key3Method()
{
if (mainFlag) key3.PerformClick();
else KeyCallback(3);
}
void Key4Method()
{
if (mainFlag) key4.PerformClick();
else KeyCallback(4);
}
void Key5Method()
{
if (mainFlag) key5.PerformClick();
else KeyCallback(5);
}
void Key6Method()
{
if (mainFlag) key6.PerformClick();
else KeyCallback(6);
}
and then every opened child subscribe the event to all its children. obviously exists a table where is specified which method of which class should serve the input event.
I would be glad if someone would tell me what is the method to know which is the form currently in use so maybe I can avoid to use the table. Every form is modal.
Thank you
|
|
|
|
|
Mario 56 wrote: I would be glad if someone would tell me what is the method to know which is the form currently in use Form.ActiveForm[^]
"Gets the currently active form for this application."
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|