EzDevInfo.com

textbox interview questions

Top textbox frequently asked interview questions

Calculate text width with JavaScript

I'd like to use JavaScript to calculate the width of a string. Is this possible without having to use a monospace typeface?

If it's not built-in, my only idea is to create a table of widths for each character, but this is pretty unreasonable especially supporting Unicode and different type sizes (and all browsers for that matter).


Source: (StackOverflow)

How to detect a textbox's content has changed

I want to detect whenever a textbox's content has changed. I can use the keyup method, but that will also detect keystrokes which do not generate letters, like the arrow keys. I thought of two methods of doing this using the keyup event:

  1. Check explictly if the ascii code of the pressed key is a letter\backspace\delete
  2. Use closures to remember what was the text in the textbox before the key stroke and check whether this has changed.

Both look kinda cumbersome.


Source: (StackOverflow)

Advertisements

Set the focus on a textbox in xaml wpf

Despite some posts on this forum and others i cannot find something that tells me how to set the focus on a TextBox.

I have a userControl with many labels and textBoxes. When the form is loaded I want the a particular textBox to have the focus.

I have set the tabIndex but that didn't seem to work.

Any suggestions?


Source: (StackOverflow)

Multiline for WPF TextBox

I am developing an app for sending some feedback.

Basically I'm trying to make a TextBox for comments, but I'm used to the WinForms MultiLine=true. I've set MinLines to 3, which is getting there, but preferably I'd like it if the user is able to type wherever in this block - like press enter and do dot points sort of thing. For example:

- Item 1        blah
- Item 2                blahlb lahbvl   d

But at the moment the text all stays on one line.

- Item 1         blah - Item 2                      blahb blahb blah

These comments will then help fill the body of an email which is sent. It may be pointless if I can't easily keep the same formatting when putting this string into the email body string (so that it looks like it does when sent as it does when typed).

Can I achieve what I'm after or do I have to leave it as all text on one line?


Source: (StackOverflow)

How do I make a textbox that only accepts numbers?

I have a windows forms app with a textbox control that I want to only accept integer values. In the past I've done this kind of validation by overloading the KeyPress event and just removing characters which didn't fit the specification. I've looked at the MaskedTextBox control but I'd like a more general solution that could work with perhaps a regular expression, or depend on the values of other controls.

Ideally this would behave such that pressing a non numeric character would either produce no result or immediately provide the user with feedback about the invalid character.


Source: (StackOverflow)

How do I automatically scroll to the bottom of a multiline text box?

I have a textbox with the .Multiline property set to true. At regular intervals, I am adding new lines of text to it. I would like the textbox to automatically scroll to the bottom-most entry (the newest one) whenever a new line is added. How do I accomplish this?


Source: (StackOverflow)

How to remove the focus from a TextBox in WinForms?

I need to remove the focus from several TextBoxes. I tried using:

textBox1.Focused = false;

Its ReadOnly property value is true.

I then tried setting the focus on the form, so as to remove it from all the TextBoxes, but this also fails to work:

this.Focus();

and the function returns false when a textbox is selected.

So, how do I remove the focus from a TextBox?


Source: (StackOverflow)

Set focus on textbox in WPF from view model (C#)

I have a TextBox and a Button in my view.

Now I am checking a condition upon button click and if the condition turns out to be false, displaying the message to the user, and then I have to set the cursor to the text box control.

if (companyref == null)
{
Lipper.Nelson.AdminClient.Main.Views.ContactPanels.CompanyAssociation cs = new Lipper.Nelson.AdminClient.Main.Views.ContactPanels.CompanyAssociation(); 

MessageBox.Show("Company does not exist.", "Error", MessageBoxButton.OK,
                    MessageBoxImage.Exclamation);

    cs.txtCompanyID.Focusable = true;

    System.Windows.Input.Keyboard.Focus(cs.txtCompanyID);

}

The above code is in the view model.

The CompanyAssociation is the view name.

But the cursor is not getting set in the TextBox.

The xaml is as under

<igEditors:XamTextEditor KeyDown="xamTextEditorAllowOnlyNumeric_KeyDown" 
  Name="txtCompanyID" ValueChanged="txtCompanyID_ValueChanged"  Text="{Binding 
  Company.CompanyId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="{Binding 
  ActualWidth, ElementName=border}" Grid.Column="1" Grid.Row="0" 
  VerticalAlignment="Top" Margin="0,5,0,0" HorizontalAlignment="Stretch"
  IsEnabled="{Binding Path=IsEditable}" />

