|
I have a textbox on my form. Now, I want to find which key was pressed in the textbox. I mean i want to find which key pressed including Function keys F1, F2, etc...Modifier Keys like SHIFT, CTRL, and ALT. How can I do this.
|
|
|
|
|
I looked up on Google and I found a post on a forum that said taht you have to use the System.Threading.Thread.Sleep(int milliseconds). Howecer, when i do this the UI hangs. Basically, what I'm trying to accomplish here goes like this: I have a for loop and between loop iterations i wish to put a 5 second delay. Now what?
|
|
|
|
|
Mridang Agarwal wrote:
looked up on Google and I found a post on a forum that said taht you have to use the System.Threading.Thread.Sleep(int milliseconds).
Bizarre. This is a pile of bull. Why would you have to sleep to get a key event ? But good for you for trying to find the answer all the same.
Mridang Agarwal wrote:
I have a for loop and between loop iterations i wish to put a 5 second delay. Now what?
Oh, this is a different question ? Yes, Thread.Sleep is your answer, but it stops your app running, obviously. The other possibility is to put this code in a thread, so only that thread sleeps, or create a big old ugly loop that uses datetime.now over and over, and calls Application.DoEvents. Why do you want your app to sleep for 5 seconds ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Oh !#$#. What he hell did I just post? That was a big goof up. I was posting a reply to another post and I accidentally posted it here. Sorry!
|
|
|
|
|
If you catch the KeyPressed event, you get a different event args to KeyDown/KeyUp. There's another event for control keys as well, I think. You can also call GetAsyncKeyState via pinvoke, for full control over what control keys are down, etc, during any keyboard event, or anywhere in your code, for that matter.
KeyDown/KeyUp have the properties to say if a control key was pressed, KeyPressed actually gets a different key code ( in the range 0-25, I *think* ) if Control is also down.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
i got the solution finally. I put the in the textbox_KeyUp event and wrote e.Keydata.ToString();
|
|
|
|
|
You'll simply need to handle the "KeyDown" event of the TextBox like so:
Just put a similar code in the KeyDown event of the TextBox:
If e.KeyCode = Keys.Enter Then<br />
'Whatever you wanna do<br />
End If
The keys F1, F2, etc... are also present in the Keys enumeration (Keys.F1, Keys.Tab, Keys.a, etc...)
PEACE!
|
|
|
|
|
how are you my friend .
listen my friend about handling the keydown event is not useful and wasting time if you try to handle it through your keydown event handling cause you will need to nest alot alot of if statements and switch keywords and it is not practical you need something like keylogger code to log any keys pressed even if your form is activated or not then you can handle the specific key when it is pressed like the following code :-
<br />
<br />
<br />
system.Runtime.InteroServices;<br />
<br />
[Dllimport("User32.dll")]<br />
Public static extern short GetAsyncKeyState(System.Windows.forms.Keys vkey);<br />
[Dllimport("User32.dll")]<br />
public static extern short GetAsyncKeyState(System.Int32 vkeys);<br />
<br />
System.Timers.Timer mytimer;<br />
System.String KeyBuffer;<br />
<br />
Private void mytimer_Elapsed(object o , System.Timers.ElapsedEventargs args)<br />
{<br />
foreach(System.Int32 i in Enum.GetValues(typeof(Keys)))<br />
{<br />
if(GetAsyncKeyState(i) == -32767)<br />
{<br />
keybuffer += Enum.GetName(typeof(keys),i);<br />
TextBox.Text += KeyBuffer;<br />
Keybuffer = "";<br />
}<br />
}<br />
}<br />
public YourKeyLogger()<br />
{<br />
mytimer.Elapsed += new ElapsedEventHandler(this.mytimer_elapsed);<br />
}<br />
I hope the code can do the work for you
Thanks my friend
Miss With The Best And Die Like The Rest
|
|
|
|
|
I have a datatable that has an id, firstname, lastname, and email address field being returned. I also have a datagrid that has the datasource set to the datatable. I have hidden the id column by using:
oDataTable.Columns["id"].ColumnMapping = MappingType.Hidden
so that the id column doesn't display to the end user but is still available to reference. I am now trying to double click the row in the datagrid and get the hidden id column value in the double click event so that I can pass that value. How is the best practice to do this?
|
|
|
|
|
oDataTable.Rows[this.yourDataGrid.CurrentCell.RowNumber]["id"].ToString()
Live Life King Size
Alomgir Miah
|
|
|
|
|
Hi.
I have such a strange problem. I define class which is derived from System.Windows.Forms.UserControl and implements some interface.
public class ORAMainControl : System.Windows.Forms.UserControl, IORAPluginControl
Now when I try to derive any other control from this ORAMainControl, VisualStudio designer won't appear, saying that non of the classes can be designed.
Did some digging, and eventually changed declaration to ORAMainControl to this:
[Designer("System.Windows.Forms.Design.DocumentDesigner, System.Windows.Forms.Design.DLL",
typeof(IRootDesigner)),
DesignerCategory("Form")]
public class ORAMainControl : System.Windows.Forms.UserControl, IORAPluginControl
Now when I attempt to design any derived class Stuido says that it can't find a designer for ORAMainControl.
Where I am wrong and what to do? I am stuck.
Thanks in advance.
|
|
|
|
|
hi,
i have a datagrid on a usercontrol which has two columns of DateTimePicker style. They are 'From Date' and 'To Date'.
when i enter data in the first row, then i also choose the date. So when i add data in the second row, the dateTimePicker in 'From Date ' Should show me the next day of the date that i entered in the 'To Date' of the previous column. This should happen for all the rows that i enter..
could ne1 plz tell me how to do this??
Any help is appreciated,
Thanks
|
|
|
|
|
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
namespace Test.Controls
{
///
/// Summary description for DataGridTimePickerColumn.
/// This class builds the DateTimePicker column user control for datagrids for windows application.
/// It inherits DataGridTextBoxColumn, since this is already an available control in the .Net Framework.
/// What needs to be modified, depending on the application, is the ValueChanged event.
/// To use this class, simply add it to the project, construct an instance, and use its properties.
/// this is a sample usage:
///
/// DataGridTimePickerColumn col3=new DataGridTimePickerColumn();
/// dataGrid1.TableStyles[0].GridColumnStyles.Add(col3);
/// dataGrid1.TableStyles[0].GridColumnStyles[7].MappingName="Date Time";
/// dataGrid1.TableStyles[0].GridColumnStyles[7].HeaderText="Date Time";
/// dataGrid1.TableStyles[0].GridColumnStyles[7].Width=100;
///
///
public class DataGridTimePickerColumn : DataGridTextBoxColumn//DataGridColumnStyle
{
private DateTimePicker myDateTimePicker = new DateTimePicker();
// The isEditing field tracks whether or not the user is
// editing data with the hosted control.
private bool isEditing;
public DataGridTimePickerColumn() : base()
{
myDateTimePicker.Visible = false;
}
protected override void Abort(int rowNum)
{
isEditing = false;
myDateTimePicker.ValueChanged -=
new EventHandler(TimePickerValueChanged);
Invalidate();
}
protected override bool Commit
(CurrencyManager dataSource, int rowNum)
{
myDateTimePicker.Bounds = Rectangle.Empty;
myDateTimePicker.ValueChanged -=
new EventHandler(TimePickerValueChanged);
if (!isEditing)
return true;
isEditing = false;
try
{
DateTime value = myDateTimePicker.Value;
SetColumnValueAtRow(dataSource, rowNum, value);
}
catch (Exception)
{
Abort(rowNum);
return false;
}
Invalidate();
return true;
}
protected override void Edit(
CurrencyManager source,
int rowNum,
Rectangle bounds,
bool readOnly,
string instantText,
bool cellIsVisible)
{
DateTime value = DateTime.Parse(GetColumnValueAtRow(source, rowNum).ToString());
if (cellIsVisible)
{
myDateTimePicker.Bounds = new Rectangle
(bounds.X + 2, bounds.Y + 2,
bounds.Width - 4, bounds.Height - 4);
myDateTimePicker.Value = value;
myDateTimePicker.Visible = true;
myDateTimePicker.ValueChanged +=
new EventHandler(TimePickerValueChanged);
}
else
{
myDateTimePicker.Value = value;
myDateTimePicker.Visible = false;
}
if (myDateTimePicker.Visible)
DataGridTableStyle.DataGrid.Invalidate(bounds);
}
protected override Size GetPreferredSize(
Graphics g,
object value)
{
return new Size(100, myDateTimePicker.PreferredHeight + 4);
}
protected override int GetMinimumHeight()
{
return myDateTimePicker.PreferredHeight + 4;
}
protected override int GetPreferredHeight(Graphics g,
object value)
{
return myDateTimePicker.PreferredHeight + 4;
}
protected override void Paint(Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum)
{
Paint(g, bounds, source, rowNum, false);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
bool alignToRight)
{
Paint(
g,bounds,
source,
rowNum,
Brushes.Red,
Brushes.Blue,
alignToRight);
}
protected override void Paint(
Graphics g,
Rectangle bounds,
CurrencyManager source,
int rowNum,
Brush backBrush,
Brush foreBrush,
bool alignToRight)
{
DateTime date = DateTime.Parse(GetColumnValueAtRow(source, rowNum).ToString());
Rectangle rect = bounds;
g.FillRectangle(backBrush,rect);
rect.Offset(0, 2);
rect.Height -= 2;
g.DrawString(date.ToString("d"),
this.DataGridTableStyle.DataGrid.Font,
foreBrush, rect);
}
protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value);
if (myDateTimePicker.Parent != null)
{
myDateTimePicker.Parent.Controls.Remove
(myDateTimePicker);
}
if (value != null)
{
value.Controls.Add(myDateTimePicker);
}
}
private void TimePickerValueChanged(object sender, EventArgs e)
{
this.isEditing = true;
base.ColumnStartedEditing(myDateTimePicker);
}
}
}
Live Life King Size
Alomgir Miah
|
|
|
|
|
HEY! Thanks..but i've already written that code..it is to use a datatmepicker in a datagrid...but u dinn get what i asked...i wld appreciate if u cld help me...
i have a datagrid on a usercontrol which has two columns of DateTimePicker style. They are 'From Date' and 'To Date'.
when i enter data in the first row, i select the date in the two columns. So when i add data in the second row, the dateTimePicker in 'From Date ' Should show me the next day of the date that i entered in the 'To Date' of the previous column. This should happen for all the rows that i enter..
plz tell me how to do this..
|
|
|
|
|
Please override the Edit method of the DataGridDateTimePickerColumn control and Fire a custom event. Subscribe to the event in the usercontrol and do the processing.
protected override void Edit(System.Windows.Forms.CurrencyManager source, int row, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
CustomEventArgs e = null;
this.row = row;
//
//Fire Event and Take Decision
//
if (this.CustomEvent != null)
{
e = new CustomEventArgs(row, this.column, isEnabled, bounds);
this.CustomEvent(this, e);
this.isEnabled = e.IsEnable;
this.isReadOnly = e.IsReadOnly;
}
if (this.isEnabled)
{
if (((CurrencyManager) source).Position == row)
{
base.Edit(source, row, bounds, readOnly, instantText, cellIsVisible);
this.source = source;
}
}
else
{
if (this.isReadOnly)
{
if (((CurrencyManager) source).Position == row)
{
base.Edit(source, row, bounds, true, instantText, cellIsVisible);
}
}
}
}
This is in the usercontrol
private void OnGridEnableDisable(object sender, CustomEventArgs e)
{
try
{
if (
this.hitTestGridInfo != null &&
this.hitTestGridInfo.Type == DataGridEx.HitTestType.RowHeader
)
{
e.IsEnable = false;
}
else
{
switch (e.Column) // remember to initialize the picker column with this index
{
case 4: // i.e. To Date
// put your logic
case 5: // From Date
}
}
}
catch
{
}
}
I hope this helps.
Live Life King Size
Alomgir Miah
|
|
|
|
|
I've been triying to make a csc.exe link for typing at the console "csc.exe myfile.cs..." and not the entire csc.exe path, i posted a topic days ago but recomendations didn't work. I tried to add a system variable but didn't worked too. What do you suggest?. MS .NET SDK is not installed on my system.
Thanks
|
|
|
|
|
|
You add the path to the environment using:
set path=%path%;c:\the\path\to\the\program
---
b { font-weight: normal; }
|
|
|
|
|
I have several self-contained C# samples, and I want to show them run to students in class. So I wrote a program, in which I opened the source file, and then ....
Microsoft.CSharp.CSharpCodeProvider cp
= new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.ICodeCompiler ic = cp.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters cpar
= new System.CodeDom.Compiler.CompilerParameters();
cpar.ReferencedAssemblies.Add("system.dll");
string src = this.textBox1.Text;
System.CodeDom.Compiler.CompilerResults cr = ic.CompileAssemblyFromSource(cpar,src);
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
MessageBox.Show(ce.ErrorText);
if (cr.Errors.Count == 0 && cr.CompiledAssembly != null)
{
Type ObjType = cr.CompiledAssembly.GetType(classname);
try
{
if (ObjType != null)
{
string[] arguments={};
myobj = Activator.CreateInstance(ObjType);
ObjType.InvokeMember("Main", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static|BindingFlags.IgnoreCase, null, null, arguments);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return true;
}
My problem is when I run it, I always catch the exception MissingMethodException, i.e. the method "Main" cannot be found.
In textBox2, I input "P2_1.FirstProgram", that's the class which hosted the method "Main".
I guess the "ObjType.InvokeMember" caused the problem, but MSDN did not tell me how to do clearly.
Help!!!!!
|
|
|
|
|
I've written a C# Jukebox app ( like, a year ago ) and the client has been very slow to test it. They are reporting a couple of problems, first that they have trouble with the coin mech stopping working, and second on some that all input is ignored. Now, these jukeboxes use custom input devices, through the parallel and joystick ports. I have used model/view/controller, and the various controller classes typically contain a timer which fires 10 times a second and checks the state of a button, if it's on, it stores the fact and then won't accept input until it's been off again, and fires an event to the model. I've discovered that these machines are left on for weeks at a time.
The problem appears most with the joystick, which is used to catch credits. Windows shows that the joystick is working ( through the control panel applet ), but the software does not accept input until it is rebooted. So I'm wondering if there's any known issue where software that runs a timer over and over can break down because the timer fails ? It seems weird to me, but it's the only explanation I can come up with.
In the meantime, I'm looking into using DirectInput for the joystick, but I'd still like to understand why the current approach is not working reliably.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Just a wild guess, maybe you need to declare the member that you access from two threads (the button state) as volatile?
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
Actually, it's all running on the same thread. What does volatile do for me ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
What timer call are you using?
.............................
There's nothing like the sound of incoming rifle and mortar rounds to cure the blues. No matter how down you are, you take an active and immediate interest in life.
Fiat justitia, et ruat cælum
|
|
|
|
|
I'm just creating a timer - do they automatically run on another thread?
private Timer timer = new Timer();
in the constructor....
timer.Interval = 100; // poll 10 times a second
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
http://msdn.microsoft.com/msdnmag/issues/04/02/TimersinNET/default.aspx[^]
The key here is that some timers can get pre-empted or simply "stop" running depending on the class you use. The windows forms timers can be really quirky.
.............................
There's nothing like the sound of incoming rifle and mortar rounds to cure the blues. No matter how down you are, you take an active and immediate interest in life.
Fiat justitia, et ruat cælum
|
|
|
|
|