Control the position of the vertical scrollbars - c#

I have a multi-line textbox (let's call it textBox1) that has plenty of text inside.
After doing a search, I highlight the string I was looking for with:
textBox1.SelectionStart = textBox1.Text.ToLower().IndexOf(STRING);
textBox1.SelectionLength = STRING.Length;
Now when I call the form that contains the textbox it highlights the selected text, but what I would like to do is that the scrollbars would scroll automatically to the highlighted text.
I tried
textBox1.ScrollToCaret();
But didn't work.
Any ideas?
Thank you.

What event are you firing this from? The Form probably isn't in a state where it can process this. If you call from Form.Load it will be too soon. If you call from Form.Shown, it should work properly.
private void Form1_Shown(object sender, EventArgs e) {
var STRING = "Suspendisse mi risus";
textBox1.SelectionStart = textBox1.Text.IndexOf(STRING);
textBox1.SelectionLength = STRING.Length;
textBox1.ScrollToCaret();
}

Related

Using MouseHover event and ToolTip

To show the relevant information(in monopoly game, the property belongs which player, current market price etc.), I put a Label on the top of a panel, and used a ToolTip object to display the information. This is the image of my current setup.
Here are the steps I have done:
1.Added MouseHover event handler (The Label name is MEDITERANEAN)
this.MEDITERANEAN.MouseHover += new System.EventHandler(this.MEDITERANEAN_MouseHover);
2.Initialized Tooltip
private void InitializeToolTip()
{
toolTipLabel.ToolTipIcon = ToolTipIcon.Info;
toolTipLabel.IsBalloon = true;
toolTipLabel.ShowAlways = true;
}
3.Call setToolTip() in MouseHover call back function
private void MEDITERANEAN_MouseHover(object sender, EventArgs e)
{
toolTipLabel.SetToolTip(MEDITERANEAN, "You put mouse over me");
rolledDice.AppendText("Mouse Over");
}
But when I start application and move my cursor over the label, there is no text from toolTipLabel. What part did I make mistakes?
Interestingly, i made other function and it works.
private void panelBoard_MouseOver(object sender, EventArgs e)
{
toolTipLabel.SetToolTip(panelBoard, "You put mouse over me");
rolledDice.AppendText("Mouse Over");
}
I think you just need to bring your lable control in front of image. Try something like this .
MEDITERANEAN.BringToFront();
I found the solution, first I should set Panel's property "Enable" to true, then set label's property "visible" to true as well.

How do I highlight the text in a textbox at the startup of a window?

I asked a question similar to this a few days ago here. The answer works fine, with the exception of performing the same operation at the start up of the window. This means that I need to have the text in a textBox highlighted every time the window is opened.
Currently I am setting the focus to the textBox at startup with no problem in the constructor. With that being said, I am guessing that the correct area to perform this operation is in the constructor. This is what I am trying currently with no luck:
public AddDestination()
{
InitializeComponent();
//Give cursor focus to the textbox
destination_textBox.Focus();
//Highlights text **DOES NOT WORK
destination_textBox.SelectionStart = 0;
destination_textBox.SelectionLength = destination_textBox.Text.Length;
}
How can I make it so that the text inside my textBox is highlighted whenever the window opens?
Instead of constructor, move it to AddDestination_Load event.
public AddDestination()
{
InitializeComponent();
}
private void AddDestination_Load(object sender, EventArgs e)
{
//Give cursor focus to the textbox
textBox1.Focus();
//Highlights text **DOES NOT WORK
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Length;
}
You can write this segment of code in Load EventHandler
//code
InitializeComponent();
//code
private void Form_Load(object sender, EventArgs e)
{
//Give cursor focus to the textbox
destination_textBox.Focus();
//Highlights text **DOES NOT WORK
destination_textBox.SelectionStart = 0;
destination_textBox.SelectionLength = destination_textBox.Text.Length;
}
To do so go to Designer, click on the window, go to Properties, click Events button, search for Load event then double-click on it.

Display picture only if textbox is blank

My Program: I have a textBox and a pictureBox (which contains an error picture, placed exactly next to textBox) in userControl in my program.
My Goal: I want to HIDE the picture in the pictureBox only if the user types text in the textBox. If the textBox is left blank, the image in the pictureBox should be shown.
I tried using errorProvider but I was totally lost because I am a newbie in C# Programming. There are many errorProvider examples online but all examples are using Form and I am trying to do it in UserControl. So, I thought I should try this method. Please can you help me with the code? Thanks for your help in advance.
ANSWER:
Sealz answer works! My program will be working offline. So, this one also works:
if (String.IsNullOrEmpty(textBox1.Text))
{
//Show Picture
pictureBox2.Visible = true;
}
else
{
//Hide Picture
pictureBox2.Visible = false;
}
Thanks everybody for looking at my question! You all are awesome. =)
You can use IsNullOrEmpty
if (String.IsNullOrEmpty(textBox1.Text))
{
//Show Picture
pictureBox1.ImageLocation = "locationofimg";
}
else
{
//Hide Picture
pictureBox1.ImageLocation = "";
}
To get fancy with it.
On form_Load() set the picturebox to nothing
private void Form1_Load(object sender, EventArgs e) {
pictureBox1.ImageLocation = "";
}
Then in the Textbox Change Method
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox1.Text))
{
pictureBox1.ImageLocation = "";
}
else
{
pictureBox1.ImageLocation = "Image\Location.com.etc";
}
}
This will make the box empty to start with no image and as you type it will pop up. If the boxes text is deleted fully the image will vanish.
Just test if the textbox has any text, and set the property accordingly.
pictureBox1.ImageLocation = (textBox1.Text.Length > 0) ?
"imagefile" : String.Empty;
If this needs to update dynamically, just perform this action in the textbox's TextChanged event.

