If statement for gauge j2me - java-me

Below is part of some code which displays a gauge which shows the progress from 0 - 100 of a word counter, however i am unsure how to code the if statement to display some text to show a successful count. I have had a go at the if statement " if gg_Progress.value = 100 " which i know is wrong, does anyone know what it should say. Thanks
public void SetGauge(int value) {
this.gg_Progress.setValue(value);
}
public void GaugeCheck () {
if gg_Progress.value = 100 {
string_Progress.setText("The number of words have been successfully counted");
}
}

if (this.gg_Progress.getValue() == 100) {

Related

Getting object name and randomly placing it to Text UI - Unity

I am a beginner in Unity and I am currently making a simple game. I have a problem managing the flow of my minigame. The minigame is simply finding an object, when I found and tap on the item name below will be shaded or marked or there can be animation just to indicate that the item is found.
What I want to do is to get the name of the objects that need to be found and set them randomly in the three (3) item names below. like every time this minigame opens the names of the items are randomly placed in the 3 texts. And when the item is found the name below will be marked or shaded or anything that will indicate it is found, but for now I will just set it inactive for easier indication. How can I properly do this whole process?
The objects inside the scene are button for them to have onCLick() events
Correction: the term choices are wrong because they just display the name of the items that you need to find, just in case you get confused with the term choice in my minigame. I will fix it.
Here is the visuals for the minigame:
The script I currently have for when the objects was clicked:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using TMPro;
public class ClickObject : MonoBehaviour
{
[SerializeField] Button pillowBtn, pcBtn, lampBtn;
// [SerializeField] TextMeshProUGUI choice1, choice2, choice3;
// FROM THE SOLUTION
[SerializeField] List<GameObject> gameObjectWanted;
[SerializeField] List<TextMeshProUGUI> textBoxes;
// METHOD NAMES IS TEMPORARY
public void pillowClicked()
{
Debug.Log("you found the " + EventSystem.current.currentSelectedGameObject.name);
}
public void desktopClicked()
{
Debug.Log("you found the " + EventSystem.current.currentSelectedGameObject.name);
}
public void lampClicked()
{
Debug.Log("you found the " + EventSystem.current.currentSelectedGameObject.name);
}
}
You asked for a lot and I hope I understood your intention.
first, if you want to randomly choose an amount of game objects from your game, I think the best way to do it is by adding the refernece of all the game objects you want to choose from radomly inside a list of game objects and then randomly take a game object of the list and make it a child of another game object I call "fatherGoTranform" on my code like that:
[SerializeField] List<GameObject> gameObjectWanted;
[SerializeField] float numOfGO = 4;
[SerializeField] Transform fatherGoTranform;
void Start()
{
for(int i=0;i<numOfGO;i++)
{
int index = Random.Range(0, gameObjectWanted.Count-1);
GameObject currentGO = gameObjectWanted[index ];
currentGO.transform.parent = fatherGoTranform;
gameObjectWanted.RemoveAt(index);
}
}
and then to click on a game object and the do with what you want try this:
void Update()
{
//Check for mouse click
if (Input.GetMouseButtonDown(0))
{
RaycastHit raycastHit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out raycastHit, 100f))
{
if (raycastHit.transform != null)
{
//Our custom method.
CurrentClickedGameObject(raycastHit.transform.gameObject);
}
}
}
}
I have not checked the code so if there is an error tell me and I will fix it
This is the solution that worked for my problem. I randomly placed numbers to the list according to childCount and every index indicate the index of the text that I want to put them on which I get as a Transform child.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using TMPro;
public class ClickObject : MonoBehaviour
{
// [SerializeField] Button pillowBtn, pcBtn, lampBtn;
[SerializeField] GameObject choices, v1, finishPanel;
// RANDOM NUMBER HOLDER FOR CHECKING
private int randomNumber, foundCount;
// RANDOMIZED NUMBER LIST HOLDER
public List<int> RndmList = new List<int>();
private void Awake()
{
foundCount = 0;
RndmList = new List<int>(new int[v1.transform.childCount]);
for (int i = 0; i < v1.transform.childCount; i++)
{
randomNumber = UnityEngine.Random.Range(0, (v1.transform.childCount) + 1);
while (RndmList.Contains(randomNumber))
{
randomNumber = UnityEngine.Random.Range(0, (v1.transform.childCount) + 1);
}
RndmList[i] = randomNumber;
// Debug.Log(v1.transform.GetChild(randomNumber-1).name);
choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().text = v1.transform.GetChild(randomNumber - 1).name;
}
}
public void objectFound()
{
string objectName = EventSystem.current.currentSelectedGameObject.name;
Debug.Log("you found the " + objectName);
for (int i = 0; i < choices.transform.childCount; i++)
{
if (objectName == choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().text)
{
// Debug.Log(i);
// choices.transform.GetChild(i).gameObject.SetActive(false);
// choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().color = Color.gray;
if(RndmList.Contains(i+1))
{
// Debug.Log(i);
// Debug.Log(v1.transform.GetChild(RndmList[i]-1).name);
choices.transform.GetChild(i).GetComponentInChildren<TextMeshProUGUI>().color = Color.gray;
v1.transform.GetChild(RndmList[i]-1).GetComponent<Button>().enabled = false;
foundCount++;
}
}
}
if(foundCount == v1.transform.childCount)
{
finishPanel.SetActive(true);
}
}
}

