Using a Label vs using a Textbox [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have a C# Application where I am using Serial communication with a Microcontroller to Display data on the application. I have used a text box to display the data :
public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
DataRec = serialPort1.ReadExisting();
int.TryParse(DataRec, out myTotal);
this.BeginInvoke(new EventHandler(DisplayText));
}
catch(NullReferenceException)
{
//catching the exception
}
}
public void DisplayText(object sender, EventArgs e)
{
textBox2.Text = myTotal.ToString();
}
Instead of TextBoxes I have also tried using labels. I get the same result and I didnt see any change in performance. I am using labels because I didnt want the user to think they can edit the values in the textboxes.
I have tried searching for the advantages of one over the other. So far the ones I have seen is :
Textboxes need to be set as readonly whereas in labels you dont need to do that.
Even when I set the textboxes as read only the Cursor is still visible whereas in Label it isn't.
What are some of the pros and cons in terms of performance, while using either a Label or Textbox?
Is it ok if I use labels ?

There are a few pro's and con's to both.
Label
Pro's:
Text is not copy able
Cursor does not change
Sets size based on text (if autosize is on, I think its on by default)
Option to align text to the right (autosize off)
Con's:
Text is not selectable/copy able
Text might outgrow form/parent with autosize
TextBox
Pro's:
Text is copy able
Fixed size (also a con)
Con's:
Does not autosize
Height not adjustable (does not apply to rich textbox/multiline = true)
My opinion:
In my opinion you should use a label if the user shouldn't be able to copy the data. The exception to this is if you have just 1 un-editable value and all others are textbox's, then you should just make a readonly textbox.

You have already answered your own question. It seems clear to me that a label is more appropriate in this case. You could argue that later that you may need the extra functionality that the text box provides but you should consider the YAGNI principle.
As to pros and cons, the user will probably believe they are able to edit the value in the text box, they wont make that mistake using a label.

Related

