WPF list modification during iteration? - wpf

I'm trying to make a very simple game where the yellow ball bouncing back and fourth. If it collides with one of the moving blue squares, the square is supposed to disappear and a new one should appear (always 3 in the window) elsewhere. When my code reaches this part, all 3 squares disappear (then reappears as intended it is not a problem) and I just cant figure out why. It would be a huge help if somebody could run over my methods responsible for the problem. Thank you in advance.
So my timer_Tick method, responsible for every frame:
void timer_Tick(object sender, EventArgs e)
{
logic.MoveBall();
if (model.Enemy.Count<3)
{
logic.AddEnemy();
}
int iii = 0;
foreach (MyShape enemy in model.Enemy) //the whole thing from here is me trying to solve list modification during iteration
{
if (logic.MoveEnemy(enemy) == -1)
{
logic.MoveEnemy(enemy);
}
else iii = logic.MoveEnemy(enemy);
}
if (iii > -1)
{
for (int j = model.Enemy.Count - 1; j >= 0; j--)
{
if (j == model.Enemy.Count - iii)
{
model.Enemy.RemoveAt(j);
}
}
}
}
MoveEnemy: I try to decide whether there is collusion and if yes, then try to remove the given shape object (blue square). Because This whole method is in a foreach, I just save the removable element and forward it to timer_Tick
public int MoveEnemy(MyShape shape)
{
int i = 0;
int ii = -1;
if ((shape.Area.IntersectsWith(model.Ball.Area)))
{
i = 0;
foreach (var e in model.Enemy)
{
i++;
if (shape == e)
{
ii = i;
}
}
}
shape.ChangeX(shape.Dx);
shape.ChangeY(shape.Dy);
bool coll = false;
foreach (var e in model.Enemy)
{
if ((e.Area.IntersectsWith(shape.Area)) && (shape != e))
{
coll = true;
}
}
if (shape.Area.Left < 0 || shape.Area.Right > Config.Width-40 || coll)
{
shape.Dx = -shape.Dx;
}
if (shape.Area.Top < 0)
{
shape.Dy = -shape.Dy;
}
if (shape.Area.Bottom > Config.Height/2)
{
shape.Dy = -shape.Dy;
}
RefreshScreen?.Invoke(this, EventArgs.Empty);
return ii;
}
And finally AddEnemy:
public void AddEnemy()
{
rnd = new Random();
int r = rnd.Next(-300, 300);
model.Enemy.Add(new MyShape(Config.Width / 2+r, 0, 40, 40));
RefreshScreen?.Invoke(this, EventArgs.Empty);
}

List<T> (or IList and Enumerable) exposes some useful methods to compact code:
int itemIndex = list.IndexOf(item); // Gets the index of the item if found, otherwise returns -1
list.Remove(item); // Remove item if contained in collection
list.RemoveAll(item => item > 5); // Removes all items that satisfy a condition (replaces explicit iteration)
bool hasAnyMatch = list.Any(item => item > 5); // Returns true as soon as the first item satisfies the condition (replaces explicit iteration)
A simplified version, which should eliminate the flaw:
void timer_Tick(object sender, EventArgs e)
{
if (model.Enemy.Count < 3)
{
logic.AddEnemy();
}
logic.MoveBall();
model.Enemy.ForeEach(logic.MoveEnemy);
model.Enemy.RemoveAll(logic.IsCollidingWithBall);
}
public void AddEnemy()
{
rnd = new Random();
int r = rnd.Next(-300, 300);
model.Enemy.Add(new MyShape(Config.Width / 2 + r, 0, 40, 40));
RefreshScreen?.Invoke(this, EventArgs.Empty);
}
public bool IsCollidingWithBall(MyShape shape)
{
return shape.Area.IntersectsWith(model.Ball.Area);
}
public int MoveEnemy(MyShape shape)
{
shape.ChangeX(shape.Dx);
shape.ChangeY(shape.Dy);
bool hasCollision = model.Enemy.Any(enemy => enemy.Area.IntersectsWith(shape.Area)
&& enemy != shape);
if (hasCollision || shape.Area.Left < 0 || shape.Area.Right > Config.Width - 40)
{
shape.Dx = -shape.Dx;
}
if (shape.Area.Top < 0)
{
shape.Dy = -shape.Dy;
}
if (shape.Area.Bottom > Config.Height / 2)
{
shape.Dy = -shape.Dy;
}
RefreshScreen?.Invoke(this, EventArgs.Empty);
}