Find word in textbox and get line number with this word C#

I have a problem, I have a text in texbox1 and I would like to search for a word there, and if I find it, I would like to get the line number from texbox1 where the found word is... is there such a possibility because I have already dug the whole internet and I can not find a solution :(
I try this:
private void button3_Click(object sender, EventArgs e)
{
int iL = 0;
foreach (string line in textBox1.Lines)
{
iL += 1;
if (line == "someword")
{
textBox2.Text = textBox1.Lines[iL].Remove(0, 15);
}
}
}
please help me because I am losing my mind :/
I just do it by my self ;) so thx for help...
And for others who want anwser..
string line = textBox1.Lines.FirstOrDefault(l => l.Contains("test"));
if (line != null)
{
textBox2.AppendText(line+Environment.NewLine); //all line with string test
textBox2.AppendText(line.Remove(0, 47) + Environment.NewLine);//only string test without 47 char from begining
}

Get Visible Text from RichTextBlock

In a UWP app, I am using a RichTextBlock that gets populated with some content. It has word wrapping enabled and has a max lines set so that regardless of the length of its content, it will only show a certain number of lines of rich text.
I'd like to know if there is a way to figure out what is the visible text?
So if I have:
<RichTextBlock TextWrapping="Wrap" MaxLines="2">
<RichTextBlock.Blocks>
<Paragraph>
<Paragraph.Inlines>
A bunch of runs go in here with text that are several lines
</Paragraph.Inlines>
</Paragraph>
</RichTextBlock.Blocks>
</RichTextBlock>
I'd like to know how much of the text is actually visible.
I'm trying to detect cases where the text is longer than a set number of lines and append a "... Read More" at the end of the last line (replacing the last 13 chars with "... Read More")
So I wrote some code to get the behavior that I want, but unfortunately this is rather slow and inefficient. So if you're using it in an app that is primarily to show a lot of text that needs to be truncated (like a ListView with a lot of text items) then this would slow down your app perf. I still would like to know if there is a better way to do this.
Here's my code (which only handles Run and Hyperlink inlines so you'll have to modify to handle other types that you need):
private static void TrimText_Slow(RichTextBlock rtb)
{
var paragraph = rtb?.Blocks?.FirstOrDefault() as Paragraph;
if (paragraph == null) { return; }
// Ensure RichTextBlock has passed a measure step so that its HasOverflowContent is updated.
rtb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
if (rtb.HasOverflowContent == false) { return; }
// Start from end and remove all inlines that are not visible
Inline lastInline = null;
var idx = paragraph.Inlines.Count - 1;
while (idx >= 0 && rtb.HasOverflowContent)
{
lastInline = paragraph.Inlines[idx];
paragraph.Inlines.Remove(lastInline);
idx--;
// Ensure RichTextBlock has passed a measure step now with an inline removed, so that its HasOverflowContent is updated.
rtb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
}
// The last inline could be partially visible. The easiest thing to do here is to always
// add back the last inline and then remove characters from it until everything is in view.
if (lastInline != null)
{
paragraph.Inlines.Add(lastInline);
}
// Make room to insert "... Read More"
DeleteCharactersFromEnd(paragraph.Inlines, 13);
// Insert "... Continue Reading"
paragraph.Inlines.Add(new Run { Text = "... " });
paragraph.Inlines.Add(new Run { Text = "Read More", Foreground = new SolidColorBrush(Colors.Blue) });
// Ensure RichTextBlock has passed a measure step now with the new inlines added, so that its HasOverflowContent is updated.
rtb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
// Keep deleting chars until "... Continue Reading" comes into view
idx = paragraph.Inlines.Count - 3; // skip the last 2 inlines since they are "..." and "Read More"
while (idx >= 0 && rtb.HasOverflowContent)
{
Run run;
if (paragraph.Inlines[idx] is Hyperlink)
{
run = ((Hyperlink)paragraph.Inlines[idx]).Inlines.FirstOrDefault() as Run;
}
else
{
run = paragraph.Inlines[idx] as Run;
}
if (string.IsNullOrEmpty(run?.Text))
{
paragraph.Inlines.Remove(run);
idx--;
}
else
{
run.Text = run.Text.Substring(0, run.Text.Length - 1);
}
// Ensure RichTextBlock has passed a measure step now with the new inline content updated, so that its HasOverflowContent is updated.
rtb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
}
}
private static void DeleteCharactersFromEnd(InlineCollection inlines, int numCharsToDelete)
{
if (inlines == null || inlines.Count < 1 || numCharsToDelete < 1) { return; }
var idx = inlines.Count - 1;
while (numCharsToDelete > 0)
{
Run run;
if (inlines[idx] is Hyperlink)
{
run = ((Hyperlink)inlines[idx]).Inlines.FirstOrDefault() as Run;
}
else
{
run = inlines[idx] as Run;
}
if (run == null)
{
inlines.Remove(inlines[idx]);
idx--;
}
else
{
var textLength = run.Text.Length;
if (textLength <= numCharsToDelete)
{
numCharsToDelete -= textLength;
inlines.Remove(inlines[idx]);
idx--;
}
else
{
run.Text = run.Text.Substring(0, textLength - numCharsToDelete);
numCharsToDelete = 0;
}
}
}
}

