|
The code :
protected override void OnKeyPress(KeyPressEventArgs e)
{
string strBefore = Text;
if (char.IsDigit(e.KeyChar))
{
....
}
}
I'm not sure... But try It out.....My compiler is not working.....
Try Using This code :
protected override void OnKeyPress(KeyPressEventArgs e)
{
string strBefore = Text;
if (char.IsDigit(e.KeyInt))
{
// The char Data Type gives u the problem or error
// The int data type has to give only the digit as input....
}
}
|
|
|
|
|
PRAVEEN@IT2008 wrote: I'm not sure... But try It out.....My compiler is not working...
If you don't have access to a compiler, you should at least take a look at the documentation intead of just guessing wildly...
There is no KeyInt property in the class.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I have the following code. I've install the service, and start it. I want to create a file 'c:\test.txt' using the timer. But the file still can't be created. I've check for Application error at windows 'event viewer', but have not find any.
using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
namespace WindowsService
{
class WindowsService : ServiceBase
{
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;
public WindowsService()
{
this.ServiceName = "G-ERP Auto backup";
this.EventLog.Source = "G-ERP Auto backup";
this.EventLog.Log = "Application";
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;
InitializeComponent();
if (!EventLog.SourceExists("G-ERP Auto backup"))
EventLog.CreateEventSource("G-ERP Auto backup", "Application");
}
static void Main()
{
ServiceBase.Run(new WindowsService());
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
protected override void OnStart(string[] args)
{
this.timer1.Enabled = true;
base.OnStart(args);
}
protected override void OnStop()
{
this.timer1.Enabled = false;
base.OnStop();
}
protected override void OnPause()
{
this.timer1.Enabled = false;
base.OnPause();
}
protected override void OnContinue()
{
this.timer1.Enabled = true;
base.OnContinue();
}
protected override void OnShutdown()
{
base.OnShutdown();
}
protected override void OnCustomCommand(int command)
{
base.OnCustomCommand(command);
}
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
return base.OnPowerEvent(powerStatus);
}
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
base.OnSessionChange(changeDescription);
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}
private void timer1_Tick(object sender, EventArgs e)
{
File.Copy(@"c:\YServer.txt", @"c:\test.txt");
if (!File.Exists(@"c:\YServer.txt"))
{
File.Copy(@"c:\YServer.txt", @"c:\test.txt");
}
}
}
}
|
|
|
|
|
Yulianto. wrote:
File.Copy(@"c:\YServer.txt", @"c:\test.txt");
if (!File.Exists(@"c:\YServer.txt"))
{
File.Copy(@"c:\YServer.txt", @"c:\test.txt");
}
File.Copy(@"c:\YServer.txt", @"c:\test.txt");
if (!File.Exists(@"c:\YServer.txt"))
{
File.Copy(@"c:\test.txt",@"c:\YServer.txt");
}
Check the difference on the bold line
Bob
Ashfield Consultants Ltd
|
|
|
|
|
Thanks. But i did not solve the problem
|
|
|
|
|
Hi,
I made this code based on some code i made in delphi, but it always fires the wrong keys. The bytes are right (I debugged and I saw its ok)
public static void SetActiveControlText(string text)
{
byte[] buffer = Encoding.ASCII.GetBytes(text);
for (int i = 0; i < buffer.Length; i++)
{
keybd_event(buffer[i], 0x45, 0x01, 0);
keybd_event(buffer[i], 0x45, 0x01 | 0x02, 0);
}
}
Any ideas what could possibily be wrong? the text I'm sending is "dirso" and the current application (notepad) start wrint "491/-1/-1" Ctrl+R and so on.
Thanks,
Dirso.
|
|
|
|
|
Hi!
Take a look at the documentation for keybd_event:
http://msdn.microsoft.com/en-us/library/ms646304(VS.85).aspx[^]
You're always giving 0x45 as second parameter (hardcoded - yuck!) instead of the scancode and the third parameter also doesn't seem right. Read the documentation and correct your code appropriately.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Hi,
I still can't find my error. When I first did it, it was in Delphi and it worked fine and also every sample I see looks like this.
Code in Delphi:
procedure TForm1.EnviaChar(c: char);
begin
keybd_event(ord(c),0,0,0);
keybd_event(ord(c),0,KEYEVENTF_KEYUP,0);
end;
I used the 0x45 because the samples have it, but I also tried "0" as the second parameter. and KEYEVENTF_KEYUP is 0x02
I hope you cn help me a little more.
Thanks,
Dirso
|
|
|
|
|
0x45 is used in the sample because it's the scancode for NUMLOCK. Don't just copy&paste samples - try to understand them and modify them according to your needs.
You should take a look at MapVirtualKey() to convert a virtual key into a scancode.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Hi,
I actually thought that parameter didn't matter, since I zeroed it in Delphi and it worked like a charm. I also tried to use 0x45, since 0 didn't work in C# and I found a few samples sending text with 0x45 (not just NUM_LOCK).
Anyway, I did what you told me to and I still have the same result.
Here is my C# code (I can't see why it's not working)
private const int KEYEVENTF_KEYUP = 0x02;
private const int KEYEVENTF_EXTENDEDKEY = 0x01;
private const uint MAPVK_VK_TO_VSC = 0x00;
public static void SetActiveControlText(string text)
{
#if(WINDOWS)
byte[] buffer = Encoding.ASCII.GetBytes(text);
uint scanCode;
for (int i = 0; i < buffer.Length; i++)
{
scanCode = MapVirtualKey((uint)buffer[i], 0);
keybd_event(buffer[i], (byte)scanCode, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(buffer[i], (byte)scanCode, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
#else
throw new NotImplementedException()
#endif
}
And here is my Delphi (working very nice) code:
procedure TForm1.EnviaChar(c: char);
begin
keybd_event(ord(c),0,0,0);
keybd_event(ord(c),0,KEYEVENTF_KEYUP,0);
end;
Thanks again,
Dirso
|
|
|
|
|
Hi!
The basic call to keybd_event seems to be OK now, the problem lies in the the way you try to convert your keys to bytes. For example, if you want to send "a" using keybd_event, you'll have to give 41 as virtual key code (there's a complete list of VK codes on MSDN).
But you're using 97 (the ASCII code for "a"), so this cannot work.
Btw. why do you insist on using keybd_event? I suggested using SendKeys.SendWait() a while ago - did you try this?
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
That's what i was looking for!! I cross plataform solution!!
Thank you so much!
Dirso
|
|
|
|
|
I have the following code:
#region Make all menustrip visible
foreach (ToolStripMenuItem menuItem in menuStrip.Items)
{
menuItem.Visible = true;
foreach (ToolStripItem toolStripItem in menuItem.DropDownItems)
{
toolStripItem.Visible = true;
}
}
#endregion
Why can't I change the toolStripItem Visible to true?
I did not catch any VisibleChanged for the toolStripItem .
|
|
|
|
|
General suggestion for similar situation : try to trace the code,is the loop even activated? - where you call the code aren't you change the visibility after this code - check the parent's visibility more carefully - in the past I've seen some situation that VS did not recognize the changes, mostly changes by designer,...
|
|
|
|
|
Hi, I am a longtime embedded C programmer who has recently begun struggling with OOP
I'm writing code for a serial communication protocol; protocol messages can belong to 2 different types (commands and responses), which share several fields and methods that I decided to factor out in a common base class, more or less like the example below:
public abstract class Message
{
private byte _FieldA;
public byte FieldA
{
}
private byte _FieldB;
public byte FieldB
{
}
private byte _FieldC;
public byte FieldC
{
}
public byte[] CompileHeader()
{
}
}
public abstract class Command : Message
{
}
public abstract class Response : Message
{
}
public class MyCommand : Command
{
}
Message, Command and Response are abstract because they are not meant to be instantiated: it only makes sense to instantiate the concrete subclass (MyCommand).
The problem is: as you see, if the actual message is a Command or a Response, FieldB must be set to a different value. However, FieldB is also used by the base Message class so I cannot move it down the hierarchy.
I'd like FieldB to be set automatically to the appropriate value in my concrete classes, depending on the base class (e.g. if I inherit from Command I'd expect FieldB = 1).
How can I implement this behaviour in C#?
Thanks in advance to all of you gurus out there!
Andrea
|
|
|
|
|
In your abstract class Message class you can do this:
public byte FieldB
{
get { return 1; }
}
And in your abstract class Response something like this:
public new byte FieldB
{
get { return 2; }
}
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thanks for your reply Greeeg. My doubt is: by doing this you are moving some of the Command-related logic to the base Message class (setting FieldB to 1 should only compete to a Command).
Isn't there any other way to implement the desired behaviour without breaking the encapsulation?
|
|
|
|
|
Metal76 wrote: My doubt is: by doing this you are moving some of the Command-related logic to the base Message class
My bad. I actually meant to move
public new byte FieldB
{
get { return 1; }
}
to the Command class. Is this what you actually wanted to achieve?
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
The new keyword would be inappropriate here. It would be better to use virtual properties and override.
If you have:
Message message = new Command();
then you would expect message.FieldB to return the override from command. If you use the new keyword, this won't happen.
|
|
|
|
|
You can put (protected) constructors in the Command and Response class. The constructor in the MyCommand class will automatically call the constructor in the Command class if it's parameterless.
Additionally, you can add a constructor to the Message class that takes a value for the FieldB property. That way a Message instance can not be created without specifying a value for the property. The constructors in the Command and Response classes can call the base constructor to specify the value:
public abstract class Command : Message {
protected Command() : base(1) {}
}
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Another way could also be to use an abstract property for FieldB in the Message class and override them in the Command and Response classes:
public abstract class Message
{
public abstract byte FieldB
{
get;
}
}
public abstract class Command : Message
{
public override byte FieldB
{
get { return 1; }
}
}
public abstract class Response : Message
{
public override byte FieldB
{
get { return 2; }
}
}
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thank you very much for your replies, both approaches seem really promising.
Regards,
Andrea
|
|
|
|
|
Greeting,
I have a table with more than 500,000 rows in SQL server 2000.
I want to show some rows of the table with a query in the crystal report.
But it take time to show the result in the crystal report.
How can I reduce the time to show result in crystal report?
Thanks.
|
|
|
|
|
About how many of those 500,000 rows are coming up in the report? Hopefully not all of them
Member 2025176 wrote: How can I reduce the time to show result in crystal report?
Run the query analyzer and look for any bottlenecks.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Hello everyone
i am trying to make this code of ( production rule ) work in away that could be much flippable i.m. in the following code the program find the results for ( one attribute only which is play or not - in the example of weather and over cast - TRUE OR fAlSE ) so i want to make it like :
- the User enters the attributes he wants to make the process be on any of the other attributes like
temperature or humidity etc..
i hope anyone could help me
namespace ID3Algo
{
public class Attribute
{
ArrayList mValues;
string mName;
object mLabel;
public Attribute(string name, string[] values)
{
mName = name;
mValues = new ArrayList(values);
mValues.Sort();
}
public Attribute(object Label)
{
mLabel = Label;
mName = string.Empty;
mValues = null;
}
public string AttributeName
{
get
{
return mName;
}
}
public string[] values
{
get
{
if (mValues != null)
return (string[])mValues.ToArray(typeof(string));
else
return null;
}
}
public bool isValidValue(string value)
{
return indexValue(value) >= 0;
}
public int indexValue(string value)
{
if (mValues != null)
return mValues.BinarySearch(value);
else
return -1;
}
public override string ToString()
{
if (mName != string.Empty)
{
return mName;
}
else
{
return mLabel.ToString();
}
}
}
public class TreeNode
{
private ArrayList mChilds = null;
private Attribute mAttribute;
public TreeNode(Attribute attribute)
{
if (attribute.values != null)
{
mChilds = new ArrayList(attribute.values.Length);
for (int i = 0; i < attribute.values.Length; i++)
mChilds.Add(null);
}
else
{
mChilds = new ArrayList(1);
mChilds.Add(null);
}
mAttribute = attribute;
}
public void AddTreeNode(TreeNode treeNode, string ValueName)
{
int index = mAttribute.indexValue(ValueName);
mChilds[index] = treeNode;
}
public int totalChilds
{
get
{
return mChilds.Count;
}
}
public TreeNode getChild(int index)
{
return (TreeNode)mChilds[index];
}
public Attribute attribute
{
get
{
return mAttribute;
}
}
public TreeNode getChildByBranchName(string branchName)
{
int index = mAttribute.indexValue(branchName);
return (TreeNode)mChilds[index];
}
}
public class DecisionTreeID3
{
private DataTable mSamples;
private int mTotalPositives = 0;
private int mTotal = 0;
private string mTargetAttribute = "result";
private double mEntropySet = 0.0;
private int countTotalPositives(DataTable samples)
{
int result = 0;
foreach (DataRow aRow in samples.Rows)
{
if ((bool)aRow[mTargetAttribute] == true)
result++;
}
return result;
}
private double calcEntropy(int positives, int negatives)
{
int total = positives + negatives;
double ratioPositive = (double)positives / total;
double ratioNegative = (double)negatives / total;
if (ratioPositive != 0)
ratioPositive = -(ratioPositive) * System.Math.Log(ratioPositive, 2);
if (ratioNegative != 0)
ratioNegative = -(ratioNegative) * System.Math.Log(ratioNegative, 2);
double result = ratioPositive + ratioNegative;
return result;
}
private void getValuesToAttribute(DataTable samples, Attribute attribute, string value, out int positives, out int negatives)
{
positives = 0;
negatives = 0;
foreach (DataRow aRow in samples.Rows)
{
if (((string)aRow[attribute.AttributeName] == value))
if ((bool)aRow[mTargetAttribute] == true)
positives++;
else
negatives++;
}
}
private double gain(DataTable samples, Attribute attribute)
{
string[] values = attribute.values;
double sum = 0.0;
for (int i = 0; i < values.Length; i++)
{
int positives, negatives;
positives = negatives = 0;
getValuesToAttribute(samples, attribute, values[i], out positives, out negatives);
double entropy = calcEntropy(positives, negatives);
sum += -(double)(positives + negatives) / mTotal * entropy;
}
return mEntropySet + sum;
}
private Attribute getBestAttribute(DataTable samples, Attribute[] attributes)
{
double maxGain = 0.0;
Attribute result = null;
foreach (Attribute attribute in attributes)
{
double aux = gain(samples, attribute);
if (aux > maxGain)
{
maxGain = aux;
result = attribute;
}
}
return result;
}
private bool allSamplesPositives(DataTable samples, string targetAttribute)
{
foreach (DataRow row in samples.Rows)
{
if ((bool)row[targetAttribute] == false)
return false;
}
return true;
}
private bool allSamplesNegatives(DataTable samples, string targetAttribute)
{
foreach (DataRow row in samples.Rows)
{
if ((bool)row[targetAttribute] == true)
return false;
}
return true;
}
private ArrayList getDistinctValues(DataTable samples, string targetAttribute)
{
ArrayList distinctValues = new ArrayList(samples.Rows.Count);
foreach (DataRow row in samples.Rows)
{
if (distinctValues.IndexOf(row[targetAttribute]) == -1)
distinctValues.Add(row[targetAttribute]);
}
return distinctValues;
}
private object getMostCommonValue(DataTable samples, string targetAttribute)
{
ArrayList distinctValues = getDistinctValues(samples, targetAttribute);
int[] count = new int[distinctValues.Count];
foreach (DataRow row in samples.Rows)
{
int index = distinctValues.IndexOf(row[targetAttribute]);
count[index]++;
}
int MaxIndex = 0;
int MaxCount = 0;
for (int i = 0; i < count.Length; i++)
{
if (count[i] > MaxCount)
{
MaxCount = count[i];
MaxIndex = i;
}
}
return distinctValues[MaxIndex];
}
private TreeNode internalMountTree(DataTable samples, string targetAttribute, Attribute[] attributes)
{
if (allSamplesPositives(samples, targetAttribute) == true)
return new TreeNode(new Attribute(true));
if (allSamplesNegatives(samples, targetAttribute) == true)
return new TreeNode(new Attribute(false));
if (attributes.Length == 0)
return new TreeNode(new Attribute(getMostCommonValue(samples, targetAttribute)));
mTotal = samples.Rows.Count;
mTargetAttribute = targetAttribute;
mTotalPositives = countTotalPositives(samples);
mEntropySet = calcEntropy(mTotalPositives, mTotal - mTotalPositives);
Attribute bestAttribute = getBestAttribute(samples, attributes);
TreeNode root = new TreeNode(bestAttribute);
DataTable aSample = samples.Clone();
foreach (string value in bestAttribute.values)
{
aSample.Rows.Clear();
DataRow[] rows = samples.Select(bestAttribute.AttributeName + " = " + "'" + value + "'");
foreach (DataRow row in rows)
{
aSample.Rows.Add(row.ItemArray);
}
ArrayList aAttributes = new ArrayList(attributes.Length - 1);
for (int i = 0; i < attributes.Length; i++)
{
if (attributes[i].AttributeName != bestAttribute.AttributeName)
aAttributes.Add(attributes[i]);
}
if (aSample.Rows.Count == 0)
{
return new TreeNode(new Attribute(getMostCommonValue(aSample, targetAttribute)));
}
else
{
DecisionTreeID3 dc3 = new DecisionTreeID3();
TreeNode ChildNode = dc3.mountTree(aSample, targetAttribute, (Attribute[])aAttributes.ToArray(typeof(Attribute)));
root.AddTreeNode(ChildNode, value);
}
}
return root;
}
public TreeNode mountTree(DataTable samples, string targetAttribute, Attribute[] attributes)
{
mSamples = samples;
return internalMountTree(mSamples, targetAttribute, attributes);
}
}
class ID3Sample
{
public static void printNode(TreeNode root, string tabs)
{
Console.WriteLine(tabs + '|' + root.attribute + '|');
if (root.attribute.values != null)
{
for (int i = 0; i < root.attribute.values.Length; i++)
{
Console.WriteLine(tabs + "\t" + "<" + root.attribute.values[i] + ">");
TreeNode childNode = root.getChildByBranchName(root.attribute.values[i]);
printNode(childNode, "\t" + tabs);
}
}
}
static DataTable getDataTable()
{
DataTable result = new DataTable("samples");
DataColumn column = result.Columns.Add("weather");
column.DataType = typeof(string);
column = result.Columns.Add("temprature");
column.DataType = typeof(string);
column = result.Columns.Add("humidity");
column.DataType = typeof(string);
column = result.Columns.Add("windy");
column.DataType = typeof(string);
column = result.Columns.Add("result");
column.DataType = typeof(bool);
result.Rows.Add(new object[] { "sol", "alta", "alta", "nao", false });
result.Rows.Add(new object[] { "sol", "alta", "alta", "sim", false });
result.Rows.Add(new object[] { "nublado", "alta", "alta", "nao", true });
result.Rows.Add(new object[] { "hot", "alta", "alta", "nao", true });
result.Rows.Add(new object[] { "hot", "baixa", "normal", "nao", true });
result.Rows.Add(new object[] { "hot", "baixa", "normal", "sim", false });
result.Rows.Add(new object[] { "nublado", "baixa", "normal", "sim", true });
result.Rows.Add(new object[] { "sol", "suave", "alta", "nao", false });
result.Rows.Add(new object[] { "sol", "baixa", "normal", "nao", true });
result.Rows.Add(new object[] { "hot", "suave", "normal", "nao", true });
result.Rows.Add(new object[] { "sol", "suave", "normal", "nao", true });
result.Rows.Add(new object[] { "nublado", "suave", "alta", "sim", true });
result.Rows.Add(new object[] { "nublado", "alta", "normal", "nao", true });
result.Rows.Add(new object[] { "hot", "suave", "alta", "sim", false });
return result;
}
[STAThread]
static void Main(string[] args)
{
Attribute Height = new Attribute("Height", new string[] { "short", "tall" });
Attribute Hair = new Attribute("Hair", new string[] { "blond", "red", "dark" });
Attribute Eye = new Attribute("Eye", new string[] { "blue", "brown" });
Attribute[] attributes = new Attribute[] { Height, Hair, Eye };
DataTable samples = getDataTable();
DecisionTreeID3 id3 = new DecisionTreeID3();
TreeNode root = id3.mountTree(samples, "result", attributes);
printNode(root, "");
Console.ReadLine();
}
|
|
|
|
|