Concatenate some strings into a global variable in C# - c#

i'm having a problem . I'm trying to make an application in C# (Windows Form Application) and i'm stuck into something.
I want to do a log-in form like in the photo. For username i'm having a combo-box where i can choose the user , but for password i want to place some buttons(like in the photo) , and when i press the button 1 for example , i want to have a string which is 1 .After that i will place 2 ,and i want to concatenate 1 and 2 into that string . Do you know how can i do that

In your case, I believe you have one event for each button. In each event you could just sum the values to a global variable or directly change the textbox. Like this:
txtPassword.Text += "0"; //the 0 button
It will change the textbox value and when you click "Sign in" you can get the txtPassword.Text as the final string.
You can also declare a string or StringBuilder globally and append the value in every button click, tho this won't change the textbox text directly.

Use a StringBuilder to concatenate strings.
StringBuilder builder = new StringBuilder();
builder.Append(a);// where "a" is your input character
And whenever you get a new character input call builder.Append(a);
In the end you get your final string by calling builder.ToString();

As a variation on the answer from #LeoFormaggi, you could have a single event handler for all the numeric buttons (i.e., one that every button's click handler points to). Then, in the handler:
var buttonText = (sender as Button)?.Text;
txtPassword.Text += buttonText ?? String.Empty;
Don't forget to make that textbox be a password field (by setting the PasswordChar property).
You don't want to bother with a StringBuilder here. It's normally the right tool for concatenating strings, but it buys you nothing in this case since you want the result to appear immediately in the text box. In this case it would simply add overhead for no good reason.

i want to thank you, i made my solution like this :
`private void nb6_Click(object sender, EventArgs e)
{
password_box.Text += "6";
}`
in every button event that i have, the password_box (which is my textbox) and i concatenate everytime the string that i want(in this case is number 6 )
Thank you and have a nice day . :)

Related

How do i make show/hide password

I've been doing this function, and i would like to ask, whats the problem behind this one? I Made if statement, and nothing happens, the chad doesn't change, i tried to make Application.Exit on the if function, and the app exited. I wanna to make that Show Password/Hide Password function on MetroTextBox
if (metroTextBox1.PasswordChar == '●')
{
metroTextBox1.PasswordChar = '\0';//
}
else
{
metroTextBox1.PasswordChar = '●';
}
I wanna to make that Show Password/Hide Password function on
MetroTextBox
void showPassword(TextBox textBox, bool show)
{
if(show)
textBox.PasswordChar = '\0';
else
textBox.PasswordChar = '*';
}
Now, anytime you want to show the password, call it like this:
showPassword(metroTextBox1, true);
Hide the password:
showPassword(metroTextBox1, false);
You can call this from a toggle box.
Use the text changed method of your textbox and to capture new inputs froms the user and validate if the key pressed corresponds to a printable character. You can apend this to a local variable and clear the text field, this way you capture the password while keeping the field empty. I think this is the idea you have
Have you tried to add button_Click ? Double click on the button ,and it will be automatically created, then you just have to paste your code!
Sorry if i was bad, but it's working fine for me!

C# Increment for loop every time button is pressed

I googled a few things before posting, but I couldn't find anything like this. Basically, I want to take text from a textbox, save as a variable (say history1) to then be able to call that in the future to display the text. I can do that, but what I'm stuck with is that I want 3 variables (history1, history2 and history3, for example) and each time the button is pressed the string is moved to the next variable.
For example, the button is pressed, the text is saved as variable history1. The text is changed and the button is pressed again, the text from history1 is moved to variable history2, and the new text is saved as history1. This would only need to work for 3 instances though, not infinitely, so when text is stored in history3 and the button is pressed the text is just overwritten.
The way I had thought of approaching this was:
string history1;
string history2;
string history3;
for (int i = 1; i < 4; i++)
{
history1 = txtOutput.Text;
btnToFile_Click()
{
history2=history1;
btnToFile_Click()
{
history3=history2;
}
}
}
However, this isn't going to work because the btnToFile_Click doesn't take any arguements. Is there an easier way to go about this or just a way to fix the method not taking arguements ?
Thanks in advance!
Make sure that you delcare history1, history2, and history3 on the form level (not inside any method).
Then, have the following code inside the handler of the click event of the button:
history3 = history2;
history2 = history1;
history1 = txtOutput.Text;
You don't need to call the btnToFile_Click() method multiple times in your loop, just move the text from end textbox to another in reverse order. Nor do you need a loop because you only have three textboxes.
Why reverse order? So you move the value to the next textbox before it is overwritten by the new value.
So:
history3 = history2;
history2 = history1;
history1 = txtOutput.Text;
btnToFile_Click() is a Click event handler for btnToFile (a button). You're not supposed to call that method yourself, it's called by the UI framework (say WPF or WinForms etc.). By the way, it does receive a parameter, then event source (since you can assign the same event handler to multiple buttons and do something based on which one sent the event)
You can try saving in a string array and move the strings within it when you call the button clicked event

