MudAutoComplete : Problem to get current text without any suggest selection - c#

I'm using MudAutoComplete to implement suggestions based on user entering text, It works perfectly.
My problem is that when user write a text & press Enter, in KeyDown eventHandler i've get the first suggestion in the list instead of current text which user entered!!
For example, when user enter "Alfki", The following suggestions show :
Hi Alfki
Alfki Bye..
Alfki Bye 2
Now, when user hit Enter after Alfki, In KeyDown eventHandler, I've got "Hi Alfki" in bound-value instead of "Alfki"!
Where is my problem and how to solve it?
Thanks ...
Updated ..
Here is my sample code :
<MudAutocomplete T="string"
Variant="Variant.Outlined"
#ref="#_txtAutoCompelete"
ResetValueOnEmptyText="true"
CoerceText="true"
#bind-Text="_strSearchQuery"
SearchFunc="#SearchQuery"
OnKeyUp="#SearchQueryResult"
Adornment="Adornment.End"
AdornmentIcon="#Icons.Filled.Search"
AdornmentColor="Color.Success">
</MudAutocomplete>
#code{
private string _strSearchQuery;
private async Task SearchQueryResult(KeyboardEventArgs e)
{
if (e.Code == "Enter" || e.Code == "NumpadEnter")
{
string strText = _txtAutoCompelete.Text; // first suggestion, not current text!
string strValue = _txtAutoCompelete.Value; // first suggestion, not current text!
string strCurrentText = _strSearchQuery; // first suggestion, not current text!
}
}
}

Related

if statement to check if the user has left the text box empty - issue

I have a web form with a text box where the user enters a URL so by default I have put an https:// but if the user does not enter a URL after and leaves it empty I want to show an error. I have used
if (textBox1.TextLength <= 9)
{
// code
}
Instead of Length check, use equality operator with .Text property to check exact text in the text box.
//This means, there is only "https://" is written in the text box
if(textBox1.Text == "https://")
{
//Show an error. Here user did not enter anything
}
else
{
//Your implementation
}

How to remove/delete Button text on when check button is clicked?

