Curser positioning

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
In a form, how would I go about putting the curser either on a certain button or in a box with everything highlighted?
 
You can use the Cursor object to set the position of the
mouse cursor.
C#:
Cursor.Position = new System.Drawing.Point(10,10)
would move the cursor to the point 10,10 from the top left corner
of the screen. Calculating the center of a control isn't hard; just
find the left/top position of the control on the screen (using the
PointToScreen method, I believe) and add half the width/height to
the position.
 
What I mean is highlighting for example all the words in a text box each time the user presses a button to make it easy to continuously enter in different text.
 
I could not figure out how to use it.

I tried what I thought would work:
TextBox.SelectAll (TB1);
 
Do I need to put it in a certain place.

It is not working.

private void Go2_Click(object sender, System.EventArgs e)
{
TB1.SelectAll();
L1.Text=""; L2.Text="";
count(ac, bc, cc);
}
 
Just put the line wherever you want the selection to take place;
make sure you're replacing TB1 with whatever the name of your
textbox is.
 
TB1 is my text box name. Here is the whole thing:

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication4
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button Go1;
		private System.Windows.Forms.Button Go2;
		private System.Windows.Forms.Label L1;
		private System.Windows.Forms.Label L2;
		private System.Windows.Forms.TextBox TB1;
		private System.Windows.Forms.TextBox TB2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Go1 = new System.Windows.Forms.Button();
			this.L1 = new System.Windows.Forms.Label();
			this.L2 = new System.Windows.Forms.Label();
			this.Go2 = new System.Windows.Forms.Button();
			this.TB1 = new System.Windows.Forms.TextBox();
			this.TB2 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// Go1
			// 
			this.Go1.Location = new System.Drawing.Point(64, 56);
			this.Go1.Name = "Go1";
			this.Go1.Size = new System.Drawing.Size(96, 24);
			this.Go1.TabIndex = 0;
			this.Go1.Text = "Go1";
			this.Go1.Click += new System.EventHandler(this.Go1_Click);
			// 
			// L1
			// 
			this.L1.BackColor = System.Drawing.Color.White;
			this.L1.Location = new System.Drawing.Point(13, 0);
			this.L1.Name = "L1";
			this.L1.Size = new System.Drawing.Size(294, 48);
			this.L1.TabIndex = 1;
			this.L1.Text = "L1";
			this.L1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// L2
			// 
			this.L2.BackColor = System.Drawing.Color.White;
			this.L2.Location = new System.Drawing.Point(16, 88);
			this.L2.Name = "L2";
			this.L2.Size = new System.Drawing.Size(288, 56);
			this.L2.TabIndex = 2;
			this.L2.Text = "L2";
			this.L2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// Go2
			// 
			this.Go2.Location = new System.Drawing.Point(160, 56);
			this.Go2.Name = "Go2";
			this.Go2.Size = new System.Drawing.Size(96, 24);
			this.Go2.TabIndex = 3;
			this.Go2.Text = "Go2";
			this.Go2.Click += new System.EventHandler(this.Go2_Click);
			// 
			// TB1
			// 
			this.TB1.Location = new System.Drawing.Point(0, 56);
			this.TB1.Name = "TB1";
			this.TB1.Size = new System.Drawing.Size(56, 20);
			this.TB1.TabIndex = 4;
			this.TB1.Text = "textBox1";
			this.TB1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			// 
			// TB2
			// 
			this.TB2.Location = new System.Drawing.Point(264, 56);
			this.TB2.Name = "TB2";
			this.TB2.Size = new System.Drawing.Size(56, 20);
			this.TB2.TabIndex = 5;
			this.TB2.Text = "textBox2";
			this.TB2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.ControlLight;
			this.ClientSize = new System.Drawing.Size(320, 142);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.TB2,
																		  this.TB1,
																		  this.Go2,
																		  this.L2,
																		  this.L1,
																		  this.Go1});
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}
int ac=0, bc=0, cc=0;
		private void Go1_Click(object sender, System.EventArgs e)
		{	
			L1.Text=""; L2.Text="";
			if(TB1.Text == "a"||TB1.Text == "b"||TB1.Text == "c")
			{
				char letter = char.Parse(TB1.Text);
				switch (letter) 
				{
					case 'a': ++ac; L1.Text+="a: "+ ac; break;
					case 'b': ++bc; L1.Text+="b: "+ bc; break;
					case 'c': ++cc; L1.Text+="c: "+ cc; break;
				}
			}
			else
			{
				MessageBox.Show("Incorrect letter entered.  Lowercase letters only");
				Cursor.Position = new System.Drawing.Point(493,430);}
			}

		public void count(int a, int b, int c) 
		{
			L1.Text+="Totals for each letter:";
			L2.Text+="a: "+ a+ "\nb: "+ b+ "\nc: "+ c;
		}

		private void Go2_Click(object sender, System.EventArgs e)
		{
			TB1.SelectAll();
			L1.Text="";  L2.Text="";
			count(ac, bc, cc);
		}
	}
}
 
What do you mean? You mean you did that and it doesn't work,
or that it did work and you want to know why?

If it did work, it's because when a textbox doesn't have focus,
the blue selection is hidden; only when the textbox has focus
does it appear.

If it doesn't work, then I don't know what to tell you. And no,
there is no way (I don't think) to do it any quicker than the way I
showed you.
 
Back
Top