How can i make to label return values in C#?

So guys i am new in C# and i want to know how to make to label return some values.
One example :
Label1 will be 75 dollars when i click in radiobutton.
When i choose the radiobutton, will change the value of 0 to 75 dollars in the painel,and will be added to the value of the buy.
I already tried some stuffs but didn't worked,i am really freaking out with this.
Please help, I need to do that for the Course of Programming.
You can use Text property for set and get value from label, such as:
int myIntVariable1 = 934;
myLabel.Text = myIntVariable1;
int myIntVariable2 = Convert.ToInt32(myLabel.Text);
Now myIntVariable1 and myIntVariable2 will have 934 value
A label cannot return a value,
if you're using a radio button \ button group, you could need to fire an CheckedChanged event, easiest way would be get the radio buttons on your form, double click for the event to get generated, then use a switch statement to have the label changed
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
label1.Text = "your new value or text here";
}
Also, just as a note of interest for you and the course you're doing, sign up to PluralSight which has mass of courses, and will teach you all you need to know about the fundamentals of C# and much more. You can get a free 10 or 14 day trial and then you can sign up from $29 per month and cancel at any time.

Getting a value before it is changed

I am having trouble with getting a value in a text box before the value is changed either by user input or programmatically.
I figure it has to do with the _TextChanged event but once this executes how do I get the old value which was already there before the change?
For example number 3 is in the text field. The number then changes to 4 how do I save 3?
Thanks in advance!
Do not store anything in the TextBox. Use it only to display/edit value.
private string value;
private void textBox1_TextChanged(object sender, EventArgs e)
{
// at this moment value is still old
var oldValue = value;
value = ((TextBox)sender).Text; // text1.Text
// here you have oldValue and new value
}
Maybe you don't want to update value after each TextChanged. Maybe you will do it when form is closed, some button is pressed, etc.
Anyhow idea should be clear: if you need to have old value, then store it somewhere yourself (as a variable, as a Tag, as application setting, exporting, etc.).
Also, take care about how user will work. If TextBox contains 3, then user may first press 4 and only then delete 3. Or user may accidentally press 54. Which of those will be old value: 3, 34, 54 ? Simply to demonstrate you, what there could be consequent problems to having concept of old value. Mayhaps you don't want it and there is better solution to what you are trying to solve by getting old value.
Why not save the current value in the textbox in a variable during the textchanged event. That way, it is the "previous" value when the event is fired again.
You can save the value of textbox in some variable.
For example
string currentVal = txtName.Text;
string currentVal can be used as a previous value in text_changed event.
Have you tried making a timer. Every tick you write the input to a string. If the input of the textbox is not equal to the content in the string, you trigger what you wanted to do. That way you can trigger a void with the old input.
private string oldInput;
....
void TimerTick(Object source, ElapsedEventArgs e)
{
if (oldInput != MyTextBox.Text) MyVoid(oldInput);
oldInput = MyTextBox.Text;
}

How to automatic add the first line of text from a list box to a text box

Right I have got a list box which contains a list of tracks and when the track is pressed it moves to a second list box, what I now need to happen is have the first item that's in the second list box move automatic to a text box.
This is my current code for the first move
private void genreListBox_DoubleClick(object sender, EventArgs e)
{
playlistListBox.Items.Add(genreListBox.SelectedItem);
}
I am thinking it should be something like this
presentlyPlayingTextBox.AppendText(playlistListBox.);
But I'm not sure how to add the first line without clicking it.
I have tried this but I get an error for the value.
presentlyPlayingTextBox.Text = playlistListBox.SelectedItem.Value;
Normally you would use something like the 'SelectedIndexChanged' or 'SelectedValueChanged' action of a listbox, otherwise events like DoubleClick will fail if the keyboard is used to change the selection.
Also that event should be triggered on the second listbox when it is changed by the first.
EDIT:
On a standard Listbox, there is no .SelectedItem.Value, only .SelectedItem.
However as a listbox holds objects, not just text, you need to say you want the text value.
presentlyPlayingTextBox.Text = playlistListBox.SelectedItem.ToString();
I don't know if I fully understand your question, but if you're just attempting to move the first item from ListBox_2 to a textbox, would the following work?
presentlyPlayingTextBox.Text = playlistListBox.Items[0]?.Value; // C# >= 6
presentlyPLayingTextBox.Text = ((playlistListBox.Items == null) ? playlistListBox.Items[0].Value : "" ); // C# < 6
You could also create your own delegate/event that would fire when the user did some action.

Categories