有关带笑脸控制的“每个循环"问题的问题 [英] Question regarding For Each Loop issue with Smiley Control

查看:76
本文介绍了有关带笑脸控制的“每个循环"问题的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我已经在此页面此处以及其上尝试过RTB控件真的很棒...尽管我希望它是自动的,而不仅仅是在单击按钮后插入一个笑脸.我将For Each循环放在Trichtextbox1上的textchanged事件上

基本上,我有表情符号代码数组(emotCodes =即":)",":("等)和位置数组(emotLoc =即"emots \ 1.gif","emots \ 2.gif"等)

我还有一个

Hi Guys,


I have tried the RTB control from this page here and its really great... Although I want it to be automatic and not just insert a smiley after clicking a button. Im putting a For each loop on a textchanged event on Trichtextbox1

Basically, I have array of the emoticon Codes (emotCodes= i.e. ":)" , ":(" , etc ) and an array of the locations(emotLoc = i.e "emots\1.gif" , "emots\2.gif" , etc )

I have also a sub

Public Sub chooseImage(ByVal imageLoc As String)
        Dim theImage As Image = Image.FromFile(imageLoc)
        If Not Trestan.Utility.isImageCorrupted(theImage) Then

            Dim thePic As New PictureBox()
            thePic.Image = theImage
            thePic.Size = theImage.Size
            Me.TRichTextBox1.AddControl(thePic)
            TRichTextBox1.AutoScroll()
        End If

    End Sub




这是我到目前为止所拥有的






Here is what I have so far



 Private Sub tRichTextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles TRichTextBox1.TextChanged

    For Each st As String In emotCodes


            Dim _index As Integer
            Dim emotcode As String = ""
            _index = TRichTextBox1.Find(st, RichTextBoxFinds.WholeWord)


            If _index > -1 Then
                If st = emotCodes(0) Then
                    
                    TRichTextBox1.Select(_index, emotCodes(0).Length)
                    chooseImage(imageLoc(0))
                    
                ElseIf st = emotCodes(1) Then
                    TRichTextBox1.SelectedText = st
                    chooseImage(imageLoc(1))
                ElseIf st = emotCodes(2) Then
                    TRichTextBox1.SelectedText = st
                    chooseImage(imageLoc(2))
                End If


            End If
            

        Next

End Sub





发生的是,它只在代码下方添加了笑脸,并多次添加了它们:(


任何帮助将不胜感激.在高级感谢:D


musiccrave





What happens is that, it only adds the smiley below the codes and adding them multiple times :(


Any help would be greatly appreciated. Thanks in advanced :D


musiccrave

推荐答案

我认为您需要始终选择要用图片替换的文本并将其删除.
I think you need to always select the text you are going to replace with a picture and remove it.
If _index > -1 Then
  TRichTextBox1.Select(_index, emotCodes(0).Length)
  TRichTextBox1.SelectedText = ""
  If st = emotCodes(0) Then
     chooseImage(imageLoc(0))
  ElseIf st = emotCodes(1) Then  
     chooseImage(imageLoc(1))
  ElseIf st = emotCodes(2) Then    
    chooseImage(imageLoc(2))
  End If
End If



顺便说一句,这种方法效率极低.您可能想用一个简单的循环替换它,在该循环中尝试将st与索引匹配,并用与该索引对应的图像替换它.无需无尽的if语句.此外,您可能只想通过仅查看当前插入符号位置,然后仅查看几个前后位置(取决于图释的最大长度)来进一步优化它.当文本变大时,您会遇到麻烦.

祝你好运!



By the way, this method is extremely inefficient. You might want to replace it with a simple loop where you try to match st with the index and replace it with the image corresponding to that index. No need for the endless if statements. Also, you might just want to optimize it any further by only looking at the current caret position and then only a few positions back and forward (depending on the maximum length of the emoticon). You would get in real trouble when the text becomes larger.

Good luck!


那我应该这样做吗?

so should i do this?

If _index > -1 Then
  
  TRichTextBox1.SelectedText = ""
  If st = emotCodes(0) Then
      TRichTextBox1.Select(_index, emotCodes(0).Length)
     chooseImage(imageLoc(0))
  ElseIf st = emotCodes(1) Then
     TRichTextBox1.Select(_index, emotCodes(1).Length)
     chooseImage(imageLoc(1))
  ElseIf st = emotCodes(2) Then
    TRichTextBox1.Select(_index, emotCodes(2).Length)
    chooseImage(imageLoc(2))
  End If
End If





关于此建议:

此外,您可能只想通过仅查看当前插入符号的位置,然后再来回移动仅几个位置(取决于图释的最大长度)来进一步优化它.当文本变为更大."


trichtextbox1处于只读模式是否安全?而且要扔给它的文本是平面文本,因此不进行任何格式设置,这还会在间距或其他方面带来一些麻烦吗?


我对此有点困惑...:

将st与索引匹配并将其替换为与该索引对应的图像"


我知道这是合乎逻辑的事情,但是我应该使用大小写转换吗? vb可以切换大小写吗?谢谢


***编辑

顺便说一句

此代码(TRichTextBox1.SelectedText =")仅擦除emotCode,而不将控件放在代码的位置.我该怎么办?谢谢高级





regarding this recommendation:

"Also, you might just want to optimize it any further by only looking at the current caret position and then only a few positions back and forward (depending on the maximum length of the emoticon). You would get in real trouble when the text becomes larger."


would it be safe if the trichtextbox1 is on readonly mode? And also the texts being thrown to it are plane texts so no formatting whatsoever, would that still get some trouble with the spacing or something?


Im a little bit confused with this one...:

"to match st with the index and replace it with the image corresponding to that index"


I know its a logical thing but should i use a case switch? is case switch possible with vb? thanks


*** edit

by the way,

this code (TRichTextBox1.SelectedText = "") only erases the emotCode but not place the control on the code''s place. how do i go about this? thanks in advanced


这篇关于有关带笑脸控制的“每个循环"问题的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