Modifying VB.NET NumericKeyPress event code to C++ - ascii

Can someone modify this for me?
Private Sub NumericKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(Keys.Back) Then
Else
If Not Char.IsDigit(e.KeyChar) Then
e.Handled = True
Beep()
Else
If Len(TextBox1.Text) > 0 Then
If Val(TextBox1.Text) > 105097565 Then
e.Handled = True
Beep()
End If
End If
End If
End If
End Sub
Or tell me how is:
1.NumericKeyPress event?
2.How to say e.KeyChar?
3.How to say IsDigit?
4.How to say Chr(ASCII number)?
5.How to handle e.Key?
6.How to system beep?
I tried:
private: System::Void textBox1_KeyPress( Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e )
{
if(e->KeyChar == (Char)8)
{}
else
{
if (!Char::IsDigit(e->KeyChar))
{
e->Handled = true;
}
else
{
if (textBox1->Text->Length > 0)
{
if (int::Parse(textBox1->Text) > 105097565)
{
e->Handled = true;
}
}
}
}
}
but it didn't work.

I added
this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox_KeyPress);
to
#pragma region Windows Form Designer generated code
void InitializeComponent(void)

Related

How can I share an event handler?

private void ExitAndSave(object sender, EventArgs e)
{
foreach (Form form in MdiChildren)
{
if (form is TextForm tf)
{
tf.BringToFront();
DialogResult dr = MessageBox.Show("Do you want to save your document?", "Save document?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
//counter++;
var textBoxValue = tf.FetchTextBoxValue();
//string filePath = $"{DateTime.Now.Ticks.ToString()}.txt";
string filePath = $"composition{DateTime.Now.Ticks.ToString()}.txt";
File.WriteAllText(filePath, textBoxValue);
}
if (dr == DialogResult.No)
{
continue;
}
}
else if (form is ImageDocumentForm)
{
MessageBox.Show("Please note that only text documents can be saved.", "Advisory:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Close();
}
This code works fine. I need it to be called when the user exits the program. I'm not able to assign this event to the form closing event in the design view.
Your event argument needs to be of the form FormClosingEventArgs e
Change that and try again.

Windows Form Multi-Select for Tree View

Is there a way to multi-select in a Windows Tree View? Similar to the image below
I know that .NET currently doesn't have a multiselect treeview. It is treated as a wrapper around the win32 native treeview control. I would like to avoid the Treeview's Checkbox property if possible. Any suggestions is greatly appreciated!
Im gonna assume you're trying to avoid check boxes. Here is an example:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
treeView1.DrawMode = OwnerDrawText;
treeView1.DrawNode += treeView1_DrawNode;
treeView1.NodeMouseClick += treeView1_NodeMouseClick;
}
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) {
// Show checked nodes with an underline
using (SolidBrush br = new SolidBrush(e.Node.TreeView.BackColor))
e.Graphics.FillRectangle(br, e.Node.Bounds);
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = e.Node.TreeView.Font;
if (e.Node.Checked) nodeFont = new Font(nodeFont, FontStyle.Underline);
using (SolidBrush br = new SolidBrush(e.Node.TreeView.ForeColor))
e.Graphics.DrawString(e.Node.Text, nodeFont, br, e.Bounds);
if (e.Node.Checked) nodeFont.Dispose();
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
if (Control.ModifierKeys == Keys.Shift && e.Node.Parent != null) {
// Extend selection
bool check = false;
foreach (TreeNode node in e.Node.Parent.Nodes) {
if (node.Checked) check = true;
node.Checked = check;
if (node == e.Node) break;
}
}
else {
unselectNodes(treeView1.Nodes);
e.Node.Checked = true;
}
}
This question has been answered here but I'll briefly answer your question. While it is true that Native Treeview control does not allow multiple selection, you can derive a subclass from it and override its behaviors.
Example code:
checkNodes method:
private void checkNodes(TreeNode node, bool check)
{
foreach (TreeNode child in node.Nodes)
{
if (child.Checked == true)
{
MessageBox.Show(child.Text);
}
//MessageBox.Show(child.Text);
checkNodes(child, check);
}
}
Treeview method after check:
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action != TreeViewAction.Unknown)
{
if (busy) return;
busy = true;
try
{
TreeNode _node = e.Node;
checkNodes(e.Node, e.Node.Checked);
if (e.Node.Checked)
{
MessageBox.Show(e.Node.Text);
}
}
finally
{
busy = false;
}
}
}
It is not trivial to do so, however it can be done.