I am making this sum creator where user will have to type an answer using custom keyboard. and on check button click if answer is correct then new question is loaded.
My problem is after answering first question answer button reset to blank but when user types next answer, only one last alphabet is deleted (for example 5 from 15). and when i type 14 it shows 114 (1 from previously typed answer).
I need help to reset answer button text to blank.
I am using buttons because later i want to add more questions at the same time so user will have multiple answers to click and type.
Can anyone please help me on this? Also tell me if this is the right method to achieve what i want.
I am calling backspace function to delete previous answer and also setting text to blank.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Keyboard : MonoBehaviour
{
string word = null;
int wordIndex = -1;
string alpha = null;
string alpha2 = null;
public Text userAnswer1 = null;
public Text valueA, valueB;
public Text scoreCount;
private int a, b, answer1, score;
char[] nameChar = new char[5];
private void Start()
{
SumCreator();
}
public void nameFunc (string alphabet)
{
wordIndex++;
char[] keepchar = alphabet.ToCharArray();
nameChar[wordIndex] = keepchar[0];
alpha = nameChar[wordIndex].ToString();
word = word + alpha;
userAnswer1.text = word;
}
public void BackspaceFunction()
{
if (wordIndex >= 0)
{
wordIndex--;
alpha2 = null;
for (int i = 0; i < wordIndex + 1; i++)
{
alpha2 = alpha2 + nameChar[i].ToString();
}
word = alpha2;
userAnswer1.text = word;
}
}
public void SumCreator ()
{
a = Random.Range(0,15);
b = Random.Range(0,15);
answer1 = a + b;
valueA.text = a.ToString();
valueB.text = b.ToString();
scoreCount.text = "score " + score.ToString();
}
public void CheckAnswer()
{
Text buttonText = userAnswer1.GetComponentInChildren<Text>();
if (answer1 == int.Parse(userAnswer1.text))
{
score++;
// userAnswer1.text = string.Empty;
buttonText.text = string.Empty;
}
SumCreator();
}
}
I've edited my answer and removed the now irrelevant parts.
Once the button "Check" is clicked, first of all erase the text in the result textbox, then do the whole other logic.
To erase the text you can use next piece of code:
Text buttonText = buttonName.GetComponentInChildren<Text>();
buttonText.text = string.Empty;
You probably want to have this "buttonText" property as a global and get it once, at the start of the program instead of getting it every time the button is clicked. It won't do much difference in a small scale program, but it's a right way of thinking.
After checking your code a bit more, I can summarize your problem:
The whole logic of your program is flawed, there're many unnecessary complicated things which make it fail in several places. It is understandable, everybody goes through this stage, nothing to be ashamed or worried about. Either way it's my subjective opinion, which may be wrong.
Back to your code, all you have to do is update your result text, say "txtResult", once anything happens.
Once you click a number, do "txtResult += numberClicked".
Once you click backspace, remove last char of txtResult. Here is a question with many answers on how to do it, it's really simple.
Once you click "Check", in case it's the right number, set txtResult to empty.
Also, every time you update txtResult, you're supposed to update the UI too of course. Let's say you do it every time, it would be one line to update txtResult, and one line to update UI for each of the above 3 cases. So in total 6 lines. A check for an empty string while in "Backspace" function adds another line. My math could be wrong, but either way, it's quite short and simple approach, nothing too complicated.
You just lack relevant knowledge, otherwise you wouldn't be doing that nightmare in your Backspace function.
Regarding the "nameFunc" function, the whole 6 lines could be replaced with "txtResult += alphabet", isn't it? I'm not sure what you get in alphabet parameter, but either way, string is an array of chars, so you can also do "txtResult += alphabet[0]" instead of what you have there.
So, in total, you got it all right, the logic was right, you figured the main aspects. But you over complicated the whole thing. I believe you'll be fine after reading all this text, and wish you the best.
If you want to clear your Text object when you have succesfully entered your answer, you should not call your "BackSpace" function.
Just replace your code to this:
if (answer1 == int.Parse(userAnswer1.text))
{
score++;
userAnswer1.text = string.Empty;
This will clear the text element.
You could also look into using InputFields in Unity, which are designed for entering input and automatically support backspace and other keyboard functions.
If you do, make sure that you set the InputField's ContentType to either Integer Number or Decimal Number

Converting HTML Code in TextBox to display Correctly (after input through a keyboard made in XAML)

I've created a Keyboard for my WPF application in XAML with the functionality of it done in the code behind C#. As of now, any key clicked (or touched), will display the respective key in a TextBox (as expected. However, when handling the HTML Code, the HTML Code gets displayed.
For reference, the button click:
private void btnQUOTATION_Click(object sender, RoutedEventArgs e)
{
HandleKeyboard(""");
}
The HandleKeyboard method:
private bool current spot = false;
private void HandleKeyboard(string key)
{
//Ternary operator
string valueSoFar = currentSpot ? txtBoxTest.Text : "";
string newValue = valueSoFar + key.ToString();
txtBoxTest.Text = newValue;
currentSpot = true;
}
Everything is fine and dandy with the other keys presented, except for quotation marks and apostrophes.
What I'm expecting in the TextBox txtBoxTest when the button for quotation marks is clicked (") is quote marks to show in the text box as quotation marks...instead of the HTML code it is showing now.
Since HandleKeyboard() cannot accept (""") or ("'") ... Is there a way to do this?
You could look to use the Encode and Decode HTML. You will need to use System.Web namespace.
e.g.
string key= "This is a "";
string deCodedKey = System.Web.HttpUtility.HtmlDecode(key);
If you put a breakpoint over the DeCodedKey line you should see it change back to ""

What button pressed inputbox C#

I am using the input box from visual basic in c# and I couldn't figure out how I know what button has been pressed. The input box return the string that has been written.
How I know if the cancel button has been clicked or the OK button?
Thank you very much for the help, I didn't find the answer :)
This is what I tried:
string notineName = Interaction.InputBox("Enter the notice name:", "Enter notice name", "");
If you have another way to do input box ( I wanted to make my own but I don't know how to return what button has been clicked) please write it here.
If the user clicks Cancel, a zero-length string is returned.
Try having a look at this documentation. MSDN
As an alternative you could use dialog boxes.
InputDialog dialog = new InputDialog("Caption Here", "Label Text Here", "Default Textbox String");
if (dialog.ShowDialog() == DialogResult.OK)
{
string result_text = dialog.ResultText;
// use result_text...
}
else
{
// user cancelled out, do something...
}
Here an enum result determines what button was selected.
string a;
a = Interaction.InputBox("message", "message");
if (a.Length > 0)
{
comboBox2.Items.Add(a);
// ok
}
else
{
// cancel
}

C# Text Box File Import

I've got a form with multiple text boxes which are file paths for the program to import data from. Currently they are checked for non-zero length by the following:
//this code imports the files required by the user, as specified in the
//file path text boxes
private void btImport_Click(object sender, EventArgs e)
{
bool hasPath = false;
foreach (TextBox box in this.gbPaths.Controls.OfType<TextBox>().Where(tb => tb.Text.Length > 0))
{
hasPath = true;
//import code
}//end foreach
if (!hasPath)
{
MessageBox.Show("You must enter at least one file path.");
}//end if
}//end import code
What I'm wondering is can I replace the //import code part with something like:
if(tb.Name = "txtAvF") then...
or similar, or do I have to do it outside of the foreach loop? Thanks in advance. Let me know if I need to clarify anything.
If you want to check to see if the TextBox is one of the ones on the form (which I think you are), then you are == which (taken from MSDN)
the operator == tests for reference equality by determining if two references indicate the same object
So this is what you're looking for:
if(box == textBox1 && !string.IsNullOrEmpty(box.Text))
{
// Import Textbox1
}
else if(box == textBox2 && !string.IsNullOrEmpty(box.Text))
{
// Import Textbox2
}
else if (box == textBox3....)
You should do it inside of the loop. Like this:
if (box.Name == "txtAvF")
box.Text = "What you want";
But setting hasPath inside the loop just holds state for your last path. You should also move MessageBox code inside loop.
The hasPath assignment seems correct to me; it's set for any one text box, and if not set at the end of the loop, a message is displayed. This rhymes well with the text so displayed. Moving the MessageBox call into the loop would cause the dialog box to never be displayed (or errenously displayed), at least as the code is implemented now, since the OfType<>().Where() guarantees that all text boxes iterated over have at least some content.
(I would add this as a comment to #Xaqron but don't have the necessary reputation yet.)

Categories

Resources