Trying to implement "Enter" event handler to textbook - c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;
namespace HelloWorld
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string filePathTEST = "";
public MainWindow()
{
InitializeComponent();
// Clearing text event handlers
textBox1.GotFocus += textBox1_GotFocus;
// Enter event handlers for textboxes
textBox1.KeyDown += new System.Windows.Input.KeyEventHandler(textBox1_KeyDown);
}
static void textBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//enter key is down
}
}
The error I am getting when I try to run the above code is the following:
Cannot implicitly convert type
'System.Windows.Forms.KeyEventHandler' to
'System.Windows.Input.KeyEventHandler'
Then I tried changing the code to System.Windows.Input and then I get the following:
Error 1 'System.Windows.Input.KeyEventArgs' does not contain a
definition for 'KeyCode' and no extension method 'KeyCode' accepting a
first argument of type 'System.Windows.Input.KeyEventArgs' could be
found (are you missing a using directive or an assembly reference?)
The whole point of me doing this is that when I press enter on a textbox, I want to take the text in that textbox and populate a certain text file with it but I am not sure how to go about doing that.

The compiler thinks you mean to use 'System.Windows.Forms.KeyEventHandler' due to the namespace you've added: System.Windows.Forms.
Remove this line and your code should work:
using System.Windows.Forms;
Second, you should use Key instead of KeyCode since that is the WPF variant:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
//
}
}

You're running foul of the rather confused state of Windows client development. System.Windows.Forms is part of the WinForms library, the original UI framework that shipped with .NET 1.0. The project you're working with is written in WPF (Windows Presentation Framework) the new lib that started shipping in .NET 3. There's a lot of overlap, but the two libraries are distinctive.
The class you want is System.Windows.Input.KeyEventArgs. If you go up onto MSDN for the documentation for this class, you'll see that it does NOT in fact have a KeyCode property. It does, however, have a Key property which should do what you want.
I'll leave actually finding and reading the MSDN documentation as an exercise for the reader. :-)

Related

Getting the path to the game through the registry + launching the process in the final folder of the path by pressing the button C # + WPF

Trying to read the installation path of the game (InstallLocation) from one section in the registry (SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 4000), write it to another section (SOFTWARE\WpfApp) in key (NewInstallPath), and then find the exe file with the game in the installation path of the game, and run
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
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;
using Microsoft.Win32;
namespace wpfApp
{
/// \<summary\>
/// Interaction logic for MainWindow.xaml
/// \</summary\>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
using var localMachineRegistry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
using var gmodAppRegistry = localMachineRegistry.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 4000", false);
string readInstallPath = gmodAppRegistry.GetValue("InstallLocation").ToString();
RegistryKey InstallPath = Registry.LocalMachine.CreateSubKey(#"SOFTWARE\WpfApp", SetValue("NewInstallPath", readInstallPath)); //launcher partition path
}
}
}
As soon as I did not try to implement it, but even according to the documentation it comes out crooked. That value is recorded, but the game does not start, then nothing is recorded at all and does not start.
The way you're intended to run steam games is through steam itself.
Steam automatically updates the app etc.
There is a rungameid command line parameter which allows you to run a game ( somewhat ) directly.
"C:\Program Files (x86)\Steam\steam" steam://rungameid/281990
And... eventually as my login credentials install etc etc were stale...
Steam may be in C:\Program Files instead.
The nice-ish aspect of this is that you just need to check where steam is and know your game app id.
You can also run a game that isn't currently installed this way. There will of course be quite a delay as steam goes and gets everything for you.
I think some games are not so straight forward that they "just" have the one exe. Some have launchers ( ours does ).

System.IO.FileNotFoundException In multi-project solution