windows phone application button content

I've got incredibly weird problem. I'm making memo game app on windows phone. When I click 2 buttons and if they have the same content they should collapse. Problem is that even if they have the same content for example 1 it shows me that this statement is false! Like 1 was not equal 1.
Here's the code:
public partial class Memo : PhoneApplicationPage
{
private int points = 100;
private string[] numbers={"a","a"};
private Button selected_button;
public Memo()
{
InitializeComponent();
button1.Content = numbers[0];
button2.Content = numbers[1];
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (selected_button != null)
{
PageTitle.Text = "" + selected_button.Content + " " + button1.Content;
if (selected_button.Content == button1.Content)
{
button1.Visibility = Visibility.Collapsed;
selected_button.Visibility = Visibility.Collapsed;
points += 3;
}
else
{
points -= 1;
selected_button.Background = new SolidColorBrush(Colors.White);
}
selected_button = null;
//PageTitle.Text = "" + points;
}
else
{
selected_button = button1;
selected_button.Background = new SolidColorBrush(Colors.Green);
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (selected_button != null)
{
PageTitle.Text = "" + selected_button.Content + " " + button2.Content;
if (selected_button.Content == button2.Content)
{
button2.Visibility = Visibility.Collapsed;
selected_button.Visibility = Visibility.Collapsed;
points += 3;
}
else
{
points -= 1;
selected_button.Background = new SolidColorBrush(Colors.White);
}
selected_button = null;
//PageTitle.Text = "" + points;
}
else
{
selected_button = button2;
selected_button.Background = new SolidColorBrush(Colors.Green);
}
}
private void button11_Click(object sender, RoutedEventArgs e)
{
button11.Visibility = Visibility.Collapsed;
}
}
Please help :)

How to move a Bug in GridWorld with arrow keys

