|
|
I tried that one, it does not work, plus it does not answer the question as it stands now.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
wetrivrrat wrote:
byte[] data = new byte[DATA_SIZE];
do i need to set the DATA_SIZE ? or is that automatic?
*sigh* Look, I have to ask you, when you read this stuff, do you consider what any of it means ? Your array will have a size, and the demo code is showing a variable to give the idea of the syntax. You need to create a byte array the size of the string. Here[^] is an article on converting a string to a byte[] and back.
wetrivrrat wrote:
it looks like i need to create a new class, then do i define the value of data in the class? or in my storage method?
What on earth for ? What makes you think you need to write a class ?
wetrivrrat wrote:
ONce that is all done, how do i read this data.. aka, how do i reverse this process?
Oh - I asked you before if you were comparing a stored hash to a new one. You cannot reverse a hash, that's one thing that makes it secure.
If you want something you can reverse, I'm not sure what algorithm to use. I would recommend retrieving a hash and comparing it to a hash of the value you expect, assuming that's what's going on here. If you're checking for a range, then you need to find another algorithm.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote:
*sigh*
You're not alone! I've been up since 6 somthing this morning going balls to the wall all day. I was sitting in a MS Word 1 intro class when I was writing to you about this(requirement for finacial aid for MCSE, don't ask) So between dealing with a teacher who didn't like me not paying attention, and having the fear of this deadline being bumped up a week I am not thinking too clearly.
that said i greatly appreciate your help thus far... and anything provided beyond this...
I have never had to implement any crypto in my apps, and this being the first i'm struggling to grasp the functions as i was dealing with the teacher...
What I need is the ablility to take today's date, hard code that to the app,
then install my app today, when it loads up the next time it checks to see if the date is the same or not, if it is it does set A of methods, if not it does set B of methods.
I am writing the date of install(installDate) to Isolated Storage thru a hashtable that
stores the info as a string(currently i'm too tired to remember why, and its too big a part of the app to disect it tonight(6hours to delivery) and reconstruct it.) then I compare that string to the date string hard coded in the app as well to today's date.
If a user found the iso-storage and changed the date to todays date then the program would re-install itself. I need to write the installDate to the iso-storage in a non-reconizable format that will then re-construct itself to the date of install for comparison.
I don't care if it is md5 or not, the reason i was looking for a way to convert the vb code was because it did what i needed it to, the problem is that I can't make it work in C#, and i'm not sure what needs to be done at this point to make it work. I tried the app from David Anton, but it has all kinds of errors that I haven't yet had the time/energy to debug, but i'll ask him about those.
If you know of a better way than this please enlighten me.... i'll post the code result from the converter David Anton suggested.
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
wetrivrrat wrote:
have never had to implement any crypto in my apps, and this being the first i'm struggling to grasp the functions as i was dealing with the teacher
OK, well, the first step is to remember that it's all still C#, the bits we're talking about now are pretty straightforward. You create an array of bytes and populate it. the example is showing you how to do that broadly, but copy and paste won't work, nor will you learn anything that way.
wetrivrrat wrote:
i'll post the code result from the converter David Anton suggested.
I already tried it, it's a disaster.
Do you really want to use code that you don't understand ?
Here it is, anyhow. This works, but I still think you're going about things the wrong way to be copying and pasting code you don't understand. I am just taking pity in this case because you're under the gun.
private string Crypt(string Text_Renamed, string Key)
{
int KeyLen = Key.Length;
string NewText = "";
int KeyPtr = 0;
for (int i = 0; (i < Text_Renamed.Length); i++)
{
short TextChr = Convert.ToInt16(Text_Renamed[i]);
short KeyChr = Convert.ToInt16(Key[KeyPtr]);
NewText += Convert.ToChar(TextChr ^ KeyChr);
KeyPtr = (KeyPtr+1) % KeyLen;
}
return NewText;
}
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
David, thanks for the direction to the app, here are the results for the project as a whole, enclosed are the results and the corresonding code.
I purposely did not omit anyting... sorry its so long...
I have higlighted the lines that are erroring.... I am currently working on debuggin this, but any assistance will be greatly apreciated!!!
remember this WAS working in vb after conversion from vb6 to vb.net.
1.\Form1.cs(269): No overload for method 'FileOpen' takes '3' arguments
2.\Form1.cs(270): The best overloaded method match for 'string.PadLeft(int)' has some invalid arguments
3.\Form1.cs(270): Argument '1': cannot convert from 'long' to 'int'
4.\Form1.cs(272): The best overloaded method match for 'Microsoft.VisualBasic.FileSystem.FileGet(int, ref System.ValueType, long)' has some invalid arguments
5.\Form1.cs(272): Argument '2': cannot convert from 'string' to 'ref System.ValueType'
6.\Form1.cs(293): No overload for method 'FileOpen' takes '3' arguments
7.\Form1.cs(331): Cannot implicitly convert type 'int' to 'short'
8.\Form1.cs(332): Cannot implicitly convert type 'int' to 'short'
using Microsoft.VisualBasic.Compatibility.VB6;
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
namespace Project1
{
internal class Form1 : System.Windows.Forms.Form
{
#region Windows Form Designer generated code
public Form1() : base()
{
if (m_vb6FormDefInstance == null)
{
if (m_InitializingDefInstance)
{
m_vb6FormDefInstance = this;
}
else
{
try
{
if (System.Reflection.Assembly.GetExecutingAssembly().EntryPoint.DeclaringType == this.GetType())
{
m_vb6FormDefInstance = this;
}
}
catch
{
}
}
}
InitializeComponent();
}
protected override void Dispose(bool Disposing)
{
if (Disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(Disposing);
}
private System.ComponentModel.IContainer components;
public System.Windows.Forms.ToolTip ToolTip1;
public System.Windows.Forms.Button cmdDecrypt;
public System.Windows.Forms.TextBox txtPath;
public System.Windows.Forms.TextBox txtSalt;
public System.Windows.Forms.TextBox txtText;
public System.Windows.Forms.Button cmdEncrypt;
public System.Windows.Forms.Label Label2;
public System.Windows.Forms.Label Label1;
[System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.components = new System.ComponentModel.Container();
this.ToolTip1 = new System.Windows.Forms.ToolTip(components);
this.ToolTip1.Active = true;
this.cmdDecrypt = new System.Windows.Forms.Button();
this.txtPath = new System.Windows.Forms.TextBox();
this.txtSalt = new System.Windows.Forms.TextBox();
this.txtText = new System.Windows.Forms.TextBox();
this.cmdEncrypt = new System.Windows.Forms.Button();
this.Label2 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.Text = "Form1";
this.ClientSize = new System.Drawing.Size(312, 213);
this.Location = new System.Drawing.Point(4, 23);
this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultLocation;
this.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Control;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
this.ControlBox = true;
this.Enabled = true;
this.KeyPreview = false;
this.MaximizeBox = true;
this.MinimizeBox = true;
this.Cursor = System.Windows.Forms.Cursors.Default;
this.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.ShowInTaskbar = true;
this.HelpButton = false;
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.Name = "Form1";
this.cmdDecrypt.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.cmdDecrypt.Text = "Decrypt";
this.cmdDecrypt.Size = new System.Drawing.Size(97, 33);
this.cmdDecrypt.Location = new System.Drawing.Point(112, 128);
this.cmdDecrypt.TabIndex = 6;
this.cmdDecrypt.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.cmdDecrypt.BackColor = System.Drawing.SystemColors.Control;
this.cmdDecrypt.CausesValidation = true;
this.cmdDecrypt.Enabled = true;
this.cmdDecrypt.ForeColor = System.Drawing.SystemColors.ControlText;
this.cmdDecrypt.Cursor = System.Windows.Forms.Cursors.Default;
this.cmdDecrypt.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.cmdDecrypt.TabStop = true;
this.cmdDecrypt.Name = "cmdDecrypt";
this.txtPath.AutoSize = false;
this.txtPath.Size = new System.Drawing.Size(265, 25);
this.txtPath.Location = new System.Drawing.Point(8, 96);
this.txtPath.TabIndex = 5;
this.txtPath.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.txtPath.AcceptsReturn = true;
this.txtPath.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
this.txtPath.BackColor = System.Drawing.SystemColors.Window;
this.txtPath.CausesValidation = true;
this.txtPath.Enabled = true;
this.txtPath.ForeColor = System.Drawing.SystemColors.WindowText;
this.txtPath.HideSelection = true;
this.txtPath.ReadOnly = false;
this.txtPath.MaxLength = 0;
this.txtPath.Cursor = System.Windows.Forms.Cursors.IBeam;
this.txtPath.Multiline = false;
this.txtPath.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.txtPath.ScrollBars = System.Windows.Forms.ScrollBars.None;
this.txtPath.TabStop = true;
this.txtPath.Visible = true;
this.txtPath.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.txtPath.Name = "txtPath";
this.txtSalt.AutoSize = false;
this.txtSalt.Size = new System.Drawing.Size(137, 25);
this.txtSalt.Location = new System.Drawing.Point(8, 56);
this.txtSalt.TabIndex = 2;
this.txtSalt.Text = "WetRi";
this.txtSalt.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.txtSalt.AcceptsReturn = true;
this.txtSalt.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
this.txtSalt.BackColor = System.Drawing.SystemColors.Window;
this.txtSalt.CausesValidation = true;
this.txtSalt.Enabled = true;
this.txtSalt.ForeColor = System.Drawing.SystemColors.WindowText;
this.txtSalt.HideSelection = true;
this.txtSalt.ReadOnly = false;
this.txtSalt.MaxLength = 0;
this.txtSalt.Cursor = System.Windows.Forms.Cursors.IBeam;
this.txtSalt.Multiline = false;
this.txtSalt.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.txtSalt.ScrollBars = System.Windows.Forms.ScrollBars.None;
this.txtSalt.TabStop = true;
this.txtSalt.Visible = true;
this.txtSalt.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.txtSalt.Name = "txtSalt";
this.txtText.AutoSize = false;
this.txtText.Size = new System.Drawing.Size(137, 25);
this.txtText.Location = new System.Drawing.Point(8, 16);
this.txtText.TabIndex = 1;
this.txtText.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.txtText.AcceptsReturn = true;
this.txtText.TextAlign = System.Windows.Forms.HorizontalAlignment.Left;
this.txtText.BackColor = System.Drawing.SystemColors.Window;
this.txtText.CausesValidation = true;
this.txtText.Enabled = true;
this.txtText.ForeColor = System.Drawing.SystemColors.WindowText;
this.txtText.HideSelection = true;
this.txtText.ReadOnly = false;
this.txtText.MaxLength = 0;
this.txtText.Cursor = System.Windows.Forms.Cursors.IBeam;
this.txtText.Multiline = false;
this.txtText.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.txtText.ScrollBars = System.Windows.Forms.ScrollBars.None;
this.txtText.TabStop = true;
this.txtText.Visible = true;
this.txtText.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.txtText.Name = "txtText";
this.cmdEncrypt.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.cmdEncrypt.Text = "Encrypt and Save";
this.cmdEncrypt.Size = new System.Drawing.Size(97, 33);
this.cmdEncrypt.Location = new System.Drawing.Point(8, 128);
this.cmdEncrypt.TabIndex = 0;
this.cmdEncrypt.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.cmdEncrypt.BackColor = System.Drawing.SystemColors.Control;
this.cmdEncrypt.CausesValidation = true;
this.cmdEncrypt.Enabled = true;
this.cmdEncrypt.ForeColor = System.Drawing.SystemColors.ControlText;
this.cmdEncrypt.Cursor = System.Windows.Forms.Cursors.Default;
this.cmdEncrypt.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.cmdEncrypt.TabStop = true;
this.cmdEncrypt.Name = "cmdEncrypt";
this.Label2.Text = "Salt";
this.Label2.Size = new System.Drawing.Size(105, 25);
this.Label2.Location = new System.Drawing.Point(152, 56);
this.Label2.TabIndex = 4;
this.Label2.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.Label2.TextAlign = System.Drawing.ContentAlignment.TopLeft;
this.Label2.BackColor = System.Drawing.SystemColors.Control;
this.Label2.Enabled = true;
this.Label2.ForeColor = System.Drawing.SystemColors.ControlText;
this.Label2.Cursor = System.Windows.Forms.Cursors.Default;
this.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Label2.UseMnemonic = true;
this.Label2.Visible = true;
this.Label2.AutoSize = false;
this.Label2.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.Label2.Name = "Label2";
this.Label1.Text = "Date";
this.Label1.Size = new System.Drawing.Size(97, 25);
this.Label1.Location = new System.Drawing.Point(152, 16);
this.Label1.TabIndex = 3;
this.Label1.Font = new System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, System.Convert.ToByte(0));
this.Label1.TextAlign = System.Drawing.ContentAlignment.TopLeft;
this.Label1.BackColor = System.Drawing.SystemColors.Control;
this.Label1.Enabled = true;
this.Label1.ForeColor = System.Drawing.SystemColors.ControlText;
this.Label1.Cursor = System.Windows.Forms.Cursors.Default;
this.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.Label1.UseMnemonic = true;
this.Label1.Visible = true;
this.Label1.AutoSize = false;
this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.Label1.Name = "Label1";
this.Controls.Add(cmdDecrypt);
this.Controls.Add(txtPath);
this.Controls.Add(txtSalt);
this.Controls.Add(txtText);
this.Controls.Add(cmdEncrypt);
this.Controls.Add(Label2);
this.Controls.Add(Label1);
cmdDecrypt.Click += new System.EventHandler(cmdDecrypt_Click);
cmdEncrypt.Click += new System.EventHandler(cmdEncrypt_Click);
base.Load += new System.EventHandler(Form1_Load);
}
#endregion
#region Upgrade Support
private static Form1 m_vb6FormDefInstance;
private static bool m_InitializingDefInstance;
public static Form1 DefInstance
{
get
{
if (m_vb6FormDefInstance == null || m_vb6FormDefInstance.IsDisposed)
{
m_InitializingDefInstance = true;
m_vb6FormDefInstance = new Form1();
m_InitializingDefInstance = false;
}
return m_vb6FormDefInstance;
}
set
{
m_vb6FormDefInstance = value;
}
}
#endregion
private void cmdDecrypt_Click(object eventSender, System.EventArgs eventArgs)
{
string Saved = null;
if (Microsoft.VisualBasic.FileSystem.Dir(txtPath.Text, Microsoft.VisualBasic.FileAttribute.Normal).Length > 0)
{
Microsoft.VisualBasic.FileSystem.FileOpen(1, txtPath.Text, Microsoft.VisualBasic.OpenMode.Binary);
Saved = string.Empty.PadLeft(Microsoft.VisualBasic.FileSystem.LOF(1));
Microsoft.VisualBasic.FileSystem.FileGet(1, Saved, 1); Microsoft.VisualBasic.FileSystem.FileClose(1);
System.Diagnostics.Debug.WriteLine(Crypt(Saved, txtSalt.Text));
MessageBox.Show(Crypt(Saved, txtSalt.Text));
}
else
{
MessageBox.Show(txtPath.Text + " not found!");
return;
}
}
private void cmdEncrypt_Click(object eventSender, System.EventArgs eventArgs)
{
if (Microsoft.VisualBasic.FileSystem.Dir(txtPath.Text, Microsoft.VisualBasic.FileAttribute.Normal).Length > 0)
{
Microsoft.VisualBasic.FileSystem.Kill(txtPath.Text);
}
Microsoft.VisualBasic.FileSystem.FileOpen(1, txtPath.Text, Microsoft.VisualBasic.OpenMode.Binary);
Microsoft.VisualBasic.FileSystem.FilePut(1, Crypt(txtText.Text, txtSalt.Text), -1);
Microsoft.VisualBasic.FileSystem.FileClose(1);
MessageBox.Show("Saved!");
}
private void Command1_Click()
{
string TT = null;
string Salt = null;
TT = "this is the one I love and hate";
Salt = "WetRivrRat";
string Encrypted = null;
Encrypted = Crypt(TT, Salt);
System.Diagnostics.Debug.WriteLine(Encrypted);
System.Diagnostics.Debug.WriteLine(Crypt(Encrypted, Salt));
}
private string Crypt(string Text_Renamed, string Key)
{
int KeyLen = 0;
int i = 0;
int KeyPtr = 0;
short KeyChr = 0;
short TextChr = 0;
KeyLen = Key.Length;
int tempFor1 = Text_Renamed.Length;
for (i = 1; i <= tempFor1; i++)
{
TextChr = System.Convert.ToInt32(Text_Renamed[0]);
KeyChr = System.Convert.ToInt32(Key[0]);
Text_Renamed = Text_Renamed.Remove(i - 1, 1).Insert(i - 1, ((char)(TextChr ^ KeyChr)).ToString());
KeyPtr = ((KeyPtr + 1) % KeyLen);
}
return Text_Renamed;
}
private void Form1_Load(object eventSender, System.EventArgs eventArgs)
{
txtText.Text = System.Convert.ToString(System.DateTime.Now);
txtPath.Text = Support.GetPath() + "\\DateTest.bin";
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
Here's a hint. The include of the VB6 dll will show anyone who looks at your code that you've spent ages importing VB6 code instead of writing C# code. I would throw ALL of this away.
wetrivrrat wrote:
using Microsoft.VisualBasic.Compatibility.VB6;
Delete this for starters. Even if you use a VB->C# converter, delete this and fix the lines that break. Otherwise, it's not C# at all.
wetrivrrat wrote:
Microsoft.VisualBasic.FileSystem.Dir(txtPath.Text, Microsoft.VisualBasic.FileAttribute.Normal).Length > 0) { Microsoft.VisualBasic.FileSystem.FileOpen(1, txtPath.Text, Microsoft.VisualBasic.OpenMode.Binary); Saved = string.Empty.PadLeft(Microsoft.VisualBasic.FileSystem.LOF(1));
C# provides much nicer ways to do all of these things. This code is a disaster.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
yes, i know,
but
his idea was the best i had, and at this point i still don't compute how to make anything else better.....
i've got 6 books sitting here on the floor all dealing with hashing, salting or MD5, and i'm not sure where to start, my brain is fried, and all i want is to NOT see a string value of the date in my isoStore, his code made that happen even if it was vb, ....................................
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
Then take the function I gave you above, it does in fact convert the string and convert it back the next time through.
FYI, it's incredibly simple. It takes the string and a key, and it walks the key over and over, so if the string is 'this is it' and the key is 'fish', it's worked out like this:
this is it
saltsaltsa
The two values are XORed, which means that all bits are reversed. 10010011 becomes 01100011. Because all bits are reversed, doing it again restores the original value.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
You might find the following, produced with our Instant C# VB.NET to C# converter, more useful (you can download a free limited version at www.instantvb.com):
//TODO: INSTANT C# TODO TASK: Insert the following converted event handlers at the end of the 'InitializeComponent' method for forms or into a constructor for other classes:
base.Load += new System.EventHandler(Form1_Load);
private string Crypt(string Text_Renamed, string Key)
{
int KeyLen = 0;
int i = 0;
int KeyPtr = 0;
short KeyChr = 0;
short TextChr = 0;
KeyLen = Key.Length;
//INSTANT C# NOTE: The ending condition of VB 'For' loops is tested only on entry to the loop. Instant C# has created a temporary variable in order to use the initial value of Len(Text_Renamed) for every iteration:
int tempFor1 = Text_Renamed.Length;
for (i = 1; i <= tempFor1; i++)
{
TextChr = System.Convert.ToInt32(Text_Renamed[0]);
KeyChr = System.Convert.ToInt32(Key[0]);
Text_Renamed = Text_Renamed.Remove(i - 1, 1).Insert(i - 1, ((char)(TextChr ^ KeyChr)).ToString());
KeyPtr = ((KeyPtr + 1) % KeyLen);
}
return Text_Renamed;
}
private void Form1_Load(object eventSender, System.EventArgs eventArgs)
{
txtText.Text = System.Convert.ToString(System.DateTime.Now);
txtPath.Text = Support.GetPath + "\\DateTest.bin";
}
David Anton
Tangible Software Solutions
www.tangiblesoftwaresolutions.com
Home of:
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
|
|
|
|
|
David Anton wrote:
for (i = 1; i <= tempFor1; i++)
This doesn't work. It looks like VB is 1 indexed, changing it to 0 and < tempFor1 fixes this.
David Anton wrote:
TextChr = System.Convert.ToInt32(Text_Renamed[0]);
KeyChr = System.Convert.ToInt32(Key[0]);
Gosh - this is a mess. Perhaps the source code being converted is the problem ? I've just done it by hand, and this needs to be i-1 with the indexes your using.
David Anton wrote:
Text_Renamed = Text_Renamed.Remove(i - 1, 1).Insert(i - 1, ((char)(TextChr ^ KeyChr)).ToString());
Yeah, this will work, it's correctly using i-1. I guess that's an example of how a conversion can work but not produce the worlds prettiest code.
I just did this by hand, and knowing how excellent these conversion tools are, I'd say at a guess his original source is a disaster, I would be surprised if your tool introduced these problems.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Thanks for noticing that - there was a bug in the conversion of the combination of Asc and Mid. It's fixed now in the latest build of Instant C#.
The for loop conversion is correct though - I'm not saying that the original code worked or not - I'm just speaking from a C# equivalency viewpoint. You are probably right that the loop itself needs to be different for the code to work (in VB or C#).
David Anton
Tangible Software Solutions
www.tangiblesoftwaresolutions.com
Home of:
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant J#: VB.NET to J# Converter
|
|
|
|
|
David Anton wrote:
You are probably right that the loop itself needs to be different for the code to work (in VB or C#).
Yeah - I thought that would be the case, I didn't think your product would have a bug that changed array indices
I'm glad I helped you find a bug though. I can only imagine how complex a product like yours would be to get absolutely right.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
hello everyone.
I have a form that contain a panel i had design. and i have a button bellow this panel . Can i print this panel only ?
quangmogulasiaUNS
|
|
|
|
|
Hello.. i have a problem.. i have an application, and i want it to do smth when a key-combination is pressed... the problem is that i want it to do that even when it doesn't have focus.. i need smth like a "global hotkey", or i need to set up an event listener, that can listen for events even when my application doesn't have focus.. can anybody help? pls...
|
|
|
|
|
Ok in your app import the User32.dll function RegisterHotKey
[DllImport("user32.dll", SetLastError=true)]<br />
public static extern bool RegisterHotKey( IntPtr hWnd,
int id,
KeyModifiers fsModifiers,
Keys vk
);
Whilst Your at it - may as well import the Unregister one too
[DllImport("user32.dll", SetLastError=true)]<br />
public static extern bool UnregisterHotKey( IntPtr hWnd,
int id
);
In your MainForm constructor - register the hot key
RegisterHotKey(Handle, 100, KeyModifiers.Control | KeyModifiers.Shift, Keys.L);
In this case it's ctrl, shift and L - anything you want really.
Then override the WndProc for the main form and handle the event
protected override void WndProc(ref Message m)<br />
{<br />
const int WM_HOTKEY = 0x0312;<br />
<br />
switch(m.Msg)<br />
{<br />
case WM_HOTKEY:<br />
{<br />
break;<br />
}<br />
}<br />
<br />
base.WndProc (ref m);<br />
}
Remember to also unregister the hot key in the Form.OnClosed method
private void Form1_Closed(object sender, System.EventArgs e)<br />
{ <br />
UnregisterHotKey(Handle, 100);<br />
}
That should do what you want.
Mark.
|
|
|
|
|
Hey . Thanks a lot
|
|
|
|
|
still, i have a problem.... "KeyModifiers fsModifiers" the program throws an error when i attempt to build it..
" The type or namespace name 'KeyModifiers' could not be found (are you missing a using directive or an assembly reference?)" it's a common error.. pls help, the rest of the code is perfect
|
|
|
|
|
[Flags()]
public enum KeyModifiers
{
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
Hope this helps you out!
|
|
|
|
|
Hey, thx !
|
|
|
|
|
You're welcome!
|
|
|
|
|
when i use find method of DataRowCollection, it returns DataRow. i need to know the row number of this returned DataRow in the DataRowCollection. but i cant. how should encounter with this DataRow to find that number?
thanks
Rastegar
|
|
|
|
|
Hello!
I just have a question regarding the transparency of a panel. I have tried this with Controls & Forms, but all to no avail.
What I am trying to accomplish is a 3 layered panel system, with an underlying form. The form could have a background image, for example, and I should be able to see through all 3 panel objects I have and see the underlying form. This is simple.
The tricky part is that I want the 3 panels to function in a sort of non-transparent fashion. I have all the panels nested inside each other. panel 1 inside panel 2, panel 2 inside panel 3.
Now when the user clicks, the hit control is handled by the inner most panel...panel 1. Then if the user drags out a box, for example, the box is drawn on the middle panel, panel 2. When the user releases the mouse button, the middle panel is cleared and the final box is drawn on the last panel, panel 3.
The problem I am having is that the panels are transparent, but see all the way through to the form. Therefore I have it drawing successfully on the middle panel, but you cannot see what is being drawn because panel one is 'transparent' through it. Sorry if that didn't make sense.
I have set all the style properties as best I can, to accept transparent background colors, etc. Yet I just can't think of a way to accomplish this little dilema. Any and all help, suggestions would be greatly appreciated! Thanks!
|
|
|
|
|
Hi rcurrie, i created this sample app, and it works pretty good! I thought it was a pretty cool idea to sort your problem out. I like a challenge . Instead of 3 Panels, i used 2 (changed them to Labels, because of the flickering). The box that the user drags is drawn on Panel1, and all previously drawn boxes are stored in FinalRects and drawn on Panel2.
But... i made it in VB, because i don't know how to program in C# . I tried converting with SharpDevelop, but errors everywhere...
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class TransPanelForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.DoubleBuffer Or ControlStyles.UserPaint, True)
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Panel1 As System.Windows.Forms.Label
Friend WithEvents Panel2 As System.Windows.Forms.Label
Friend WithEvents Panel3 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Panel1 = New System.Windows.Forms.Label
Me.Panel3 = New System.Windows.Forms.Label
Me.Panel3.SuspendLayout()
Me.SuspendLayout()
'
'Panel1
'
Me.Panel1.BackColor = System.Drawing.Color.Transparent
Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.Panel1.Location = New System.Drawing.Point(0, 0)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(270, 244)
Me.Panel1.TabIndex = 0
'
'Panel3
'
Me.Panel3.BackColor = System.Drawing.Color.Transparent
Me.Panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.Panel3.Controls.Add(Me.Panel1)
Me.Panel3.Dock = System.Windows.Forms.DockStyle.Fill
Me.Panel3.Location = New System.Drawing.Point(10, 10)
Me.Panel3.Name = "Panel3"
Me.Panel3.Size = New System.Drawing.Size(272, 246)
Me.Panel3.TabIndex = 0
'
'TransPanelForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Panel3)
Me.DockPadding.All = 10
Me.Name = "TransPanelForm"
Me.Text = "TransPanelForm"
Me.Panel3.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
''' <summary>The current rectangle that is dragged.</summary>
Private DragRect As Rectangle
''' <summary>A collection of rectangles.</summary>
Private FinalRects As New ArrayList
''' <summary>The position at which the mouse was pressed.</summary>
Private MouseDownPos As Point
''' <summary>Is the left mouse button down?</summary>
Private LeftMouseDown As Boolean
Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaintBackground(e)
Dim rect As Rectangle = Me.ClientRectangle
Dim brush As New LinearGradientBrush(rect, Color.White, Color.LightSteelBlue, LinearGradientMode.ForwardDiagonal)
e.Graphics.FillRectangle(brush, rect)
brush.Dispose()
End Sub
Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs)
MyBase.OnSizeChanged(e)
Me.Invalidate()
End Sub
''' <summary>Calculates the DragRect.</summary>
Private Function CalcDragRect(ByVal mouseX As Integer, ByVal mouseY As Integer) As Rectangle
Return Rectangle.FromLTRB(Math.Min(mouseX, MouseDownPos.X), _
Math.Min(mouseY, MouseDownPos.Y), _
Math.Max(mouseX, MouseDownPos.X), _
Math.Max(mouseY, MouseDownPos.Y))
End Function
#Region " Panel1_Mouse... "
Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
If e.Button = MouseButtons.Left Then
Debug.WriteLine("Panel1_MouseDown")
MouseDownPos = New Point(e.X, e.Y)
LeftMouseDown = True
DragRect = New Rectangle(e.X, e.Y, 0, 0)
Panel2.Invalidate()
End If
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
If LeftMouseDown Then
Debug.WriteLine("Panel1_MouseUp")
LeftMouseDown = False
DragRect = Me.CalcDragRect(e.X, e.Y)
Panel2.Invalidate()
FinalRects.Add(DragRect)
End If
End Sub
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If e.Button = MouseButtons.Left Then
Debug.WriteLine("Panel1_MouseMove")
DragRect = Me.CalcDragRect(e.X, e.Y)
Panel1.Invalidate()
End If
End Sub
#End Region
''' <summary>Draws a rectangle.</summary>
''' <remarks>Instead of just doing 'g.DrawRectangle(pen, rect)',
''' this method draws a line if the width and/or height
''' of the rectangle is zero. This way a line is drawn,
''' instead of just nothing.</remarks>
Public Shared Sub DrawRect(ByVal g As Graphics, ByVal rect As Rectangle, ByVal pen As Pen)
If (rect.Width = 0) AndAlso (rect.Height = 0) Then
g.DrawLine(pen, rect.X, rect.Y, rect.X, rect.Y + 0.01!)
ElseIf (rect.Width > 0) AndAlso (rect.Height = 0) Then
g.DrawLine(pen, rect.X, rect.Y, rect.Right, rect.Y)
ElseIf (rect.Width = 0) AndAlso (rect.Height > 0) Then
g.DrawLine(pen, rect.X, rect.Y, rect.X, rect.Bottom)
Else '(rect.Width > 0) AndAlso (rect.Height > 0)
g.DrawRectangle(pen, rect)
End If
End Sub
Private Sub Panel2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel2.Paint
For Each rect As Rectangle In FinalRects
DrawRect(e.Graphics, rect, Pens.Red)
Next
End Sub
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
If LeftMouseDown Then
Dim pen As New Pen(Color.Blue)
pen.DashStyle = DashStyle.Dash
DrawRect(e.Graphics, DragRect, pen)
pen.Dispose()
End If
End Sub
End Class
|
|
|
|
|
Wow Absofukinlutely! Thanks so much for taking the time to work that out! I have a little VB.NET experience so I'll see if I can convert it to C# and see how it handles. Thanks again so much!!
|
|
|
|
|