|
Hi all,
I wanted to ask if it is possible to make the combobox only accept values from its dropdown options, because you can just enter some rubbish in the combobox and then I would have to check if the chosen input actually is a valid input(ie one from the options). Is there another way to do this or I would have to validate the input against the options?
Regards,
gamehack
|
|
|
|
|
Yes, you can. Check out the ComboBox.DropDownStyle property.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Thank you
|
|
|
|
|
My pleasure. Happy coding!
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
I was looking at some advanced code for learning experience. Can someone explain why a Dispose method would be used in classes? Is the destructor not good enough?
~RythmMachineApp()
{
Dispose();
}
public void Dispose()
{
if (Mixer != null)
Mixer.Dispose();
System.GC.SuppressFinalize(this);
}
|
|
|
|
|
A Dispose method is used to make the object release any unmanaged resources. If you would put that in the destructor instead, the resources would no be released until the object is garbage collected.
---
b { font-weight: normal; }
|
|
|
|
|
The reason is that you have zero control over when the "finalizer" (as the destructor is called in C#) is called.
If you have a class that has a Dispose method then you are supposed to call it when you are done with any object of that class. The Dispose method would normally close connections to external resources, free up unmanaged objects and so on.
You call Dispose from the finaliser in case the caller of the class did not clean it up properly. It also means that you only have one method where the clean up code resides.
You can have the language automatically called Dispose on objects with an IDisposable interface like this:
using (SomeDisposableObject obj = new SomeDisposableObject())
{
}
Does this help?
My: Blog | Photos
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious
|
|
|
|
|
I have an understanding now. I thought the redundancy with destructor and Dispose was odd, but now i see it is a way to have more control over the NET framework behavior.
|
|
|
|
|
I was looking at some advanced code for learning experience. Can someone go over and explain the weird looking parts of the following code snippets?
SNIPPET #1
What is with the '?' and ':'?
public int GetBufferedSize()
{
return m_Player != null ? m_Player.GetBufferedSize() : 0;
}
lock(this)
{
return m_Playing ? this.Size : 0;
}
SNIPPET #2
What is with the 'this'?
public Track this[int ndx]
{
get
{
lock(m_Tracks)
return (Track)m_Tracks[ndx];
}
}
|
|
|
|
|
?: is the ternary operator that is the equivalent of if/else
a?b:c ==> if(a){b}else{c}
Does this help?
My: Blog | Photos
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious
|
|
|
|
|
|
Forgot the second one.
In the context you have presented this[] creates an indexer method for the class.
My: Blog | Photos
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious
|
|
|
|
|
It's a conditional value. If the value in front of the question mark is true, the first value is used, otherwise the second. It has the same effect as:
public int GetBufferedSize()
{
if (m_Player != null) {
return m_Player.GetBufferedSize();
} else {
return 0;
}
}
redfish34 wrote: What is with the 'this'?
That is the way you declare an indexer for the class.
---
b { font-weight: normal; }
|
|
|
|
|
|
I've found several articles on comparing two bitmaps to see if they are the same, however, they address two images that are the same size. What if I have two images of different sizes and I want to determine if the smaller image resides somewhere within the larger one. This seems like an easy task, but I'm not having any success as of yet.
Any help would be appreciated.
Thanks.
|
|
|
|
|
I've been reading through the projects here on codeproject, but i still don't fully grasp all of this.
I'm writing a simple class that I can use manage application settings in my forms. Note, it is not meant to handle nested elements. There will be one root element "<configuration>" and inside that will be the settings as key/value pairs.
Here's what I've got so far:
class Config
{
private string m_Path;
private string m_Err;
private XmlDocument xmlDoc;
private XmlNode xmlNode;
// The Config constructor - supply the path name to the file, load the document
public Config(string Path)
{
m_Path = Path;
Load();
}
// Load the stored settings into the XmlDocument
public bool Load()
{
try
{
XmlTextReader xtr = new XmlTextReader(m_Path);
}
catch (Exception ex)
{
m_Err = ex.ToString();
return false;
}
try
{
xmlDoc = new XmlDocument();
xmlDoc.Load(xtr);
}
catch (Exception ex)
{
m_Err = ex.ToString();
return false;
}
return true;
}
// Attempt to return the value, given the key name
public string GetValue(string key)
{
// ITERATE THROUGH KEYS, IF FOUND, RETURN THE VALUE, OTHERWISE, return "NULL";
return "NULL";
}
// Attempt to set the value of the given key. If it does not exist, create a new key/value
// Upon creation or modification of the XmlDocument, write the document to the configuration file
public bool SetValue(string key, string value)
{
// ITERATE THROUGH THE VALUES, FIND THE CORRECT NODE
// IF NODE IS FOUND, MODIFY THE VALUE
// IF NODE IS NOT FOUND, CREATE THE NEW VALUE
return true;
}
// Return an arraylist containing all key/value pairs as such: "key:value"
public ArrayList GetAllValues()
{
ArrayList aryList = new ArrayList();
// Finish Me!
// foreach(key k in XmlDocument)
// aryList.Add((string)(key) + (string)(value));
return aryList;
}
public bool WriteFile()
{
try
{
XmlTextWriter xmw = new XmlTextWriter(m_Path);
// WRITE xmlDoc using xmw to m_Path
}
catch(Exception ex)
{
m_Err = ex.ToString();
return false;
}
return true;
}
}
As you can see, the things I don't know how to do are written as comments describing what needs to happen at that particular segement. If somoene can help me out with this, I'd be greatly appreciative. I've been pouring over my C# manuals and the projects online for about 8 hours or so, trying to find a straight forward way to do this, but haven't figured it out yet.
As always, thanks much.
|
|
|
|
|
budidharma wrote: public string GetValue(string key)
{
// ITERATE THROUGH KEYS, IF FOUND, RETURN THE VALUE, OTHERWISE, return "NULL";
return "NULL";
}
You can use the SelectNodes[^] or the SelectSingleNode[^] method on XmlDocument to do that. Give it an XPath expression and it will return you the appropriate nodes. In your case, it will probably look like
xmlDoc.SelectNodes("//nodeName[@key='keyValue'");
assuming the key value pairs are of the form <nodeName key="KeyValue value="Value>
budidharma wrote: public bool SetValue(string key, string value)
{
// ITERATE THROUGH THE VALUES, FIND THE CORRECT NODE
// IF NODE IS FOUND, MODIFY THE VALUE
// IF NODE IS NOT FOUND, CREATE THE NEW VALUE
return true;
}
To modify an existing node, get a reference to it using the above method. Then use the Attributes property and the indexer to get a reference to the attribute and then just set the Value[^] property to the desired value.
You can use the CreateNode[^] method to create a new node.
budidharma wrote: public ArrayList GetAllValues()
{
You can again use SelectNodes, or you can just do a foreach over XmlDocument.DocumentElement.ChildNodes, if it's always going to be the key-value pairs directly under the root.
budidharma wrote: // WRITE xmlDoc using xmw to m_Path
Use the WriteTo[^] method.
I guess you didn't look hard enough, all the details are available in MSDN
Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro
|
|
|
|
|
I would like to know how create a button that can scroll the content of a tabpage.
Thank you for your help
To be
|
|
|
|
|
Here we go. In your Form , you have the following somewhere:
private TabControl myTabControl;
private Button scrollLeftButton;
myTabControl.TabPages.Add( new TabPage( "A Tab Page" ) );
myTabControl.TabPages[ 0 ].AutoScroll = true;
scrollLeftButton = new Button();
scrollLeftButton.Text = "Scroll Left";
scrollLeftButton.Click += new System.EventHandler( scrollLeftButton_Click );
private void scrollLeftButton_Click(object sender, System.EventArgs e)
{
myTabControl.TabPages[ 0 ].AutoScrollPosition =
new System.Drawing.Point( 10 - myTabControl.TabPages[ 0 ].AutoScrollPosition.X,
mtc.TabPages[ 3 ].AutoScrollPosition.Y );
} And, that should do it. Just make other buttons to adjust up, down, and right scrolling.
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
Hello gurus,
I would like to know what is the C# equivalent of the LOBYTE and HIBYTE Win32 macros?
I wish to get the low byte and high byte from a C# ushort . How can I do that?
Thanks for your help.
Best regards.
There is no spoon.
|
|
|
|
|
Well the C++ macros are defined as follows:
<br />
#define MAKEWORD(a, b) ((WORD)(((BYTE)((DWORD_PTR)(a) & 0xff)) | ((WORD)((BYTE)((DWORD_PTR)(b) & 0xff))) << 8))<br />
#define MAKELONG(a, b) ((LONG)(((WORD)((DWORD_PTR)(a) & 0xffff)) | ((DWORD)((WORD)((DWORD_PTR)(b) & 0xffff))) << 16))<br />
#define LOWORD(l) ((WORD)((DWORD_PTR)(l) & 0xffff))<br />
#define HIWORD(l) ((WORD)((DWORD_PTR)(l) >> 16))<br />
#define LOBYTE(w) ((BYTE)((DWORD_PTR)(w) & 0xff))<br />
#define HIBYTE(w) ((BYTE)((DWORD_PTR)(w) >> 8))<br />
Looks ugly and complex but its not as hard as it looks. Most of the casting can be removed actually. C++ macros don't have parameter types so their casting to ensure all the integers are the correct size before performing the math on them.
I used the UInt32, UInt16 and Byte types to be explicit.
So I created the C# equivalent:
<br />
class ByteAccess<br />
{<br />
public static UInt32 MakeLong(UInt16 high, UInt16 low)<br />
{<br />
return ((UInt32)low & 0xFFFF) | (((UInt32)high & 0xFFFF) << 16);<br />
}<br />
public static UInt16 MakeWord(byte high, byte low)<br />
{<br />
return (UInt16)(((UInt32)low & 0xFF) | ((UInt32)high & 0xFF) << 8);<br />
}<br />
public static UInt16 LoWord(UInt32 nValue)<br />
{<br />
return (UInt16)(nValue & 0xFFFF);<br />
}<br />
public static UInt16 HiWord(UInt32 nValue)<br />
{<br />
return (UInt16)(nValue >> 16);<br />
}<br />
public static Byte LoByte(UInt16 nValue)<br />
{<br />
return (Byte)(nValue & 0xFF);<br />
}<br />
public static Byte HiByte(UInt16 nValue)<br />
{<br />
return (Byte)(nValue >> 8);<br />
}<br />
}<br />
Enjoy.
David
|
|
|
|
|
I got a question regarding Table Class and how to programmatically add a table.
First of all i build my Dynamic table in Page_Load() event in the Code Behind C# class as:
private void Page_Load(object sender, System.EventArgs e)
{
// Generate rows and cells.
Table Table1 = new Table();
int numrows = 3;
int numcells = 2;
for (int j=0; j<numrows; j++)=""
="" {="" tablerow="" r="new" tablerow();
="" for="" (int="" i="0;" i<numcells;="" i++)="" {
="" tablecell="" c="new" tablecell();
="" c.controls.add(new="" literalcontrol("row="" "="" +="" j.tostring()="" ",="" cell="" i.tostring()));
="" r.cells.add(c);
="" }
="" table1.rows.add(r);
="" }
="" }
next="" added="" the="" following="" line="" to="" html="" code:
<asp:table="" id="Table1" style="Z-INDEX: 106; LEFT: 50px; POSITION: absolute; TOP: 80px" runat="server"
font-size="X-Small" font-names="Century Gothic" font-bold="True" forecolor="Red">
When i run tha application, it doesn't give me any errors but it still don't show the generated table either?
What Am I doing Wrong???
|
|
|
|
|
|
tat's must it!!!!
i didn't
|
|
|
|
|
hi,
I want to create a tab control in my application using with xml.
each tab links to separate pages.
give me one example
baski
|
|
|
|