Printing a sharp sign character - c#

I am trying to print this Unicode character(and many others) but the Console displays a ?.
It should print the sharp sign https://unicode-table.com/en/266F/
Could you please help me?
"kjfnv" is in UTF-16BE, I have tried it in UTF-16LE too (6F 26), but it does not work
char kjfnv = '\u6F26';

I am trying to print this Unicode character(and many others) but the Console displays a '?'.
The problem is not with the character, but it's the console not being configured the correct font to display this character.
You can manually set a console's Font, here's a tutorial on how to do it. I don't know of a way to set it programmatically (yet).
https://www.tenforums.com/tutorials/93961-change-console-window-font-font-size-windows.html
Here's a .net Fiddle: https://dotnetfiddle.net/xLvRq2
using System;
public class Program
{
public static void Main()
{
char kjfnv = '\u266F';
Console.WriteLine(kjfnv);
}
}
Output:
♯
Update:
my personal set of applicable fonts in the console is very limited - meaning: I am not able to display the character.
However, the .net fiddle proves the methodology is correct - meaning: changing your console host, or find a way to make use of the correct font in Console should do the trick.
Here's a article on how to change fonts in powershell. Possibly a similar thing exists for the default console host: https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/adding-more-fonts-to-powershell-console
#EricMovsessian: wasn't able to get this working without seriously needing to change some default values. For me this is not worth further investigation.
Keep in mind Console graphic capabilities are very limited, perhaps you want to consider an alternative technique, or just use the number sign # as a substitute

Related

Formatting text with padding does not line up in C#

I am fairly new to programming and I just wrote a simple application in C# .NET to retrieve information about system drive space. The program functions fine but I'm struggling with formatting the output.
See output:
I'm trying to use padding to get the text to line up in sort of a column format within a rich text box but the output doesn't line up because if there are multiple drives, the drive names are different lengths which throws off the padding. Even if the drive letter comes back one as M: and the other as I: the difference in the size of the letter is enough to throw off the alignment while padding.
I am wondering if there is a way to force each string value to a specific length so the padding is applied evenly or if maybe there's an even better way to format my output. Thank you in advance for your time and let me know if any further information would be helpful!
Note: One of the comments asked an important question, regarding whether the question refers to the System.Windows.Forms.RichTextBox (WinForms) or the System.Windows.Controls.RichTextBox (WPF) control. This answer applies only to the WinForms version of RichTextBox, so if you're using WPF, this doesn't apply.
The most important thing, and this was mentioned in the comments, is that you'll need to use a Monospaced font.
Since you stated you're using a RichTextBox, you'll need to know how to set it to use whatever monospaced font you've chosen.
To do that, you can use the RichTextBox.SelectionFont property.
For more general instructions, refer to this MSDN article: Setting Font Attributes for the Windows Forms RichTextBox Control
Once you set the RichTextBox.SelectionFont property, only text added to the control afterwards will use the specified font. To apply the font to existing text (i.e. you populate the RichTextBox and then change the font to an appropriate monospaced font), take a look at this answer, which tells you precisely what to do.
Once that's done, there remains the simple matter of adding the appropriate amount of whitespace to the end of each string, such that the next piece of data appears at the appropriate position. You'll probably be using String.PadRight, but for more general information about padding strings, check out this MSDN article: Padding Strings in the .NET Framework
Here is string formatting example:
string varOne = "Line One";
double varTwo = 15/100;
string output= String.Format("{0,-10} {1,5:P1}", varOne, varTwo);
//expected output is
//Line One 15 %
where formatting properties in curly brackets are:
{index[,alignment][ :formatString] }

C# window application get word from the cursor position from the other application