I'm making a game for my computer Science class and I am trying to move a character object that extends Bug with the arrow keys. Should I put the code to move with the arrow keys in the Character class or in the World class? And what should the code look like? Right now I've got this code in the Character class and it complies fine, but when I try to to run it in the grid nothing happens when I press the arrow keys.
public class Character extends Bug
{
Random pokemon;
public Character()
{
}
public void act(KeyEvent e)
{
move(e);
pokemon = new Random();
if(pokemon.nextInt(10) == 5)
System.out.println("It works!!");
}
public void move(KeyEvent e)
{
Grid<Actor> gr = getGrid();
Location loc = getLocation();
if(gr == null)
return;
if( e.getKeyCode() == KeyEvent.VK_RIGHT)
{
if(!(getDirection() == 90))
setDirection(90);
else
{
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
}
else if( e.getKeyCode() == KeyEvent.VK_LEFT)
{
if(!(getDirection() == 270))
setDirection(270);
else
{
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
}
else if( e.getKeyCode() == KeyEvent.VK_UP)
{
if(!(getDirection() == 0))
setDirection(0);
else
{
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
}
else if( e.getKeyCode() == KeyEvent.VK_DOWN)
{
if(!(getDirection() == 180))
setDirection(180);
else
{
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
}
public class Character extends Bug
{
Random pokemon;
public Character()
{
}
public void act(KeyEvent e)
{
move(e);
pokemon = new Random();
if(pokemon.nextInt(10) == 5)
System.out.println("It works!!");
}
public void move(KeyEvent e)
{
Grid<Actor> gr = getGrid();
Location loc = getLocation();
if(gr == null)
return;
if( e.getKeyCode() == KeyEvent.VK_RIGHT)
{
if(!(getDirection() == 90))
setDirection(90);
else
{
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
}
else if( e.getKeyCode() == KeyEvent.VK_LEFT)
{
if(!(getDirection() == 270))
setDirection(270);
else
{
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
}
else if( e.getKeyCode() == KeyEvent.VK_UP)
{
if(!(getDirection() == 0))
setDirection(0);
else
{
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
}
else if( e.getKeyCode() == KeyEvent.VK_DOWN)
{
if(!(getDirection() == 180))
setDirection(180);
else
{
Location next = loc.getAdjacentLocation(getDirection());
if (gr.isValid(next))
moveTo(next);
else
removeSelfFromGrid();
}
}
Is this code correct for a KeyEvent and how can I call on this code from the World class?
Any help would be greatly appreciated!
In the ActorWorld class there is a method boolean keyPressed(String description, Location loc) this method is the for the sole purpose of being overridden in a subclass. description is the KeyStroke in the format found here, and loc is the Location that the cursor was on when the key was pressed. (Although in your case it doesn't matter)
So in short, you should extend KeyPressed in a Custom CharacterWorld extends ActorWorld class.

Code to execute if a navigation fails

Hello I have no idea where I should start looking. I add few prop (before that my code run fine), then I get
System.Diagnostics.Debugger.Break();
so then I comment that changes, but that didn't help.
Could you suggest me where I should start looking for solution?
MyCode:
namespace SkydriveContent
{
public partial class MainPage : PhoneApplicationPage
{
private LiveConnectClient client;
FilesManager fileManager = new FilesManager();
// Constructor
public MainPage()
{
InitializeComponent();
}
private void signInButton1_SessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
{
if (e.Status == LiveConnectSessionStatus.Connected)
{
client = new LiveConnectClient(e.Session);
infoTextBlock.Text = "Signed in.";
client.GetCompleted +=
new EventHandler<LiveOperationCompletedEventArgs>(OnGetCompleted);
client.GetAsync("/me/skydrive/files/");
fileManager.CurrentFolderId = "/me/skydrive/files/";
}
else
{
infoTextBlock.Text = "Not signed in.";
client = null;
}
}
void OnGetCompleted(object sender, LiveOperationCompletedEventArgs e)
{
//Gdy uda nam się podłaczyc do konta skydrive
if (e.Error == null)
{
signInButton1.Visibility = System.Windows.Visibility.Collapsed;
infoTextBlock.Text = "Hello, signed-in user!";
List<object> data = (List<object>)e.Result["data"];
fileManager.FilesNames.Clear();
filemanager.filesnames.add("..");
foreach (IDictionary<string,object> item in data)
{
File file = new File();
file.fName = item["name"].ToString();
file.Type = item["type"].ToString();
file.Url = item["link"].ToString();
file.ParentId = item["parent_id"].ToString();
file.Id = item["id"].ToString();
fileManager.Files.Add(file);
fileManager.FilesNames.Add(file.fName);
}
FileList.ItemsSource = fileManager.FilesNames;
}
else
{
infoTextBlock.Text = "Error calling API: " +
e.Error.ToString();
}
}
private void FileList_Tap(object sender, GestureEventArgs e)
{
foreach (File item in fileManager.Files)
{
if (item.fName == FileList.SelectedItem.ToString() )
{
switch (item.Type)
{
case "file":
MessageBox.Show("Still in progress");
break;
case "folder":
fileManager.CurrentFolderId = item.ParentId.ToString();
client.GetAsync(item.Id.ToString() + "/files");
break;
default:
MessageBox.Show("Coś nie działa");
break;
}
}
else if (FileList.SelectedItem.ToString() == "..")
{
client.GetAsync(fileManager.CurrentFolderId + "/files");
}
}
}
}
}
Running stop at that line.
// Code to execute if a navigation fails
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// A navigation has failed; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
You should check all of the URLs you have both in the XAML and code. When you get to the NavigationFailed function, it means that the phone tried to navigate to some page that did not existed. We would be able to help more if you could tell what were you doing when the app threw the exception.
System.Diagnostics.Debugger.Break();
usually happens because of an Uncaught Exception.
Either post the code which started giving problems, or the stack trace when you encounter this problem.
No one can tell anything without actually seeing what you are doing.

Resources