|
Because 08H is not a valid hex number, in any language that I know of . The best way to convert this to a byte (which can hold 0-255) is:
byte converted = 0x00;
bool successful = byte.TryParse(inputString.Substring(inputString.IndexOf("x")), out converted);
if (!successful)
{
MessageBox.Show("Problem!");
}
else
{
}
|
|
|
|
|
Ed.Poore wrote: Because 08H is not a valid hex number, in any language that I know of
It was 80H and it seems valid to a hex converter clickety.
Basically what I am doing is reading in a list of Hex values, provided by Microsoft, of keycodes to be used by keybd_event .
You always pass failure on the way to success.
|
|
|
|
|
The H can be used as a suffix for some programming languages (can't remember which), 0x is the standard C, C++, C# prefix etc. It's just used by the compiler to determine whether it's a hex number or a decimal number. You're getting beyond the range of simple .Substring stuff and moving dangerously close to Regular Expression territory.
|
|
|
|
|
Thanks - I think I've been hit by the stupid stick today.
Been doing too much high level programming and forgot how hex works
I fixed it by limiting the substring to 2 characters.
int integer = int.Parse(tmpByte.Substring(tmpByte.IndexOf("x") + 1,<big>2</big>), System.Globalization.NumberStyles.HexNumber);
Thank you to everyone for their help
You always pass failure on the way to success.
|
|
|
|
|
You could use something like the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
namespace HexNumbers
{
class Program
{
private static void Main(string[] args)
{
var regex = new Regex(@"(%|&?#|\\x|U\+|=|H'|16#|8'h|#|16r|16#|#x|#16r|&H|&h|$|&|0h|0H)?(?<number>[\d|A-F|a-f]+)('?|#)?(h|H)?", RegexOptions.Compiled);
var numbers = new string[] { "%20", "ꯍ", "#FF", "0x5A3", "\\x1B", "U+20AC", "=D1a", "FFh", "0A3CH", "$5A3", "H'ABCD'", "16#5A3#", "8'hFF", "#01AF", "16r6EF7", "16#ABCD", "#xABC", "&H5A3", "&5A3", "0hA3", "X'5A3'" };
foreach (var number in numbers)
{
var isMatch = regex.IsMatch(number);
if (isMatch)
{
Console.WriteLine("{0} is a match and it's value is {1}", number, int.Parse(regex.Match(number).Groups["number"].Value, NumberStyles.HexNumber));
}
else
{
Console.WriteLine("{0} is not a hex number", number);
}
}
}
}
} Sorry about the length of the expression but is should handle all types of hex number as dictated by the Wikipedia listing for prefixes and suffixes. You could change the + symbol after the (\d|A-F)+ bracket to {2} if you want to limit it to two hex digits.
|
|
|
|
|
|
Well if I knew the restrictions that you're placing on the data it could be greatly simplified but thought I'd cater for as much as I could.
|
|
|
|
|
Out of interest Ed (and forgive me, I'm a little bit pissed), but why the "var" on Regex and numbers?
|
|
|
|
|
It's a new feature in C# version 3.
It's needed for when you are using anonymous types in LINQ.
It can also be used to make the code easier to read if the data type that you are declaring is obvious from the context, like:
var cmd = new System.Data.SqlClient.SqlCommand(query, connection);
instead of repeating the data type:
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query, connection);
However, Ed is using it everywhere, even when the data type is not obvious from the context, which instead makes the code harder to read.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Guffa wrote: However, Ed is using it everywhere, even when the data type is not obvious from the context, which instead makes the code harder to read.
That's what I thought
|
|
|
|
|
Guffa wrote: However, Ed is using it everywhere, even when the data type is not obvious from the context, which instead makes the code harder to read.
Cos me is lazy
Besides It's very rare anyone else looks at my code so I tend to use it out of habit because I don't have a problem remembering what type a variable is. The name is usually enough if I don't remember straight away. Besides the IDE makes it easy enough to check.
|
|
|
|
|
That's fine when you are playing with yourself, but it doesn't make good example code.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Meh, saves my poor fingers from wearing down with extraneous key-strokes. Besides makes the transition to JavaScript easier
|
|
|
|
|
Hello,
I have a problem...
In designer I have set data source to "subjektBindingSource" (without "").
Then on click on a button I delete a selected row from a database, and it works ok, but my datagridview doesn't show the changes...
<br />
SqlConnection conn = new SqlConnection();<br />
<br />
conn.ConnectionString = connStr;<br />
String naredbaSql = null;<br />
naredbaSql = "DELETE from Subjekt WHERE ID = " + idUser;<br />
<br />
conn.Open();<br />
<br />
SqlCommand comand = new SqlCommand(naredbaSql, conn);<br />
comand.ExecuteNonQuery(); <br />
<br />
conn.Close();<br />
<br />
So, all I want is to refresh my datagridview so it shows data from database without that row I erased. I tried with Refresh and Update methods, but it doesn't work...
can you help me?
Thank you,
Niksa
|
|
|
|
|
Check where you are binding your datagridview
Sincerely,
Elina
Life is great!!!
Enjoy every moment of it!
|
|
|
|
|
Hy,
I'm really new to C# programming and I have a few problems with data binding.
I am building a Windows application.
I have two tables in my database and they are related.
First table is named Author (and has these fields: AuthorID, AuthorName),
and the other one is named Books (BooksID, AuthorID -> this is a Foreign Key, and BookTitle).
My question is: How can I (using a form) change the data in my database?
I have found a lot of tutorials, but they didn't really helped me. All they suggest is that I can relate tables on a form by dragging AuthorName from the Author table on AuthorID from the Books table. But if I do that, Authors are displayed in combobox so I can't enter a new author if I want to enter new data...
I know that I probably made it sound really confusing, so I apologize in advance
Thanks
|
|
|
|
|
Alessandra77 wrote: I'm really new to C# programming
Alessandra77 wrote: I have two tables in my database and they are related.
If you're REALLY new, then you're probably diving in a little too deep.
Alessandra77 wrote: How can I (using a form) change the data in my database?
Your form should ideally just display the data, and call a middle tier that knows about books and authors, and which then calls a data layer that knows how to persist your data to the DB.
There's two UI approachs to take, one is to show your data in a grid and support entering new data which is then saved, the other is to have a seperate form that shows your fields and you enter them to have them entered into your DB.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks for your help, I'll try to do what you suggested...
|
|
|
|
|
I have built a custom message box form (Windows form), on this form I have a label which displays a message that gets passed in to the message box. My question is how can I resize the label and form based on the text passed in? if a large block of text is passed in, the form and label need to resize to be able to show all the text and not just cut it off.
thanks for help
|
|
|
|
|
Check out this for measuring a string (in pixels) http://msdn2.microsoft.com/en-us/library/403ezxd2(VS.80).aspx[^]
Just use MeasureString on your string, and it will return the width and height of the text, which you can then use to resize your label and form. Hopefully
My current favourite word is: Bacon!
-SK Genius
|
|
|
|
|
Hi,
set label.AutoSize true so the label grows and shrinks automatically,
then set label.Text, now get label.Size and adapt form.Size accordingly.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
I have built a custom gradient form as part of a windows control library. What I would like to do is to hide the BackColor property from the forms designer (since it has now been replaced by BackColor1 and BackColor2). Currently when I create a new inherited form which inherits from my GradientForm in the designer pane I can see BackColor, BackColor1, and BackColor2. With a windows control I would use something like the following to remove BackColor:
internal class GradientPanelDesigner : ScrollableControlDesigner
{
public GradientPanelDesigner()
{
}
protected override void PreFilterProperties(System.Collections.IDictionary properties)
{
properties.Remove("BackColor");
properties.Remove("BackgroundImage");
}
}
Is there an equivalent for a Windows Form?
thanks for help
|
|
|
|
|
Hello,
i have a combobox binded in the xaml code to an xml file:
<stackpanel.resources>
<xmldataprovider>
x:Key="AufgabenlisteXml"
XPath="Aufgaben/Fachgebiet"
Source=".\Aufgabenliste.xml">
<combobox horizontalalignment="Left" x:name="ComboBoxFach" selectedindex="1">
DataContext="{StaticResource AufgabenlisteXml}"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True">
<combobox.itemtemplate>
<datatemplate>
<textblock text="{Binding XPath=@FachName}">
The ComboBox works fine, but when i try to use the selected value in my Code i just get back an empty string.
Code i tried:
String new = ComboBoxFach.Text.ToString();
or
String new = ComboBoxFach.SelectedValue.ToString();
Other propertys of the ComboBox can be read. For example the SelectedIndex is shown correctly.
I think the problem is cause of the data binding cause i`m new to these concept.
Hope someone can help.
Thanks for answers.
|
|
|
|
|
What about SelectedItem.ToString() ?
My current favourite word is: Bacon!
-SK Genius
|
|
|
|
|
Selected.Item.ToString returns System.Xml.XmlElement - and i have no Idea how to convert this.
|
|
|
|