I have C# window application and I want to get text where the cursor is currently located or text is selected from the other application like notepad, notepad++ or any browser etc.
Did you already have a look at this CodeProject article ? This could be a start even if this is not exactly what you are looking for.
See http://msdn.microsoft.com/en-us/library/windows/desktop/ms632604(v=vs.85).aspx.
If it do not solve your issue, have a look at http://msdn.microsoft.com/en-us/library/system.windows.automation.textpattern.getselection(v=vs.110).aspx, as suggested in the comments.
Getting the text under the cursor (or from the caret) requires UI Automation and TextPattern support from the application. The problem is that not all applications support this, and the older the application, the less likely it is to have TextPattern support.
Getting selected text is, ironically enough, somewhat easier, although still not 100%. I outlined a solution in this answer. It does involve managing focus and manipulating the clipboard for the most general solution, and it is by no means perfect.
Another option, that involves a ton of work, is to use a mirror driver to capture the screen contents, and then use other technologies (OCR, etc.) to capture the text. I don't really recommend this; it's not supported in Windows 8 and above, but if you absolutely have to have 100% support across applications with the least impact, then it's a possibility. It's a lot of work, though. Definitely not for the squeamish.
This is possible using Accessibility technologies (like screen readers). However, it will require a great deal of troubleshooting:
The answer about MSAA on the following question is where you will need to start.
Best way to get the 'word before the cursor' in any open app's text field
Also, the following question is helpful about implementing it:
How to get the word under the cursor in Windows?
The problem is that you are trying to get data from another application. Unless that application supports a way to provide this to you it will be very difficult.
It would be much easier if the info could be retrieved from within the application, like from within a textbox or rich text control on a form
You can use clipborad to copy or get that text and then transfer it to your desired window.
You can use SendKeys class to demonstrate the keyboard.
For example you can use SendKeys.Send("^C") in your program and then a code to focus on Notepad++ and then SendKeys.Send("^V").
SendKeys.Send("^C");
// code to change active window and focus on Notepad++.
SendKeys.Send("^V");
Thanks for help me.
Still I'm not able to get the text from the caret position. So finally I get the all the text from the active window and fetch my text using Regex.
private string SelectText(IntPtr hWnd)
{
string text = string.Empty;
Regex regex = new Regex(#"(\d{3}-\w{5,8})");
if (InputSimulator.IsKeyDown(VirtualKeyCode.SHIFT))
{
InputSimulator.SimulateKeyUp(VirtualKeyCode.SHIFT);
}
if (InputSimulator.IsKeyDown(VirtualKeyCode.MENU))
{
InputSimulator.SimulateKeyUp(VirtualKeyCode.MENU);
}
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
text = Clipboard.GetText();
if (!string.IsNullOrEmpty(text) && regex.IsMatch(text))
{
Thread.Sleep(100);
text.Trim();
string[] textArr = text.Split(' ');
text = textArr[textArr.Length - 1];
}
else
{
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_A);
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
ClickOnPoint();
Thread.Sleep(100);
text = Clipboard.GetText();
MatchCollection matchCollection = regex.Matches(text);
if (matchCollection.Count > 0)
{
text = matchCollection[0].Value;
}
else
{
text = string.Empty;
}
}
Clipboard.Clear();
return text;
}

How to train a user who is using my code which implements system.speech and SpeechRecognitionEngine