Related

Combining two instantiate scripts (Unity 3D)

I need to combine the following scripts into one script containing two functions, one that instantiates the next prefab in the array and the other that instantiates the previous prefab in the array (it is a virtual tour app). Both should also destroy the current prefab. I could then call the functions via event triggers.
I have this code for the "next Prefab" script.
public GameObject[] Spheres;
int currentIndex = 0;
GameObject currentObject;
public Camera MainCamera;
void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
if (gameObject.tag == "ArrowNEXT")
{
Vector3 rayOrigin = MainCamera.ViewportToWorldPoint(new Vector3(0.1f, 0.1f, 0));
RaycastHit hit;
if (Physics.Raycast(rayOrigin, MainCamera.transform.forward, out hit))
{
if (hit.collider != null)
{
{
Destroy(currentObject);
currentIndex++;
if (currentIndex > Spheres.Length - 1) currentIndex = 0;
currentObject = Instantiate(Spheres[currentIndex]);
}
}
}
}
}
I need to combine it with the following:
using UnityEngine;
public class RayCastPrevFIX: MonoBehaviour
{
public GameObject[] Spheres;
int currentIndex = 0;
GameObject currentObject;
public Camera MainCamera;
void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
if (gameObject.tag == "ArrowPREV")
{
Vector3 rayOrigin = MainCamera.ViewportToWorldPoint(new Vector3(0.1f, 0.1f, 0));
RaycastHit hit;
if (Physics.Raycast(rayOrigin, MainCamera.transform.forward, out hit))
{
if (hit.collider != null)
{
{
Destroy(currentObject);
currentIndex--;
if (currentIndex < 0) currentIndex = Spheres.Length - 1;
currentObject = Instantiate(Spheres[currentIndex]);
}
}
}
}
}
}
How would I go about this? Any help is greatly appreciated.I have this so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SphereSwap : MonoBehaviour
{
public GameObject[] Spheres;
int currentIndex = 0;
GameObject currentObject;
public Camera MainCamera;
void Next()
{
Destroy(currentObject);
currentIndex++;
if (currentIndex > Spheres.Length - 1) currentIndex = 0;
currentObject = Instantiate(Spheres[currentIndex]);
}
void Previous()
{
Destroy(currentObject);
currentIndex--;
if (currentIndex < 0) currentIndex = Spheres.Length - 1;
currentObject = Instantiate(Spheres[currentIndex]);
}
}
You're on the right path, but you could shorten the code as well as use one function for both cases to avoid boilerplate:
using UnityEngine;
public class SphereSwap : MonoBehaviour
{
public GameObject[] Spheres;
int currentIndex = 0;
GameObject currentObject;
public Camera MainCamera;
const string ArrowPrevTag = "ArrowPREV";
const string ArrowNextTag = "ArrowNEXT";
private void HandleClick(bool next)
{
if(Spheres == null || Spheres.Length == 0)
{
Debug.Log($"Spheres list is empty, nothing to swap.");
return;
}
Vector3 rayOrigin = MainCamera.ViewportToWorldPoint(new Vector3(0.1f, 0.1f, 0));
RaycastHit hit;
if (Physics.Raycast(rayOrigin, MainCamera.transform.forward, out hit))
{
if (hit.collider != null)
{
// destroy current sphere.
Destroy(currentObject);
// go next or previous.
currentIndex += next ? 1 : -1;
// circular clamp if overflow
if (currentIndex < 0)
currentIndex = Spheres.Length - 1;
else if (currentIndex >= Spheres.Length)
currentIndex = 0;
// finally do instantiate.
currentObject = Instantiate(Spheres[currentIndex]);
}
}
}
private void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
// 'CompareTag' is more efficient than gameobject.tag == "sometag"
if (gameObject.CompareTag(ArrowNextTag))
HandleClick(true);
else if (gameObject.CompareTag(ArrowPrevTag))
HandleClick(false);
}
}
}

How do I repeat an action in c# to fix the jumping mechanism

