getting rid of the carot

Z

Zach

I want to get rid of the carot on the screen. Below is my code which isn't
getting rid of the carot.
Please help.

using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace E_sys
{
public partial class CSbox : Form
{
[DllImport("User32.dll")]
static extern Boolean HideCaret(System.IntPtr hWnd);
public CSbox(string text)
{
InitializeComponent();
textBox1.Text = text;
textBox1.SelectionStart = 0;
HideCaret(textBox1.Handle);
}
}
}
 
A

Arne Vajhøj

I want to get rid of the carot on the screen. Below is my code which
isn't getting rid of the carot.
Please help.

using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace E_sys
{
public partial class CSbox : Form
{
[DllImport("User32.dll")]
static extern Boolean HideCaret(System.IntPtr hWnd);
public CSbox(string text)
{
InitializeComponent();
textBox1.Text = text;
textBox1.SelectionStart = 0;

Try add:

textBox1.Focus();

here.
HideCaret(textBox1.Handle);
}
}
}

Tested with:

[DllImport("User32.dll",SetLastError=true)]
static extern bool HideCaret(System.IntPtr hWnd);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern int FormatMessage(int dwFlags, string
lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int
nSize, string[] Arguments);
private string FormatMessageWrapper(int err)
{
StringBuilder sb = new StringBuilder(256);
FormatMessage(4096, null, err, 0, sb, sb.Capacity, null);
return sb.ToString();
}
void Button1Click(object sender, EventArgs e)
{
textBox1.Focus();
if(!HideCaret(textBox1.Handle))
{

MessageBox.Show(FormatMessageWrapper(Marshal.GetLastWin32Error()));
}
}

which gives some indication if something goes wrong, but it
was not quite obvious that "Access denied" was due to
not having focus. Google found that.

Arne
 
Z

Zach

On 2/25/2012 9:42 AM, Zach wrote:
SNIPPED

Try add:

textBox1.Focus();

here.
SNIPPED
Arne




Arne, thanks, I couldn't make Focus() do the trick.

For your info: the position of the form (a message) is on top of another
form.

Following your lead I found that this.Show() does do the trick!
Q: Is that OK? See code below.

I am wanting to do a couple of things:
1. vertically space out the ad hoc message,
2. get rid of the highlighting,
3. get rid of the carot.

Q Re 1: Might you be able to improve on the loop with the \r\n?

Something like:

..... = "\r\n';
TextBox1 += new string((..., nroff);

but that won't work of course.

Zach.


using System;

using System.Windows.Forms;

using System.Runtime.InteropServices;



namespace E_sys

{

public partial class PearlMessage : Form

{

[DllImport("User32.dll")]

static extern Boolean HideCaret(System.IntPtr hWnd);

public PearlMessage(string text)

{

InitializeComponent();

string[] temp = text.Split((char)13);

int nroff = temp.Length + 1;

int down = (10 - nroff) / 2;

for (int ctr = 0; ctr < down; ctr++)

{

textBox1.Text += "\r\n";

}

textBox1.AppendText(text);

this.Show();// <---------------------

HideCaret(textBox1.Handle);

textBox1.Click +=new EventHandler(textBox1_Click);

}

private void textBox1_Click(object o, EventArgs e)

{

this.Dispose();

}

}

}
 
A

Arne Vajhøj

Arne, thanks, I couldn't make Focus() do the trick.

For your info: the position of the form (a message) is on top of another
form.

Following your lead I found that this.Show() does do the trick!

The form probably need to be visible for it to work as well.

:)
Q: Is that OK? See code below.

I am wanting to do a couple of things:
1. vertically space out the ad hoc message,
2. get rid of the highlighting,
3. get rid of the carot.

Q Re 1: Might you be able to improve on the loop with the \r\n?

Something like:

.... = "\r\n';
TextBox1 += new string((..., nroff);

but that won't work of course.
public PearlMessage(string text)

{

InitializeComponent();

string[] temp = text.Split((char)13);

I would use '\r' here.
int nroff = temp.Length + 1;

int down = (10 - nroff) / 2;

for (int ctr = 0; ctr < down; ctr++)

{

textBox1.Text += "\r\n";
}

Given that down should always be less than 5 then you
could replace the loop with:

textBox1.Text += MULTINEWLINES.Substring(0, Math.Max(down,0) * 2);

where

private const string MULTINEWLINES =
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n";

(I added a little bit extra just in case)

Arne
 
Z

Zach

T: 0031 (0)85 773 0573
M: 0031 (0)6 208 55 608
E: (e-mail address removed)
WWW: pearltree.org
Arne Vajhøj said:
Given that down should always be less than 5 then you
could replace the loop with:

textBox1.Text += MULTINEWLINES.Substring(0, Math.Max(down,0) * 2);

where

private const string MULTINEWLINES =
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n";

(I added a little bit extra just in case)
Yes!!
Good idea.
Thanks.
Zach.
 
Z

Zach

Arne, yes the form needs to be visible. But that wasn't the essence: I had
the Show() in the parent, not in the child. Additionally the child needed a
Show(); Zach.
 
A

Arne Vajhøj

yes the form needs to be visible. But that wasn't the essence: I
had the Show() in the parent, not in the child. Additionally the child
needed a Show();

How do you start the app?

Application.Run should do all that stuff.

Arne
 
Z

Zach

WWW: pearltree.org
Arne Vajhøj said:
How do you start the app?

Application.Run should do all that stuff.

Arne

I call the form with "new PearlMessage(text).Show();"

:( ?

Zach
 
Z

Zach

Arne Vajhøj said:
Is PearlMessage your main form?

Arne
int x1 = this.Location.X;
int y1 = this.Location.Y;
int heiht = this.Height;
int width = this.Width;
PearlMessage cs = new PearlMessage(messages[22].ToString());
int x2 = cs.Width;
int y2 = cs.Height;
int x3 = (x1 + x2/2) ;
int y3 = (y1 + y2/2) ;
cs.Owner = this;
cs.Location = new Point(x3,y3);
cs.Show();
 
Z

Zach

WWW: pearltree.org
Is PearlMessage your main form?

Arne
Re previous:

Sorry, saw two redundant code lines.

Zach.

int x1 = this.Location.X;
int y1 = this.Location.Y;
PearlMessage cs = new PearlMessage(messages[22].ToString());
int x2 = cs.Width;
int y2 = cs.Height;
int x3 = (x1 + x2/2) ;
int y3 = (y1 + y2/2) ;
cs.Owner = this;
cs.Location = new Point(x3,y3);
cs.Show();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top