System.FormatException was unhandled

In form load i'm updating Drop Down List Value
ddlFont:
foreach (FontFamily font in System.Drawing.FontFamily.Families)
{
ddlFont.Items.Add(font.Name);
}
ddlFontSize:
for (int i = 8; i < 24; i++)
{
ddlFontSize.Items.Add(i.ToString().Trim());
}
ddlFontStyle:
ddlFontStyle.Items.Add(System.Drawing.FontStyle.Bold.ToString());
ddlFontStyle.Items.Add(System.Drawing.FontStyle.Italic.ToString());
ddlFontStyle.Items.Add(System.Drawing.FontStyle.Regular.ToString());
ddlFontColor:
ddlColor.Items.Add(System.Drawing.Color.Black.Name.ToString());
ddlColor.Items.Add(System.Drawing.Color.Blue.Name.ToString());
ddlColor.Items.Add(System.Drawing.Color.Green.Name.ToString());
ddlColor.Items.Add(System.Drawing.Color.Red.Name.ToString());
ddlColor.Items.Add(System.Drawing.Color.White.Name.ToString());
ddlColor.Items.Add(System.Drawing.Color.Yellow.Name.ToString());
If user change the Font, size, style, color then i must change RichTextBox control text font, size, style, color.
I call the "FontFormation" method from SelectedIndexChanged event of Drop Down List controls which is having Font Name ddl, Font Style ddl, Font Size ddl.
private void ddlFont_SelectedIndexChanged(object sender, EventArgs e)
{
FontFormation();
}
In my code first two conditions are executing without error but last one alone showing "Input string was not in a correct format" error at ddlFont.Text.
public void FontFormation()
{
if (FontStyle.Bold.ToString() == ddlFontStyle.Text)
{
rchtxtMainBody.Font = new System.Drawing.Font(ddlFont.Text, Convert.ToUInt32(ddlFontSize.Text), FontStyle.Bold);
}
else if(FontStyle.Italic.ToString() == ddlFontStyle.Text)
{
rchtxtMainBody.Font = new System.Drawing.Font(ddlFont.Text, Convert.ToUInt32(ddlFontSize.Text), FontStyle.Italic);
}
else if (FontStyle.Regular.ToString() == ddlFontStyle.Text)
{
rchtxtMainBody.Font = new System.Drawing.Font(ddlFont.Text, Convert.ToUInt32(ddlFontSize.Text), FontStyle.Regular);
}
}
If i remove my last else if condition i.e FontStyle.Regular.ToString() == ddlFontStyle.Text then code is getting executed without any error.
The problem lies in your ddlFontSize.Text. The exception occurs when it cannot convert in to UInt32. Please debug your code and make sure that ddlFontSize.Text doesn't have "px" or "pt" with and and its only a number in string format.

ProgressBar Word Counter problem

Below is the code I am using to display a gauge progressbar from 0 - 100 whilst counting the number of words from a website. When it has finished counting the number of words i.e. when it has reached 100 I want it to display some text. However using the code below all I get is the else statement printed because it is still counting while the gauge check method is called. How do I get around this?
public void SetGauge(int value) {
this.gg_Progress.setValue(value);
}
public void GaugeCheck () {
if (this.gg_Progress.getValue() == 100) {
progress_Result.setText("The number of words from this website have been successfully counted");
}
else {
progress_Result.setText("The number of words has not been successfully counted");
}
}
if (command == Continue2) {
// write pre-action user code here
switchDisplayable(null, getReading());
// write post-action user code here
DelimiterCheck(tfd_Delimiter.getString());
this.Count();
this.GaugeCheck();
}
I suggest that you call progress_Result.setText("The number of words has not been successfully counted"); only once when you first initialize your gauge. Then remove the else clause from GaugeCheck.

Resources