So I've been stuck on this error for a good week now, and it has been very frustrating
(Impossible to load the file or assembly 'Audio.Default.Switcher.Wrapper.dll' or one of its dependencies. The specified module wasn't found)
I'm working with Visual Studio 2017 and I've downloaded the SoundSwitch project, and everything is working perfectly fine. I'm able to run "SoundSwitch" without any errors.
The way the solution works (as I'm able to understand) is that the "SoundSwitch" C# project is the "master" and it has references to "Audio.Default.Switcher.Wrapper" as well as "SoundSwitch.UI.UserControls"
"Audio.Default.Switcher.Wrapper" as a reference to "AudioDefaultSwitcher"
Both "Audio.Default.Switcher.Wrapper" and "AudioDefaultSwitcher" seem to compile as .dll files
"SoundSwitch.UI.UserControls" cannot be started and is used for display stuff I guess
"SoundSwitch" is the only startable project (not counting mine of course)
The K005_test is MY project, I've added it and it has references (only) to "SoundSwitch" and I'm trying to use the functions from the SoundSwitch project inside K005_test. And so far my K005_test project consists only of
Program.cs (where I get the error by the way)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace K005_test
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); //Exception thrown here
}
}
}
Form1.cs (where I put my code for the test)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace K005_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("This is a test");
var test = SoundSwitch.Model.AppModel.Instance.AvailablePlaybackDevices; //Error this line exists
}
}
}
And when I run my K005_test project, it compiles and runs just fine, except when I click the button Then I get the error I posted above. Also the message box doesn't open.
What I've tried so far :
Flipping everything to "x86" or "Win32", it seems that it might be related a problem with 64bit something
Using references to both "Audio.Default.Switcher.Wrapper.dll" and "SoundSwitch" in K005_test
copy "Audio.Default.Switcher.Wrapper.dll" pretty much everywhere and praying the gods
Trying to understand where it looks for "Audio.Default.Switcher.Wrapper.dll" but I wasn't able to find anything when looking in the exception details
Nothing has helped in any way.
For anybody wondering, I've uploaded my entire project folder here
https://drive.google.com/open?id=1OABjDiZyF0B-1-b15_9lzMrPZ4UpyKqU

The type or namespace name does not exist in the namespace (are you missing an assembly reference?)