How to swap two images in Xamarin Forms [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have created a currency converter app in Xamarin forms in which we have two Image controls that contain the country flags. Now, if the user clicks the swap button, the flags swap each other.
For example, the source of the first image will go to the second image, and vice versa.
<Image Source="usaflag.png" x:Name="Img1"/>
<Image Source="australiaflag.png" x:Name="Img2"/>
<Button Text="SWAP" x:Name="BtnSwap" Clicked="BtnSwap_OnClicked"/>
Here's my XAML Code. I know the we will write the swapping code in the code behind file. I am not using the MVVM style pattern so if you've any code regarding swapping then kindly share it with me.
Okay, so you want to swap two images in xamarin forms. Well, that's pretty simple!
You just need to add this code inside the OnClick() event of a button.
private void BtnSwap_OnClicked(object sender, EventArgs e)
{
var firstImage = Img1.Source;
var secondImage = Img2.Source;
Img2.Source = firstImage;
Img1.Source = secondImage;
}
In this case, you can Bind the sources and change those values on BtnSwap_OnClicked.

What component should I use to display enabled/disabled state? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have been wondering what component I should use to display state of enabled/disabled behaviours in WinForms.
For example I have function that returns state of 2 different processes.
And I want to display like green and red color on GUI, so that user could easily know which process has wich state.
In Java I have used ProgressBar, like setting it to 100 and 0, so it represents the state of that process. But as for now, I have moved to C#, so I would like to know what components you use for this purpose, maybe there is something better to use.
Any component with a background color property will do. I suggest you use Panel and switch the background color according to your state.
You didn't specify the technology, but CheckBox is one option in both WinForms and WPF. Plus you can use Label too (with BackColor / Background properties). Finally if you want to go fancy, you can use animations in WPF.
You could either use a Panel and change its background color or (more intuitively) a read-only CheckBox.
A progress bar is not really a good choice, unless you put it in "marquee" state when something is currently active and hide it when it is not. Otherwise a progress bar is meant to display ... well ... progress.
So this comes down to:
If you want to show that some process is activated/deactivated, for example by some sort of configuration, or simply "not performing a task right now", I'd use either a Panel with a background color or a CheckBox.
If you want to show that some process is currently working (without knowing the exact progress), I'd use a ProgressBack with a "marquee" state (also called "indeterminate").

Visual studio - toggle switch hiding text blocks? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am doing a school project and i canĀ“t figure out how to "hide" some text blocks when the toggle switch is on and the opposite? Developing a windows 8 app. Thanks and btw. How do you make a collection from multiple text blocks(XAML)?
private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
{
}
Assuming your control structure is fairly flat, you can get by using the Tag property on the TextBox. In your XAML, put some distinct value in the Tag field for each TextBox you want to make toggle-able, like the word 'CanToggle'. Then you can do something like
private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
{
foreach (Control currentControl in this.Children)
{
if (currentControl.Tag == "CanToggle")
currentControl.Visible = !currentControl.Visible;
}
}
If your control collection is not flat, then you'll have to figure out how to dig recursively through the collection of controls to locate all the TextBox that you want to toggle. This answer may help.
Visual Studio Main Menu - Edit - Outlining - Toggle All Outlining: Ctrl+M, Ctrl+L
Personally, I use Ctrl+M to "collapse to defininitions" more than anything else though.

How to convert Text into upper case in textbox? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have created a text box which can accept only one character. When the user types anything into the box, I want it converted into upper case.
Set TextBox.CharacterCasing to CharacterCasing.Upper.
textBox1.CharacterCasing = CharacterCasing.Upper;
Right-click your textbox, and then click Properties.
In the Properties window, locate the CharacterCasing property, and then click to select Upper from the list.
(Source: http://support.microsoft.com/kb/818363)
Use KeyDown event and check the input key. Then use String.ToUpper(). For example:
private void YourTextBox_KeyDown(object sender, KeyEventArgs e)
{
YourTextBox.Text = YourTextBox.Text.ToUpper();
}
string lower = "converted from lowercase";
Console.WriteLine(lower.ToUpper());
Take a look at this: How to convert strings to lower, upper, or title (proper) case by using Visual C#
You could either change the textbox's casing to upper case like this:
TextBox.CharacterCasing = CharacterCasing.Upper;
Or you could create an event which is executed when the textbox's text has changed.
TextBox.Text = TextBox.Text.ToString().ToUpper();
And so whenever someone types something inside of the textbox it will be changed to upper case, but I do not think that this is a good idea. I recommend using my first advice.

Textbox with specific numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to create a TextBox in windows form application in visual studio that contains only specific numbers. for example i want one textbox to be able to contain only numbers between 0 and 1. If its any other value I want a message to appear that says wrong number. The app is in written in C#.
I have seen this example
C# Numeric Only TextBox Control
but it is for numeric and i want specific numeric.
Edit:My mistake i wasn't clear enough. i tried the NumericUpDown but it doesn't work for all examples. In the second box i want to have values from 20 to 340 but only the .5 floating points between (20,20.5,21 etc) and if i use NumericUpDown, if the user inserts 22.6 the cell accepts it and i don't want that. That's why i ask if it's possible to control the value inserted with a Textbox or a MaskedTextBox and if it's eligible then for the user to be able to enter it the box. Hope i am clear enough now.
You should use a NumericUpDown.
You can use it like this;
NumericUpDown control = new NumericUpDown();
control.Minimum = 0;
control.Maximum = 1;
control.DecimalPlaces = 2; // or something you want.
control.Increment = .01; // step is .01
create user control inheriting TextBox and add an event for KeyPress as follows
public class MyTextBox:TextBox
{
List<int> numberstobeallowed = Enumerable.Range(20, 340 - 20).ToList();
public MyTextBox()
{
this.Leave += new EventHandler(MyTextBox_Leave);
}
void MyTextBox_Leave(object sender, EventArgs e)
{
if (numberstobeallowed.Contains(Convert.ToInt32(this.Text)))
this.Text = string.Empty;
}
}
hope this helps

Categories