I have already coded using the System.Speech.Recognition namespace and use a XML SRGS file for grammer and the SpeechRecognitionEngine.
I want to be able to lead the user through a training of the words or phrases that are important for the app I have written.
I have just seen and read this How to train SAPI I understand that this example uses the unmanaged API (this api exposes a little more) but is exactly the same as far as the engine is concerned.
So if I now set up a form and follow the instruction from the link to initiate training. Can i have my own text on the form and ask the user to read this text. Then conclude the training as indicated in the link. This procedure will train my speech engine that I have already coded with the System.Speech.Recognition namespace.
If I am incorrect is the next best, that I get a user to open their system panel, start the speech recognizer and get them to dictate into maybe Notepad my special phrases until it gets them right most of the time.
Or can i only suggest to them to do the general training?
Conclusion and a few other things
The C/C++ speech developers reference has many more things in it than the Automation reference. When you see here in this forum or others posts from especially Eric Brown and his blog he is more than likely referring to the C/C++ methods.
Using the below code on a Win 7 x64 bit machine for the first time caused me to get a "Class not registered" exception and Google did not help me to solve the problem. I needed to target at least "anycpu".
Otherwise the below is perfect, it basically starts the Training part of the UI that you would otherwise get from the Speech Recognizer interface in Control Panel, except you have your own words. This is perfect.
A simpler alternative is to run the existing training UI with your own training text. The automation-compatible APIs (Microsoft Speech Object Library, aka SpeechLib) expose IspRecognizer::DisplayUI, and you can call that with your own training text.
The training text needs to be a double-null terminated string, also known as a multistring. Here's some code that converts a string array to a multistring:
static string StringArrayToMultiString(
ICollection<string> stringArray
)
{
StringBuilder multiString = new StringBuilder();
if (stringArray != null)
{
foreach (string s in stringArray)
{
multiString.Append(s);
multiString.Append('\0');
}
}
return multiString.ToString();
}
Then, to actually call DisplayUI, you would do something like this:
static void RunTraining(string[] TrainingText)
{
SpSharedRecoContext RC = new SpSharedRecoContext();
string Title = "My App's Additional Training";
ISpeechRecognizer spRecog = RC.Recognizer;
spRecog.DisplayUI(hWnd, Title, SpeechLib.SpeechUserTraining, StringArrayToMultiString(TrainingText);
}

How to handle interactive input from the console using Cucumber or SpecFlow

This question is related to the codebreaker example found in the RSpec book.
See code here: https://github.com/kristianmandrup/rspec-book-codebreaker/tree/master/features
I was wondering if anyone has made a full solution for the codebreaker game.
I am keen to see how to test the outer loop, which decides if the game should continue or not.
The functionality may look like this
You found the secret code!
Would you like to try again? : n
Good-bye
If the user selects ‘y’ then the game starts again.
I am interested to see the scenarios and step definitions.
Would it be something like:
Scenario: user finds secret code
Given the secret code is "1234"
When I guess "1234"
Then I should see "You found the secret code!"
And I should see "Would you like to try again?"
Another spec file:
Background: Found secret code
Given the secret code is "1234"
And I guess "1234"
Scenario: user chooses to quit the game
Given I see "Would you like to try again?"
When I enter "n"
Then I should see "Good-bye"
Scenario: user chooses to continue with the game
Given I see "Would you like to try again?"
When I enter "y"
Then I should see "Enter guess:"
As seen in the link above, currently a double (mock) output object is passed in. All output is captured by this double and then is used in assertions. Input is a bit different. When the application displays "Would you like to try again?", then the input mock needs reply with 'y' or 'n', depending on the test. Perhaps this is not the way to go about it?
There is something similar to mimic a user guessing, that step definition looks like this:
When /^I guess "([^\"]*)"$/ do |guess|
#game.guess(guess)
end
This is the current main method:
def generate_secret_code
options = %w[1 2 3 4 5 6]
(1..4).map { options.delete_at(rand(options.length))}.join
end
game = Codebreaker::Game.new(STDOUT)
secret_code = generate_secret_code
at_exit { puts "\n***\nThe secret code was: #{secret_code}\n***" }
game.start(secret_code)
while guess = gets.chomp
game.guess(guess)
end
The outer loop was added without any tests. I would like to know how to deal with the gets.chomp - how can I 'mock' that behaviour? Is this how to handle interactive input form the console?
I am actually using SpecFlow and C#, if possible provide a solution for C#.
A Ruby solution may also help...
Thanks
It is possible to launch a console application and attach to its standard input and output streams. See the example in the documentation of ProcessStartInfo.RedirectStandardInput.
A cleaner way (which would also make the tests faster, but wouldn't qualify as full end-to-end system testing) would be to restructure the application somewhat, so that it uses regular Stream objects instead of Console.In and Console.Out; the main() method can pass these classes to the rest of the application. Then, your tests can invoke the other methods directly and pass MemoryStream objects as the input/output streams.

How Do You Simulate Typing in c#?

I am thinking of making a few video turorials on C#, my problem is that I don't type very fast and I don't want to put the user to sleep as they watch me typing in real-time.
I would like to write a small C# program that will take a line of text and feed it to the keyboard buffer, so that I can simulate keyboard typing.
Does anyone know how to access the keboard buffer to do this?
If this has been done before or if someone knows of an existing program to do this, can you point me in the right direction.
Thanks.
You should use SendKeys, i can show you how to use
here is an example: SendKeys.Send('A');
but u can use it with your own character: SendKeys.Send(CHARACTER HERE);
what happens if we have a string variable, you will get nothing
if it happens use it this way:
string letter = "exampleletter";
foreach (char ch in letter)
SendKeys.Send(ch.ToString());
i hope it works for you
Yogibear
At PDC and other conferences I've been at, they make liberal use of code snippets to quickly drop the new code into place.
I'm not really sure that you can write to keyboardbuffer or something like this
what I know it to send to some windows some keyboard commands
in your case it will be sending keyboard commands to notepad probably
in that case use the function provided above
but I would recommend to cut video parts (typing moments) out of your video
instead of writing code in C#
You could use
SendKeys Class
Provides methods for sending keystrokes to an application.

Categories

Resources