I know that this question has been already asked, but I cannot find anything that can help solve my problem.
I want to connect my aspx page (Retete.aspx) to a Microsoft SQL database. I'm getting this error on Retete.aspx.cs:
Error 1 The type or namespace name 'GlobalClass' does not exist in the namespace 'ProiectSera' (are you missing an assembly reference?)
The error points to this line of code:
ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
Where ProiectSera is the project name, GlobalClass is the file where I make the operation on the db.
My using statements are:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
The Target Framework is set to .net-4.5.
What should I do to solve this error?
Update
My GlobalClass.cs is:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.IO;
using ProiectSera;
using System.Data.Services;
namespace ProiectSera.App_Code
{
public static class GlobalClass
{
static public void Update(string _param1, string _param2)
{//function to update}
}
}
App_Code is a folder where GlobalClass.cs is. I tried and
ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text); //
but I had the same error. And I put the GlobalClass.cs in the project's root. I also removed the .App_Code from namespace ProiectSera.App_Code
UPDATE1
My Retete.aspx.cs is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProiectSera;
using ProiectSera.App_Code;
namespace ProiectSera
{
public partial class Retete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
string intValRefTempSol;
string intValRefTempAer;
// intValRefTempSol = ValRefTempSol.Text;
// intValRefTempAer = ValRefTempAer.Text;
// ProiectSera.App_Code.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
}
}
}
Your GlobalClass is in the namespace ProiectSera.App_Code.
So the class name is ProiectSera.App_Code.GlobalClass
Make sure you don't have a ProiectSera class in the ProiectSera namespace also, otherwise if declaring using ProiectSera on top, it'll try to use it (as a general rule, don't name any class the same as your namespace).
If that still doesn't work, you may want to try using the global namespace:
global::ProiectSera.GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
and see if that works: if it doesn't, and GlobalClass is in the same project, then there's something else you haven't shown us
Update
The only other thing that comes to mind, if you are positive that both files are on the same project/assembly, is that GlobalClass.cs is not being actually compiled. Make sure the Build Action is set to Compile (you can see the build action right clicking on the GlobalClass.cs in the solution explorer and selecting Properties).
If you are using VS.NET:
Right click on the References folder on your project.
Select Add Reference.
Select the .NET tab (or select the Browse button if it is not a .NET Framework assembly).
Double-click the assembly containing the namespace in the error message.
Press the OK button.
Ensure the following...
That you have a project reference to ProiectSera from your web application, if it's in a separate library. If GlobalClass is in the web application project itself, you won't require this, but if GlobalClass is defined in a separate library in the same solution or elsewhere, then you're going to need to add a reference to that external library in your web application.
Ensure that ProiectSera should not be ProjectSera (if it's a typo).
Ensure that you have your ProiectSera using statement in place at the top (using ProiectSera;). Assuming that this is the correct namespace. Check the namespace of your GlobalClass class and use that using accordingly in the class that wishes to use its functionality.
Then, simply call this in your code, assuming that Update is a static method of GlobalClass.
GlobalClass.Update(ValRefTempSol.Text, ValRefTempAer.Text);
EDIT: given that you've now posted your GlobalClass code, you can see that the namespace for this class is ProiectSera.App_Code;. So you need to have that as your using statement in the other class using ProiectSera.App_Code;. And then call it via simply GlobalClass.Update, as mentioned above.
If you don't understand how namespacing works in C#, then I recommend you check this out (https://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx).

'Debug' does not contain a definition for 'Print'

In the following code I would like to just output "hi" into a console window as a test when both of the check boxes are checked.
using System;
using System.Diagnostics;
private void button_Click(object sender, RoutedEventArgs e)
{
show.Text = inputText.Text;
if (check1_cont && check2_cont == true )
{
Debug.Print("hi");
}
}
The only problem is that in System.Diagnostics, the 'Print' in 'Debug.Print' doesn't seem to exist. I've checked https://msdn.microsoft.com/en-us/library/system.diagnostics.debug.print(v=vs.110).aspx and the Print method does exist. Any help regarding a solution to an absent Paint method would be much appreciated.
EDIT 1:
Apparently Debug.WriteLine doesn't give an error, but when I run the program and check the two boxes and press the button, no console appears.
EDIT 2:
In case it helps anyone, here is the full code for the GUI application that I am using.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using Microsoft;
using System.Threading.Tasks;
using System.Data;
using System.Dynamic;
using System.Xml;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace normieap
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public Boolean check1_cont = false;
public Boolean check2_cont = false;
public MainPage()
{
this.InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
show.Text = inputText.Text;
if (check1_cont && check2_cont == true )
{
MessageBox.Show("hi");
}
}
private void check1_Click(object sender, RoutedEventArgs e)
{
check1_cont = true;
}
private void check2_Click(object sender, RoutedEventArgs e)
{
check2_cont = true;
}
}
}
The button is going to cause multiple things to go at once, one of the things I would like to go at once is an instance of a console window of some sort to open.
EDIT 3:
This is meant to be a application for PC but for some reason when I started the project, Visual Studio 2015 community thought it was an application for a phone and is causing problems with PC exclusive commands. If someone could give information on how to fix that as well, that would be great.
You appear to not be using the full desktop framework and instead are using some kind of mobile framework. For example, if you check Silverlight's documentation you can see Debug.Print is not available.
If you check the "Version information" for Print you will see
Version Information
.NET Framework
Available since 2.0
Switch to Debug.WriteLine("hi") instead, that should work in all frameworks, its version information says
Version Information
Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
If you are not seeing output I would recommend showing what your test variables are instead.
private void button_Click(object sender, RoutedEventArgs e)
{
show.Text = inputText.Text;
Debug.WriteLine(check1_cont.ToString());
Debug.WriteLine(check2_cont.ToString());
}

Argument exception on URI parsing when downloading video

I'm not experienced with C#, but I got the basics down. Now I'm trying to download videos from YouTube with the Video Library (in the VS package manager: Install-Package VideoLibrary).
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;
using System.IO;
using VideoLibrary;
namespace TubeDemo
{
public partial class MainWindow : Window
{
string link = "https://www.youtube.com/watch?v=8SbUC-UaAxE";
string link2 = "https://www.youtube.com/watch?v=BlRqTNkgEuo";
public MainWindow()
{
InitializeComponent();
}
void SaveVideoToDisk_Click(object sender, EventArgs e)
{
var youTube = YouTube.Default; // starting point for YouTube actions
var video = youTube.GetVideo(link2); // gets a Video object with info about the video
File.WriteAllBytes(#"C:\testfire\" + video.FullName, video.GetBytes());
}
}
}
Above functions SaveVideoToDisk_Click gets called from a .xaml button, which works fine. But not every video works OK. video.URI gets awfully big, over 800 characters. Some of the URLs turn out to cause the video.URI to throw an exception:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
In the code provided, passing link as argument throws while passing link2 works just fine.
Can I fix this?
If not, how should I handle these exceptions? Just try, catch and report or is checking before a better idea?

Categories

Resources