I'd like to pass a generated array (arNum[20]) from Form number 1(MyForm.h) to Form number 2(MyForm1.h) in the following function
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int x;
int arNum[20];
if (!int::TryParse(txtNodes->Text, x))
MessageBox::Show("First box wasn't an integer");
else
{
for (int i = 0; i < txtNumbers->Text->Length; i+=2)
{
if (!(int)txtNumbers->Text[i])
{
MessageBox::Show("Invalid Input!");
return;
}
else
{
arNum[i] = (int)txtNumbers->Text[i];
}
}
MessageBox::Show("Data submitted correctly.");
MyForm1 ^form = gcnew MyForm1();
form->
}
}
Thanks.
Related
I'd like to add Ladder by the number of counts. How do I get the numbers in and work smoothly?
public void NumOutbox_TextChanged(object sender, TextChangedEventArgs e)
{
var temp = NumOutbox.Text;
if (int.TryParse(temp, out int result))
{
if (result >= startBox.SetBoxMIN && result <= startBox.SetBoxMAX)
{
ChangeColumn(result);
}
else
{
NumOutbox.Text = "2";
}
}
else
{ .
MessageBox.Show("숫자를 입력해주세요 \nPlease enter a number");
}
}
private void ChangeColumn(int count)
{
// I want to have Ladder as many as the count, but I don't know how.
// LadderSet ladder = new LadderSet();
// Ladders.Children.Add(ladder);
}
enter image description here
enter image description here
I have an array with 5 integer values.
I need to check each value in it against input values from a text box.
A label displays a Value found message if the input value exists in the array.
If not, theValue not found message is displayed.
How do I display the Value not found message correctly?
Here's my code,
private void button1_Click(object sender, EventArgs e)
{
try
{
int[] id = { 1, 2, 3, 4, 5 };
int row;
int x = Convert.ToInt32(txtid.Text);
for (row = 0; row < id.Length; row++)
{
if (id[row] == x)
{
txtdesc.Text = "Value found!";
}
}
txtdesc.Text = "Value not found!";
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Without going into great detail, using a flag is the simplest way of tracking a Boolean state when you are first starting out.
private void button1_Click(object sender, EventArgs e)
{
try
{
int[] id = { 1, 2, 3, 4, 5 };
int row;
int x = Convert.ToInt32(txtid.Text);
bool found = false;
for (row = 0; row < id.Length; row++)
{
if (id[row] == x)
{
found = true;
}
}
txtdesc.Text = found ? "Value found!" : "Value not found!";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
With LINQ, these things get a lot easier:
// At the top of the file
using System.Linq;
private void button1_Click(object sender, EventArgs e)
{
try
{
int[] id = { 1, 2, 3, 4, 5 };
int x = Convert.ToInt32(txtid.Text);
txtdesc.Text = id.Contains(x) ? "Value found!" : "Value not found!";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
int[] id = { 1, 2, 3, 4, 5 };
int row;
int x = Convert.ToInt32(txtid.Text);
for (row = 0; row < id.Length; row++)
{
if (id[row] == x)
{
txtdesc.Text = "Value found!";
return; //only add this line
}
}
txtdesc.Text = "Value not found!";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
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);
}
i try to write on label an array with methods. When i try to put values of array it writes system int 32 on label, here is the code and how can put values on label
p.s. sorry about bad English
enter code here void arr_5( int[] mas5)
{
for (int i=0, j=5; i<10; i++, j+=5)
{
mas5[i] = j;
}
}
private void button1_Click(object sender, EventArgs e)
{
int[] a = new int[10];
arr_5 (a);
label1.Text += a.ToString() + " ";
}
That's because an array contains more than one value and in order to show it you could do something like:
int[] arr_5( int[] mas5)
{
for (int i=0, j=5; i<10; i++, j+=5)
{
mas5[i] = j;
}
return mas5;
}
private void button1_Click(object sender, EventArgs e)
{
int[] a = new int[10];
arr_5 (a);
String label="";
for(int i=0; i<a.length; a++)
{
label= label + a +" ";
}
label1.Text =label;
}
Please be aware that i've changed your arr_5 to return the newly created array.
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
}
}
}