How to disable cursor in textbox?

Is there any way to disable cursor in textbox without setting property Enable to false?
I was trying to use ReadOnly property but despite the fact that I can't write in textbox, the cursor appears if I click the textbox. So is there any way to get rid of this cursor permamently?
In C#, you can use the following read-only textbox:
public class ReadOnlyTextBox : TextBox
{
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public ReadOnlyTextBox()
{
this.ReadOnly = true;
this.BackColor = Color.White;
this.GotFocus += TextBoxGotFocus;
this.Cursor = Cursors.Arrow; // mouse cursor like in other controls
}
private void TextBoxGotFocus(object sender, EventArgs args)
{
HideCaret(this.Handle);
}
}
In C# you can disable the cursor in a textbox by temporarily disabling and then re-enabling the text box whenever it receives the focus. Note there is no need to make the textbox read only if using this method. For example:
private void TextBox_Enter(object sender, EventArgs e)
{
TextBox.Enabled = false;
TextBox.Enabled = true;
}
You could use a Label instead. When in the designer, you set BorderStyle = Fixed3D, BackColor = Window and AutoSize = False, it looks a lot like a TextBox.
However, the cursor in a TextBox is provided so that the user can scroll through the text when it is longer than the box. You'll lose that functionality with a Label, unless you are sure that it will always fit. Other than that, it is not possible to remove the cursor from a TextBox.
Putting the hideCaret function inside the TextChanged event will solve the problem:
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
private void textBox1_TextChanged(object sender, EventArgs e)
{
HideCaret(textBox1.Handle);
}
Easiest solution for me was to just override the on focus event and focus back to the parent. This prevents the cursor and any editing of the textbox by the user and basically disables the text box with out having to set the Enabled = false property.
private void Form1_load(object sender, EventArgs e) {
textBox1.ReadOnly = true;
textBox1.Cursor = Cursors.Arrow;
textBox1.GotFocus += textBox1_GotFocus;
}
private void textBox1_GotFocus(object sender, EventArgs e) {
((TextBox)sender).Parent.Focus();
}
Like #Mikhail Semenov 's solution, you can also use lambda express to quickly disable the cursor if you do not have many textboxes should do that:
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
textBox1.ReadOnly = true;
textBox1.BackColor = Color.White;
textBox1.GotFocus += (s1, e1) => { HideCaret(textBox1.Handle); };
textBox1.Cursor = Cursors.Arrow;
You can set it programatically.
textBox1.Cursor = Cursors.Arrow;
This is not strictly an answer to the question, but perhaps it can solve some similar problem(s). I use a textbox control which can look like a label for a control which displays a scale, but can be edited, when clicked. Start enabled = false and make an activation (enabled = true) in a mousehandler of the parent of the textbox control (which, when disabled, border None and backcolor = parent backcolor, looks like a label). E.g. when enter hit or other event, disable again in KeyDown handler.
(Of course the parent mouse click routine can check whether the mouseclick really occured in the label/textbox control).
If you need the textbox control to activate by tabbing, some more work is required (than I have done).
I use the form constructor to find the textbox parent at runtime and to apply the delegate mouse control. Perhaps you can do this as wel in compile time (Form header), but that seemed a little error-prone to me.
One way of doing it is using View + TabIndex, you can do indexing of some other controls on the dialog as first, let say for buttons if there any. Then as if the control tabIndex is not the first i.e 0, cursor won't get appear there.
To disable the edit and cursor in the TEXT BOX
this.textBox.ReadOnly = true;
this.textBox.Cursor = Cursors.No;//To show a red cross icon on hover
this.textBox.Cursor = Cursors.Arrow //To disable the cursor
you can use RightToLeft Property of Text Box, set it to true, you will not get rid of the Cursor, but it will get fixed at right corner and it will not appear automatically after every text you type in your text Box. I have used this to develop an application like Windows Calculator.

Add character counter to MemoExEdit control

I am trying to make a character counter, 40/200...41/200 and so on. Now for a textbox control I am hooking into the KeyUp Event with something like this...
public static void GetRemainingChars(MyTextBox txt, LabelControl lbl)
{
var maxChars = txt.Properties.MaxLength;
lbl.Text = txt.Text.Length + "/" + maxChars;
}
Unfortunately the MemoExEdit control has a popup window you type the text into and that seems to be hidden. I tried the KeyUp, EditValueChanging, TextChanged, and they all do the same thing. They don't fire till the user closes the popup. I am guessing that it is a composite control that transfers the editvalue when it closes.
Any ideas on how I can get at the popups events? Is there a different way to do this?
Just because I couldn't find this anywhere else I will post my solution for other's benefit.
Subscribe to the Popup Event of the MemoExEdit control, then inside that subscribe to the EditValueChanging Event. That is where you can hook in. See below for MY working version. Tweaks may be needed for yourself. Also, the Popup Event is created in my Designer.cs file.
private void memContactWith_Properties_Popup(object sender, EventArgs e)
{
MemoExPopupForm popupForm = (sender as DevExpress.Utils.Win.IPopupControl).PopupWindow as MemoExPopupForm;
MemoEdit me = popupForm.Controls[2] as MemoEdit;
me.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(me_EditValueChanging);
}
void me_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
var memo = (sender as MemoEdit);
var maxChars = memo.Properties.MaxLength;
lblContactWithCharCount.Text = memo.Text.Length + "/" + maxChars;
}

Categories