WPF中的富文本框。查找或选择已在框中的文字?

时间:2017-01-03 00:59:48

标签: c#

编辑:在帖子末尾包含我的整个程序代码

我正在进行交易或没有交易游戏,除了在猜测时消除侧面的数字之外我一切正常。希望有人可以指导我......

Here is what my GUI looks like

GUI after first round. Box 1 is "my" box, bolded boxes are ones I've already opened

我有一个带有2个富文本框的WPF。我有一组数字,我把一半放在一个文本框中,一半放在另一个文本框中。我将值显示为货币以适应游戏主题。

这是我的数组代码并显示在文本框中:

//initialize array with dollar values
        displayNumbers = new int[] {0, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750,
            1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000};

        //initialize usedNumbers array with a length
        usedNumbers = new int[displayNumbers.Length];

        //display amounts in 1st textblock. boxSize = 26 (size of array)
        for(int i = 0; i<(boxSize/2); i++)
        {
            textBox1.AppendText(displayNumbers[i].ToString("C0") + " \n");
        }

        //display amounts in 2nd textblock
        for(int i = boxSize/2; i < boxSize; i++)
        {
            textBox2.AppendText(displayNumbers[i].ToString("C0") + "\n");
        }

我的游戏目前的工作方式是,数组随机播放,无论您按下哪个按钮,该值都将是数组中的下一个随机值。在我洗牌之前,我会在两侧显示值。

如何获取数组中索引的当前值,并在我的富文本框中找到该数字并更改颜色或背景等,以便用户知道该值是不在线的?

我已经使用按钮的内容来显示与该按钮相关联的值,但我也应该从侧边框中删除该值。

根据我在网上找到的内容,我认为指针可能是我的答案?但我还不知道如何使用这些......