I'm creating a basic 2D platformer game however the jumping mechanism will only run once and land directly afterwards. How do I loop this?
I tried detection collisions (from tag: Terrain) and this does help a lot however it's still not working correctly.
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController2D : MonoBehaviour
{
private Rigidbody2D rb2D;
private Vector2 velocity;
Vector2 xvelocity = new Vector2(10, 0);
Vector2 yvelocity = new Vector2(0, 10);
public float jumptime;
bool grounded = true;
bool jump = false;
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetButton("Jump"))
{
jump = true;
}
}
private void FixedUpdate()
{
//Debug.Log(Input.GetAxisRaw("Vertical"));
if (left == true)
{
//Debug.Log("Left");
rb2D.MovePosition(rb2D.position + -xvelocity * Time.fixedDeltaTime);
}
if (right == true)
{
//Debug.Log("Right");
rb2D.MovePosition(rb2D.position + xvelocity * Time.fixedDeltaTime);
}
//if (jump == true && grounded == true)
//{
// jumptime -= Time.fixedDeltaTime;
// if (jumptime > 0)
// {
// rb2D.MovePosition(rb2D.position + yvelocity * Time.fixedDeltaTime);
// }
// if (jumptime <= 0)
// {
// jump = false;
// jumptime = 2;
// }
if (jump == true && grounded == true && jumptime > 0)
{
jumptime -= Time.fixedDeltaTime;
rb2D.MovePosition(rb2D.position + yvelocity * Time.fixedDeltaTime);
} else if (jumptime <= 0)
{
jump = false;
jumptime = 2f;
}
//if (Time.fixedDeltaTime >= 2)
//{
// jump = false;
// rb2D.MovePosition(rb2D.position + -yvelocity * Time.fixedDeltaTime);
//}
}
void OnCollisionExit2D(Collision2D other)
{
Debug.Log("No longer in contact with " + other.transform.name);
jump = true;
grounded = false;
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Terrain")
{
Debug.Log("Landed");
jump = false;
grounded = true;
}
}
}
The expected outcome is that the action 'jump' will loop for ~1/1.5 seconds with a good height (vector2 yvelocity) so it will jump higher and will come down afterwards (thanks to the gravity from the Rigidbody (2D))
As stated in the comments I think the main issue is coming from this line of code.
if (jump == true && grounded == true && jumptime > 0)
It is much likely that one of those bool is not what you expect it to be. Anyway I suggest you to convert the line like so:
if (jump && grounded && jumptime > 0)
You do not need == true for booleans.
Anyway, to solve your question in an easier way, I would suggest you to use AddForce instead of move (because you're using a rigibody2d anyway).
rb2D.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
A small note about horizontal velocity. If you're using a rigibody it would be better to move it with the same rigidbody and not with the transform:
rb2D.MovePosition(rb2D.position + Vector2.left * xspeed * Time.fixedDeltaTime);
Your code will become:
public class PlayerController2D : MonoBehaviour
{
private Rigidbody2D rb2D;
private Vector2 velocity;
public float jumpForce = 5;
bool grounded = true;
void Start() { rb2D = GetComponent<Rigidbody2D>(); }
void Update()
{
if (Input.GetButton("Jump") && grounded)
rb2D.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
//calculate horizontal speed here
xspeed = ...;
}
private void FixedUpdate()
{
rb2D.MovePosition(rb2D.position + Vector2.left * xspeed * Time.fixedDeltaTime);
}
void OnCollisionExit2D(Collision2D other)
{
Debug.Log("No longer in contact with " + other.transform.name);
grounded = false;
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Terrain")
{
Debug.Log("Landed");
grounded = true;
}
}
}

How do i remove the milliseconds from my timer

I am making this game for school and I am trying to add a timer to levels. I have the timer working but not the way I want it to. It refreshes and it has milliseconds. I have searched every website for an answer but I can only find with dates and times or how do display them. I need the exact opposite.
package GameState;
import Main.GamePanel;
import TileMap.*;
import Entity.*;
import Entity.Enemies.*;
import Audio.AudioPlayer;
//timer imports
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
public class Level1State extends GameState {
private TileMap tileMap;
private Background bg;
private static int cnt;
private Player player;
private ArrayList<Enemy> enemies;
private ArrayList<Explosion> explosions;
private HUD hud;
private AudioPlayer bgMusic;
public Level1State(GameStateManager gsm) {
this.gsm = gsm;
init();
}
public void init() {
tileMap = new TileMap(30);
tileMap.loadTiles("/Tilesets/grasstileset.gif");
tileMap.loadMap("/Maps/level1-1.map");
tileMap.setPosition(0, 0);
tileMap.setTween(1);
bg = new Background("/Backgrounds/grassbg1.2.jpg", 0.1);
player = new Player(tileMap);
player.setPosition(100, 100);
populateEnemies();
explosions = new ArrayList<Explosion>();
hud = new HUD(player);
bgMusic = new AudioPlayer("/Music/level1-1.mp3");
bgMusic.play();
}
private void populateEnemies() {
enemies = new ArrayList<Enemy>();
Slugger s;
Point[] points = new Point[] {
new Point(200, 100),
new Point(860, 200),
new Point(1525, 200),
new Point(1680, 200),
new Point(1800, 200)
};
for(int i = 0; i < points.length; i++) {
s = new Slugger(tileMap);
s.setPosition(points[i].x, points[i].y);
enemies.add(s);
}
}
public void update() {
// update player
player.update();
tileMap.setPosition(
GamePanel.WIDTH / 2 - player.getx(),
GamePanel.HEIGHT / 2 - player.gety()
);
// set background
bg.setPosition(tileMap.getx(), tileMap.gety());
// attack enemies
player.checkAttack(enemies);
// update all enemies
for(int i = 0; i < enemies.size(); i++) {
Enemy e = enemies.get(i);
e.update();
if(e.isDead()) {
enemies.remove(i);
i--;
explosions.add(
new Explosion(e.getx(), e.gety()));
}
}
// update explosions
for(int i = 0; i < explosions.size(); i++) {
explosions.get(i).update();
if(explosions.get(i).shouldRemove()) {
explosions.remove(i);
i--;
}
}
}
public void draw(final Graphics2D g) {
// draw bg
bg.draw(g);
// draw tilemap
tileMap.draw(g);
// draw player
player.draw(g);
// draw enemies
for(int i = 0; i < enemies.size(); i++) {
enemies.get(i).draw(g);
}
// draw explosions
for(int i = 0; i < explosions.size(); i++) {
explosions.get(i).setMapPosition(
(int)tileMap.getx(), (int)tileMap.gety());
explosions.get(i).draw(g);
}
// draw hud
hud.draw(g);
ActionListener actListner = new ActionListener() {
public void actionPerformed(ActionEvent event) {
cnt += 1;
g.drawString("Counter = "+cnt, 120, 120);
}
};
Timer timer = new Timer(1000, actListner);
timer.start();
}
public void keyPressed(int k) {
if(k == KeyEvent.VK_LEFT) player.setLeft(true);
if(k == KeyEvent.VK_RIGHT) player.setRight(true);
if(k == KeyEvent.VK_UP) player.setUp(true);
if(k == KeyEvent.VK_DOWN) player.setDown(true);
if(k == KeyEvent.VK_W) player.setJumping(true);
if(k == KeyEvent.VK_E) player.setGliding(true);
if(k == KeyEvent.VK_R) player.setScratching();
if(k == KeyEvent.VK_F) player.setFiring();
}
public void keyReleased(int k) {
if(k == KeyEvent.VK_LEFT) player.setLeft(false);
if(k == KeyEvent.VK_RIGHT) player.setRight(false);
if(k == KeyEvent.VK_UP) player.setUp(false);
if(k == KeyEvent.VK_DOWN) player.setDown(false);
if(k == KeyEvent.VK_W) player.setJumping(false);
if(k == KeyEvent.VK_E) player.setGliding(false);
}
}
this is the part i need help with:
ActionListener actListner = new ActionListener() {
public void actionPerformed(ActionEvent event) {
cnt += 1;
g.drawString("Counter = "+cnt, 120, 120);
}
};
Timer timer = new Timer(1000, actListner);
timer.start();
Can anyone help me? I just can't get it to stop refreshing and it won't remove the milliseconds either.
Thank you in advance
P.S.
I have a feeling the first second runs way slower than the others.
P.P.S.: i have looked at "how to implement timers" but none of those worked and I found this one on a website that doesn't return an error.

All elements change in Arraylist - Processing

My program is a zombie survival game, set in a 2d array of blocks.
I use an arraylist - my first attempt - to store the zombies, and each in tern "checks" if there is a block above, below and around it, to detect it's movement.
I'll post the relevant code here, and upload the sketch folder separately.
ArrayList zombies;
void setup() {
zombies = new ArrayList();
}
void draw() {
for (int i = 0; i < zombies.size(); i++) {
Zombie zombie = (Zombie) zombies.get(i);
zombie.draw();
}
}
void keyPressed() {
if (key == 'z') {
zombies.add(new Zombie());
}
}
class Zombie
{
int posX = 20;
int posY = 10;
boolean fall;
boolean playerOnBlock;
Zombie() {
posX = 10;
posY = 590;
fall = false;
}
void draw() {
grid.blockCheck(posX, posY, 2);
fill(0, 255, 0);
rect(posX, posY, 10, 10);
}
void fall() {
posY += 5;
println("zombiefall"+posY);
}
void move(boolean left, boolean right, boolean above) {
if (left == true && player.playerX < posX) {
posX -= 1;
}
if (right == true && player.playerX > posX) {
posX += 1;
}
}
}
class Grid {
void blockCheck(int x, int y, int e) {
for (int i = 0; i < l; i++)
for (int j = 0; j < h; j++)
{
grid[i][j].aroundMe (x, y, i, j, e);
}
}
}
class Block {
void aroundMe(int _x, int _y, int _i, int _j, int entity) {
int pGX = _x/10;
int pGY = _y/10;
if (entity == 1) {
if (pGY+1 == _j && pGX == _i && state == 4) {
player.fall();
}
if (pGX == _i && pGX-1 <= _i && _y == posY && state == 4) {
leftOfMe = true;
}
else
{
leftOfMe = false;
}
if (pGX+1 == _i && _y == posY && state == 4) {
rightOfMe = true;
}
else
{
rightOfMe = false;
}
if (pGY-1 == _j && _x == posX && state ==4) {
aboveOfMe = true;
}
else
{
aboveOfMe = false;
}
player.controls(leftOfMe, rightOfMe, aboveOfMe);
}
if (entity == 2) {
if (pGY+1 == _j && pGX == _i && state == 4) {
for (int i = 0; i < zombies.size(); i++) {
Zombie zombie = (Zombie) zombies.get(i);
zombie.fall();
}
}
if (pGX == _i && pGX-1 <= _i && _y == posY && state == 4) {
ZleftOfMe = true;
}
else
{
ZleftOfMe = false;
}
if (pGX+1 == _i && _y == posY && state == 4) {
ZrightOfMe = true;
}
else
{
ZrightOfMe = false;
}
if (pGY-1 == _j && _x == posX && state ==4) {
ZaboveOfMe = true;
}
else
{
ZaboveOfMe = false;
}
for (int i = 0; i < zombies.size(); i++) {
Zombie zombie = (Zombie) zombies.get(i);
zombie.move(ZleftOfMe, ZrightOfMe, ZaboveOfMe);
}
}
Sketch is here: http://www.mediafire.com/?u5v3117baym846v
I believe the problem lies in specifying which element of an arraylist I am referring to, as I can observe the issues to be:
All "zombies" fall when one detects that it should fall.
Zombie's speed increases with each additional zombie added - somehow treating all the zombie elements as one zombie object?
This might be a similar issue:
All elements of An ArrayList change when a new one is added?
But I've fiddled with my project and I can't seem to get it working still.
Please don't hesitate to ask for more information on my project. I will be with my computer all evening so should be able to reply quickly. Thanks in advance.
Thanks for your help.
I'm using it like this:
ArrayList <Zombie> zombies = new ArrayList <Zombie>();
-------------------------------------------
void setup(){
zombies = new ArrayList();
-------------------------------------------
void draw(){
for (Zombie z:zombies) {
z.draw();
}
}
-------------------------------------------
void keyPressed() {
if (key == 'z') {
for (int i = 0; i< 1; i++) {
zombies.add(new Zombie(i));
}
}
-------------------------------------------
class Zombie
{
int posX = 20;
int posY = 10;
boolean fall;
boolean playerOnBlock;
int z;
Zombie(int _z) {
posX = 10;
posY = 590;
fall = false;
z = _z;
}
void draw() {
grid.blockCheck(posX, posY, 2);
fill(0, 255, 0);
rect(posX, posY, 10, 10);
}
void fall() {
posY += 5;
println("zombiefall"+posY);
}
void move(boolean left, boolean right, boolean above) {
if (left == true && player.playerX < posX) {
posX -= 1;
}
if (right == true && player.playerX > posX) {
posX += 1;
}
}
}
Here is the idea :)
//use generics to keep it simple. Only Zoombies in this list
ArrayList <Zoombie> samp = new ArrayList <Zoombie>();
void setup() {
//a regular for loop to use the i as id
for (int i = 0; i< 100; i++) {
samp.add(new Zoombie(i));
}
//run just once
noLoop();
}
void draw() {
//a enhanced for, no need for use get()
// z will represent each element in the collection
for (Zoombie z:samp) {
z.d();
}
println("done");
}
// a dummy class ;)
class Zoombie {
int id;
Zoombie (int _id)
{
id =_id;
}
void d()
{
println(id);
}
}

wpf richtextbox check if caret is in the last line or count how many lines it has

I am trying to find out in a richtext box whether the caret is position in the last line. Is this possible?
NOTE: At the end I also added: or count how many lines it has, this is because in the miscrosoft forum there is an example for detecting in which line the caret is.
thanks
Please verify the msdn link
http://social.msdn.microsoft.com/Forums/en/wpf/thread/667b5d2a-84c3-4bc0-a6c0-33f9933db07f
If you really wanted to know the line number of the caret, you could do something like the following (probably needs some tweaking):
TextPointer caretLineStart = rtb.CaretPosition.GetLineStartPosition(0);
TextPointer p = rtb.Document.ContentStart.GetLineStartPosition(0);
int caretLineNumber = 1;
while (true)
{
if (caretLineStart.CompareTo(p) < 0)
{
break;
}
int result;
p = p.GetLineStartPosition(1, out result);
if (result == 0)
{
break;
}
caretLineNumber++;
}
The code to get the number of lines:
Int32 CountDisplayedLines(RichTextBox rtb)
{
Int32 result = -1;
rtb.CaretPosition = rtb.Document.ContentStart;
while (rtb.CaretPosition.GetLineStartPosition(++result) != null)
{
}
return result;
}
I have found a solution. Maybe there is a simpler way, if so please let me know
private void OnHasRtbReachedEnd(System.Windows.Controls.RichTextBox rtb)
{
TextPointer pointer1 = rtb.CaretPosition;
int iCurrentLine = GetLineNumber(rtb);
rtb.CaretPosition = rtb.Document.ContentEnd;
int iLastLine = GetLineNumber(rtb);
if (iCurrentLine == iLastLine)
{
if (!_bIsRtbMovingUpDown)
MoveToNextDataGridRow();
_bIsRtbMovingUpDown= false;
}
else
{
_bIsRtbMovingUpDown= true;
}
rtb.CaretPosition = pointer1;
}
//This code comes from
//http://social.msdn.microsoft.com/Forums/en/wpf/thread/667b5d2a-84c3-4bc0-a6c0-33f9933db07f
private int GetLineNumber(System.Windows.Controls.RichTextBox rtb)
{
TextPointer caretLineStart = rtb.CaretPosition.GetLineStartPosition(0);
TextPointer p = rtb.Document.ContentStart.GetLineStartPosition(0);
int caretLineNumber = 1;
while (true)
{
if (caretLineStart.CompareTo(p) < 0)
{
break;
}
int result;
p = p.GetLineStartPosition(1, out result);
if (result == 0)
{
break;
}
caretLineNumber++;
}
return caretLineNumber;
}
I tried the code and is not giving correct results always.
One smart way to do it is this
int previousCursorPosition;
private void RichTarget_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up || e.Key == Key.Down)
{
Xceed.Wpf.Toolkit.RichTextBox rich = (Xceed.Wpf.Toolkit.RichTextBox)sender;
previousCursorPosition = rich.CaretPosition.GetOffsetToPosition(rich.CaretPosition.DocumentStart);
}
}
private void RichTextBox_KeyUp_1(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up)
{
Xceed.Wpf.Toolkit.RichTextBox rich = (Xceed.Wpf.Toolkit.RichTextBox)sender;
if (previousCursorPosition == rich.CaretPosition.GetOffsetToPosition(rich.CaretPosition.DocumentStart))
{
//do your staff
}
}
else if (e.Key == Key.Down)
{
Xceed.Wpf.Toolkit.RichTextBox rich = (Xceed.Wpf.Toolkit.RichTextBox)sender;
if (previousCursorPosition == rich.CaretPosition.GetOffsetToPosition(rich.CaretPosition.DocumentStart))
{
//do your staff
}
}
}

Resources