C#

Fermé
cesar - 6 mai 2003 à 11:43
 Nigui - 7 sept. 2007 à 08:57
Salut
je suis entrain de teste de capter les evenements , mais ca marche
pas . est ce que quelqu'un peut m'aider merci d'avance.
void textBoxText_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
this.textBox.Text = "ca marche ";

} else this.textBox.Text = "ca marche pas";
if(e.KeyCode == Keys.Delete) this.textBox.Text = "salut";
this.textBox.Text = "con";
}



j'ai tester meme avec des boutons.

cesar

4 réponses

Salut, j'ai cherché ton problème, en fait tu as oublié de faire qqch :
Il faut que tu ailles sur ton Form1.cs[design] tu séléctionnes la fenetre et tu vas dans le panneau "Propriétés" et tu cliques sur Evenements (symbolisé par un éclair) tu vas tout en bas et tu ecris à côté de la case "Key Up" le nom de la fonction dans mon exemple j'ai pris comme nom "KeyUp_actions" ensuite tu fais Entrée, t'es directement envoyé sur la page de code et là tu mets :

private void KeyUp_actions(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
this.label1.Text = "Ca marche";
}
else
{
this.label1.Text = "Ca marche pas";
}
if(e.KeyCode == Keys.Delete)
{
this.label1.Text = "salut";
}

}


Sinon mieux vaut mettre un label plutôt qu'un textbox car le textbox envoie directement ton curseur dans sa barre et ca fait donc foirer la prise de commande...
Si t'as un pb, envoie moi un mail...
@+
0
Salut

merci pour ta reponce. je vais regarder ceci. car moi j'utilise le logiciel SharpDevelopp et non pas V.Net.

j'ai comme meme avance un petit peut ; comme tu vois avec ce programme, mais l'orsque j'ai essai d'introduire leOnKeyDown
pour agir si on apuie sur la touche Down ou Up..
il m'indique une erreur d'acceé (can not access) dans:

*****this.textBox.OnKeyDown += new System.Windows.Forms.KeyEventArgs(this.textBoxChanged);*******



est ce que tu peut m'aider a resoudre encore ce probleme.
merci d'avance pour ton aide

cesar
using System;
using System.Windows.Forms;

namespace MyFormProject
{
class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TreeView treeView;
private System.Windows.Forms.Button button;
private System.Windows.Forms.TextBox textBox;
public MainForm()
{
InitializeComponent();
// textBoxChanged(object sender, System.Windows.Forms.KeyEventArgs e);
}

// This method is used in the forms designer.
// Change this method on you own risk


private void textBoxChanged(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.ControlKey)
{
this.textBox.Text = "salut";
// Display a pop-up help topic to assist the user.
// Help.ShowPopup(textBox, "Enter your first name", new Point(textBox.Right, this.textBox.Bottom));
} else this.textBox.Text = "salut";
if(e.KeyCode == Keys.Delete) this.textBox.Text = "salut";
this.textBox.Text = "con";
}


private void treeView_KeyDown(object sender, KeyEventArgs e)
{
/* If the 'Alt' and 'E' keys are pressed,
* allow the user to edit the TreeNode label. */
if(e.Alt && e.KeyCode == Keys.E)

{
treeView.LabelEdit = true;
// If there is a TreeNode under the mose cursor, begin editing.
TreeNode editNode = treeView.GetNodeAt(
treeView.PointToClient(Control.MousePosition));
if(editNode != null)
{
editNode.BeginEdit();
}
}
}

private void treeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
// Disable the ability to edit the TreeNode labels.
treeView.LabelEdit = false;
}










void buttonClick(object sender, System.Windows.Forms.MouseEventArgs e)
{this.button.Text="er";
if( e.Clicks == 2) this.button.Text="er";
else this.button.Text="er";
}



void textBox2TextChanged(object sender, System.EventArgs e)
{// if(e..KeyCode == Keys.Down)
{
// this.textBox2.Text = "salut";
// Display a pop-up help topic to assist the user.
// Help.ShowPopup(textBox, "Enter your first name", new Point(textBox.Right, this.textBox.Bottom));
}

}

void textBox2TextChanged(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.Handled) this.textBox2.Text = " ";
}

void InitializeComponent() {
this.textBox = new System.Windows.Forms.TextBox();
this.button = new System.Windows.Forms.Button();
this.treeView = new System.Windows.Forms.TreeView();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(24, 40);
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(152, 20);
this.textBox.TabIndex = 0;
this.textBox.Text = "textBox";
this.textBox.OnKeyDown += new System.Windows.Forms.KeyEventArgs(this.textBoxChanged);

this.textBox2.TextChanged += new System.EventHandler(this.textBox2TextChanged);
//
// button
//
this.button.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.button.ForeColor = System.Drawing.SystemColors.Control;
this.button.Location = new System.Drawing.Point(32, 96);
this.button.Name = "button";
this.button.Size = new System.Drawing.Size(120, 32);
this.button.TabIndex = 1;
this.button.Text = "button";
this.button.MouseMove += new System.Windows.Forms.MouseEventHandler(this.buttonClick);
//
// treeView
//
this.treeView.ImageIndex = -1;
this.treeView.Location = new System.Drawing.Point(64, 152);
this.treeView.Name = "treeView";
this.treeView.SelectedImageIndex = -1;
this.treeView.Size = new System.Drawing.Size(136, 72);
this.treeView.TabIndex = 2;
// this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeViewAfterSelect);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(176, 72);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(96, 20);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "textBox2";
// this.textBox2.TextChanged += new System.EventHandler(this.textBox2TextChanged);

//textBoxText_KeyUp
// MainForm
//
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox2,
this.treeView,
this.button,
this.textBox});
this.Text = "This is my form";
this.ResumeLayout(false);
}

[STAThread]
public static void Main(string[] args)
{
Application.Run(new MainForm());
}
}
}
0
Je crois qu'il est préférable de faire un OnKey down non pas sur le textbox mais sur le panneau qui contient le textbox, la fenetre "Form"quoi...car sinon si tu selectionnes pas le textbox ca marche pas ...
et encore une fois le textbox c pas top, le label est mieux...
0
je ne trouve pas les zones dont tu parles.
je veux lier un traitement à une touche de fonction
exemple: j'appuie sur f11 , j'effectue la mise à jour
je ne trouve pas comment faire
pourriez vous m'aider
Merci à tous
0