我还是初学者,所以如果能以初学者的方式回答这个问题,我真的很感激。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DealorNoDeal
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        int[] displayNumbers;
        int[] usedNumbers;
        Random random = new Random();
        int boxSize = 26;
        Button button;
        int turns = 0;
        int boxesToChoose;
        int boxesLeft = 26;
        int bankOffer;
        bool isOffer = false;
        bool deal = false;
        private Button firstChoice;
        int round = 0;

        public MainWindow()
        {
            InitializeComponent();

            //initialize array with dollar values
            displayNumbers = new int[] {0, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500, 750,
                1000, 5000, 10000, 25000, 50000, 75000, 100000, 200000, 300000, 400000, 500000, 750000, 1000000};

            //initialize usedNumbers array with a length
            usedNumbers = new int[displayNumbers.Length];

            //display amounts in 1st textblock
            for(int i = 0; i<(boxSize/2); i++)
            {
                textBox1.AppendText(displayNumbers[i].ToString("C0") + " \n");
            }

            //display amounts in 2nd textblock
            for(int i = boxSize/2; i < boxSize; i++)
            {
                textBox2.AppendText(displayNumbers[i].ToString("C0") + "\n");
            }

            //deal/nodeal buttons disabled on startup only enabled when deal is offered
            buttonDeal.IsEnabled = false;
            buttonNoDeal.IsEnabled = false;

            //randomize dollar values for boxes now that they have been displayed in order
            ShuffleDecks();
        }

        private void ShuffleDecks()
        {
            //fisher-yates shuffle algorithm
            int index;
            string temp;

            for (int i = 0; i < boxSize; i++)
            {
                index = random.Next(boxSize);
                temp = Convert.ToString(displayNumbers[i]);
                displayNumbers[i] = displayNumbers[index];
                displayNumbers[index] = Convert.ToInt32(temp);
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            button = sender as Button;
            if (deal == true)
            {
                if (bankOffer > displayNumbers[0])
                {
                    textBlockDisplay.Text = "You made a good deal! Your winnings: " + bankOffer.ToString("C0");
                }
                else
                {
                    textBlockDisplay.Text = "Better luck next time! Your winnings: " + bankOffer.ToString("C0");
                }

                firstChoice.Content = displayNumbers[0].ToString("C0");
                firstChoice.IsEnabled = false;
            }

            else
            {
                if (isOffer == false)
                {
                    //first turn is to pick a box, not opening a box
                    if (turns == 0)
                    {
                        button.IsEnabled = false;
                        firstChoice = button;
                        boxesToChoose = BoxesToOpen();
                        textBlockDisplay.Text = "Now choose " + boxesToChoose + " box(es)";
                    }

                    //opening boxes
                    else
                    {
                        button.FontWeight = FontWeights.Bold;
                        button.Content = displayNumbers[turns].ToString("C0");
                        usedNumbers[turns] = displayNumbers[turns];
                        boxesToChoose--;
                        textBlockDisplay.Text = "Now choose " + boxesToChoose + " box(es)";
                        //FindText(textBox1,Convert.ToString(button.Content),Brushes.Red);
                    }
                    boxesLeft--;
                    CheckTurns();
                    turns++;
                }
            }
        }

        //see what turn we are on and make an offer
        private void CheckTurns()
        {
            if(turns == 6 || turns == 11 || turns == 15 || turns == 18 || turns == 20 || turns == 21 || turns == 22 || turns == 23 || turns == 24 || turns == 25)
            {
                //enable deal/nodeal buttons
                buttonDeal.IsEnabled = true;
                buttonNoDeal.IsEnabled = true;

                isOffer = true; //disable the other boxes you haven't opened yet

                //calculate the offer
                bankOffer = CalculateOffer(boxesLeft);

                textBlockDisplay.Text = "The bank offers you: " + bankOffer.ToString("C0") +" Deal?";
                round++;
            }
        }

        private void buttonDeal_Click(object sender, RoutedEventArgs e)
        {
            if (turns < 25)

            {
                deal = true;
                GreyAllBoxes();
                firstChoice.IsEnabled = true;
                textBlockDisplay.Text = "Game Over! You walk away with " + bankOffer.ToString("C0") + " Click your first box to see if you made a good deal.";
            }
            else
            {
                textBlockDisplay.Text = "Game Over! You walk away with " + bankOffer.ToString("C0") + " Click your first box to see if you made a good deal.";
            }
        }

        private void buttonNoDeal_Click(object sender, RoutedEventArgs e)
        {
            if (turns < 25)
            {
                isOffer = false;
                boxesToChoose = BoxesToOpen();
                textBlockDisplay.Text = "Now choose " + boxesToChoose + " box(es)";
            }
            else
            {
                //stick with first box or take last box
                textBlockDisplay.Text = "Do you choose your 1st box, or " + bankOffer.ToString("C0") + "?";

                buttonDeal.Content = bankOffer.ToString("C0");
                buttonNoDeal.IsEnabled = false;

                firstChoice.IsEnabled = true;
            }
        }

        private int CalculateOffer(int boxes)
        {
            int offer;
            int mainSum = displayNumbers.Sum();
            int usedSum = usedNumbers.Sum();

            //offer = sum of boxes left, divided by number of boxes left
            offer = (mainSum - usedSum)/boxes;

            return offer;
        }

        private void GreyAllBoxes()
        {
            buttonDeal.IsEnabled = false;
            buttonNoDeal.IsEnabled = false;
            button1.IsEnabled = false;
            button2.IsEnabled = false;
            button3.IsEnabled = false;
            button4.IsEnabled = false;
            button5.IsEnabled = false;
            button6.IsEnabled = false;
            button7.IsEnabled = false;
            button8.IsEnabled = false;
            button9.IsEnabled = false;
            button10.IsEnabled = false;
            button11.IsEnabled = false;
            button12.IsEnabled = false;
            button13.IsEnabled = false;
            button14.IsEnabled = false;
            button15.IsEnabled = false;
            button16.IsEnabled = false;
            button17.IsEnabled = false;
            button18.IsEnabled = false;
            button19.IsEnabled = false;
            button20.IsEnabled = false;
            button21.IsEnabled = false;
            button22.IsEnabled = false;
            button23.IsEnabled = false;
            button24.IsEnabled = false;
            button25.IsEnabled = false;
            button26.IsEnabled = false;
        }

        private void UnGreyAllBoxes()
        {
            button1.IsEnabled = true;
            button2.IsEnabled = true;
            button3.IsEnabled = true;
            button4.IsEnabled = true;
            button5.IsEnabled = true;
            button6.IsEnabled = true;
            button7.IsEnabled = true;
            button8.IsEnabled = true;
            button9.IsEnabled = true;
            button10.IsEnabled = true;
            button11.IsEnabled = true;
            button12.IsEnabled = true;
            button13.IsEnabled = true;
            button14.IsEnabled = true;
            button15.IsEnabled = true;
            button16.IsEnabled = true;
            button17.IsEnabled = true;
            button18.IsEnabled = true;
            button19.IsEnabled = true;
            button20.IsEnabled = true;
            button21.IsEnabled = true;
            button22.IsEnabled = true;
            button23.IsEnabled = true;
            button24.IsEnabled = true;
            button25.IsEnabled = true;
            button26.IsEnabled = true;
        }

        private int BoxesToOpen()
        {
            if (round == 0)
            {
                boxesToChoose = 6;
            }
            else if(round == 1)
            {
                boxesToChoose = 5;
            }
            else if (round == 2)
            {
                boxesToChoose = 4;
            }
            else if (round == 3)
            {
                boxesToChoose = 3;
            }
            else if(round == 4)
            {
                boxesToChoose = 2;
            }
            else
            {
                boxesToChoose = 1;
            }

            return boxesToChoose;
        }
    }

1 个答案:

答案 0 :(得分:0)

回答您的问题需要多个代码块。由于你没有展示很多部分,我只是引导你完成任务的建议和参考。

  1. 不需要跟踪索引,因为该按钮具有您需要在richtextbox和process中搜索的数量,即文本。

  2. 从文本框中删除所选金额

    一个。点击按钮,首先在富文本框中搜索金额XWPFParagraph paragraph = document.createParagraph(); XWPFRun run=paragraph.createRun(); run.setText("A"); run.addBreak(); run.setText("B"); run.addBreak(); run.setText("C"); document.write(OutPutFilePath); 。请参阅<firebase-query>.disabledlink1处的解决方案。一个接一个地搜索文本框。如果在第1发现第二个文本框中没有需要seacrh。

    湾选择搜索量并装饰它。

    ℃。我的建议是用StrikeThrough来装饰它,而不是从文本框中删除它。您可能也会突出显示红色。删除所选文本很简单,只需使用"$" + searchAmount + " \n"。可以使用textBox1.SelectedText = "";进行红色突出显示。对于删除线,您必须使用textBox1.SelectionColor = Color.Red;TextRange。请参阅link2