<Button Template="{StaticResource buttonTemp1}" Command="{Binding ContactCommand}" 
  CommandParameter="searchCompany" Content="Search"  Width="80"  Grid.Column="2" 
  Grid.Row="0" VerticalAlignment="Top" Margin="0" HorizontalAlignment="Left"  
  IsEnabled="{Binding Path=IsEditable}" />

Please help


Source: (StackOverflow)

Set cursor position in html textbox [duplicate]

This question already has an answer here:

Does anybody know how to move the cursor in a textbox to a particular position?

For example, if a text-box (e.g. input element, not text-area) has 50 characters in it and I want to position the cursor before character 20, how would I go about it?

Jon


Source: (StackOverflow)

What is a reasonable length limit on person "Name" fields?

I have a simple webform that will allow unauthenticated users to input their information, including name. I gave the name field a limit of 50 characters to coincide with my database table where the field is varchar(50), but then I started to wonder.

Is it more appropriate to use something like the Text column type or should I limit the length of the name to something reasonable?

I'm using SQL Server 2005, in case that matters in your response.

EDIT: I did not see this broader question regarding similar issues.


Source: (StackOverflow)

Making a TextBox behave like your browser's address bar

When a C# WinForms text box receives focus, I want it to behave like your browser's address bar.

To see what I mean, click in your web browser's address bar. You'll notice the following behavior:

  • Clicking in the textbox should select all the text if the textbox wasn't previously focused.
  • Mouse down and drag in the textbox should select only the text I've highlighted with the mouse.
  • If the textbox is already focused, clicking does not select all text.
  • Focusing the textbox programmatically or via keyboard tabbing should select all text.

I want to do exactly this in WinForms.

FASTEST GUN ALERT: please read the following before answering! Thanks guys. :-)

Calling .SelectAll() during the .Enter or .GotFocus events won't work because if the user clicked the textbox, the caret will be placed where he clicked, thus deselecting all text.

Also, calling .SelectAll() during the .Click event won't work because the user won't be able to select any text with the mouse; the .SelectAll() call will keep overwriting the user's text selection. )


Source: (StackOverflow)

Any way to make a WPF textblock selectable?

I want to make the text displayed in the Witty, an open source Twitter client, selectable. It is currently displayed using a custom textblock. I need to use a TextBlock because I'm working with the textblock's inlines to display and format the @username and links as hyperlinks. A frequent request is to be able to copy-paste the text. In order to do that I need to make the TextBlock selectable.

I tried to get it to work by displaying the text using a read-only TextBox styled to look like a textblock but this will not work in my case because a TextBox does not have inlines. In other words, I can't style or format the text within a TextBox individually like I can with a TextBlock.

Any ideas?


Source: (StackOverflow)

Open file dialog and select a file using WPF controls and C#

I have a TextBox named textbox1 and a Button named button1. When I click on button1 I want to browse my files to search only for image files (type jpg, png, bmp...). And when I select an image file and click Ok in the file dialog I want the file directory to be written in the textbox1.text like this:

textbox1.Text = "C:\myfolder\myimage.jpg"

Source: (StackOverflow)

JavaScript set focus to HTML form element

I have a web form with a text box in it. How do I go about setting focus to the text box by default?

something like this:

<body onload='setFocusToTextBox()'>

so can anybody help me with it? I don't know how to set focus to the text box with JavaScript.

<script>
function setFocusToTextBox(){
//What to do here
}
</script>

Source: (StackOverflow)

Watermark / hint text / placeholder TextBox in WPF

How can I put some text into a textbox which is removed automatically when user types something in it? (In WPF)


Source: (StackOverflow)