Find the x, y point of a string within a text box - c#

Is there a way to return a point for a string within a text box? I found a COM function GetTextExtentPoint that will return the length of a string, but I want to know the point where the string starts.

You're looking for the GetPositionFromCharIndex method.

First, figure out the index of the first character of the string.
int index = textBox1.Text.IndexOf(someString);
Then use GetPositionFromCharIndex.
Point stringPos = textBox1.GetPositionFromCharIndex(index);
(Code not tested, but something like this should work. Of course you will have to deal with the possibility of duplicate occurrences of your string in the textbox.)

what comes to mi mind is to take a snapshot of both the form and text then do some fancy image comparing to find the starting point.. but for this you need to write/download a library that has theese comparing methods... thus becoming very complicated...
why do you need to do this?

Related

Printing Different Patterns in Single Program

Are there solutions for the following problem?
A user enters an X coordinate, Y coordinate, length, and (optional)
number. If a number was entered, print a straight line with the
specified length, followed by the (x,y) coordinates. If n=2, print
bisecting lines with the specified length. If n=3, print a triangle
where the lines are the specified length.
Assuming you want ASCII drawings, here's how this would work. Note that I'm just providing an outline of how this would work, since I don't want to answer the interview question for you. Also, this is not production-quality since there's no validation (or differentiation between lack of input and invalid input).
First, let's ask the user for their input. Console.ReadLine is how you do this. Since we'll be getting input four times, let's make it a method. The return type can be int since everything we're getting from the user is a number. We'll print the prompt (passed in as an argument) with Console.WriteLine and then return the result of Console.ReadLine after converting it to an int (do you need validation)? Since n is optional, maybe return something like -1 if the user doesn't enter anything.
Store the results so that we can use them later in our calculations. Use an if... else if (or switch) statement to determine if n was supplied by the user and take action accordingly. We can call different methods depending on whether we want to draw a line, two bisecting lines, or a triangle.
Is the actual drawing the problem? I'm having trouble understand exactly where you need help, and the drawing of the shapes would be more complicated. For now, though, this should get you on the right track.

Split Double Values?

i have two values written like this " +5.000" (first space then plus and the double value, double value is height in meters)
the first one is on textBox and second one is i'm receiving via ref key.
simply enough i want to get the result firstvalue - secondvalue = result
for example ( +5.000 - +2.800 = 2.200)
result only in digits without plus.
i have asked this question also on c# forum
https://social.msdn.microsoft.com/Forums/en-US/a024e097-8013-4771-bbf6-99c7fd4cf457/double-split-
Thanks and Best Regards
OK, I think you're trying to solve a few problems here in one question. Let's break it down.
You need to get a value from a text box. I'll assume its called txtBox in the absense of any code, so you need to write:
double a = Double.parse(txtBox.Text);
You then need to perform your calculation. This needs to be written the other way around, for example:
result = a - b;
With limited source code it's difficult to answer properly.

How can I increment and decrement a unicode character by 1?

I am writing a small program with a label and 2 buttons. The label is initialized with a unicode character, for example "\u221A" (√).
I would like 1 button to increment the unicode value of the label, say to "\u221B", then to "\u221C", etc, and another to decrement it, say to "\u2219", then "\u2218", etc.
I have no idea where to begin, and have googled for quite a while. I've tried doing stuff myself, but none of it compiles.
char is a numeric type. You can add and subtract numbers from it, then cast it back to char and create a string from it.
Without code I can't give a specific answer. However perhaps this is what you're thinking of:
char ch = label.Text[0]; // assumes label is not empty; get first character of string
++ch; // increment; use -- to decrement instead
label.Text = ch.ToString(); // back to string
Add your own error handing and range checking. Also keep in mind that many code points aren't defined and many others won't display in certain fonts.

Parsing in CSharp - how to understand it

It's just few days ago that I jumped into learning C# and I already have one problem with understanding basics.. Maybe it's just the language barrier (I'm not English native speaker). Please, could you explain me how to understand parsing? For example: while creating a very simple calculator I wanted to read the first input number (which is a variable a). I use this code:
float a = float.Parse(Console.ReadLine());
and the same with b for the other number:
float b = float.Parse(Console.ReadLine());
I learnt that the float is a data type for decimals numbers so what exactly does this particular Parse() stands for?
Obviously, I tried to run the application without parsing and it wouldn't work because it reads it as string, but why? Thank you..
Console.ReadLine() returns a string, which represents a piece of text. So, from the computer's point of view, what you have after calling Console.ReadLine() is a piece of text. It may or may not contain the text "6.0", but from the computer's point of view, it is just a piece of text. As such, you cannot use it to add, subtract etc.
Using the float.Parse(...) method, you tell the computer: "This piece of text actually represents a floating point number, could you please read the text and give me back a number, so that I can start doing math with it?".
The method you are using, float.Parse() is just one of many such methods that take a String input value, and attempt to convert it into the target type, here a float.
There is a safer alternative, however, and it is TryParse():
float a;
if (float.TryParse(Console.ReadLine(), out a))
{
//do something with your new float 'a'
}
In either case, your are asking the framework to inspect the value you provide, and attempt to make a conversion into the requested type. This topic can be quite deep, so you'll want to consult MSDN for the specifics.
Console.ReadLine reads text that the user inputs and returns it to the program so that you may do with it what you want. Therefore, the ReadLine method returns a string.
If you want to work with a decimal (check the decimal class instead of float), you need to convert the string, which is a character sequence, to a number of your desired type, that's where float.Parse comes in:
float.Parse accepts a string and if possible, returns a float value.
Almost every type contains the Parse method which is used to transform a string into the calling one.

How do I receive and store data input in C#?

Basically I'm still in the starting phase, and I know how to use Console.WriteLine, but I have no idea on how to have it read input from the user.
For example, I'd want a user to be able to type in a pair of numbers, then return the result.
My goal is a program that receives input , then combines them and returns the average.
This is all in Visual C# Express 2008
string input = Console.ReadLine();
that will return you a string of the user's input. Check out MSDN for documentation on the Console class. Also look at the Convert class.
int num = Convert.ToInt32(input);
Good luck new coder.
First create a variable to store the user input, like this:
int variablenameofyourchoice = 0;
Then take input like this and store it in your variable name:
variablenameofyourchoice = int.parse(Console.ReadLine())
Then do whatever you want with that variable. If you want two numbers do this twice.
This is a very general topic with a lot of answers, but Console.ReadLine is one counterpart of Console.WriteLine. It reads a line of text from standard in.
Have a look at Console.ReadLine() or Console.Read() in the MSDN Documentation

Categories