|
Use String.Format().
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
I have a panel with scrollbars, and now I want to be able to control the scrollbars with up-down-right-left and pgdown/pgup keys. So I've added key listeners, like below:
.
.
else if(ke.KeyCode == Keys.Left)
{
AutoScrollPosition = new Point(this.AutoScrollPosition.X + 10, 0)
}
else if(ke.KeyCode == Keys.Right)
{
AutoScrollPosition = new Point(this.AutoScrollPosition.X - 10, 0)
}
.
.
now the first time i press the 'right' key it will jump 10, but when I press the same key again,
the position will be reset!!!! why why why.
I check the autoscroll position before i press 'Right' and it's 0. afterwards it's -10.
the before i press it again it's -10 also. But when i press it yet again, it's back to 0 ...??
can anyone please tell my why (-10) - (10) equals 0 ??
|
|
|
|
|
Have you tried this?
else if(ke.KeyCode == Keys.Left)<br />
{<br />
AutoScrollPosition.X += 10;<br />
}<br />
else if(ke.KeyCode == Keys.Right)<br />
{<br />
AutoScrollPosition.X -= 10)<br />
}
|
|
|
|
|
Does anyone know how i can use the GetCursorInfo API in C#? I'd like to know the dllimport and an example code.
Click here to view the GetCursorInfo API.
|
|
|
|
|
You have to recreate the sturct and ref that in your P/Invoked statement:
public struct CursorInfo
{
public int size;
public int flags;
IntPtr cursor;
System.Drawing.Point point;
}
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern bool GetCursorInfo(ref CursorInfo info);
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thanks for the quick reply but i'd like to know how to use it too. I'd like to know how to get the cursor image (or icon) using GetCursorInfo.
|
|
|
|
|
It's all in the documentation on MSDN. I suggest you read it. There's examples, too. Always consult the docs for questions like this, when asking "what is X" or "how does Y" work, otherwise you're just grabbing at straws and guessing, which doesn't build any skills. I don't mean to sound rude, but researching is half the development battle, and these questions are exactly what the Windows API functions, .NET class library, and other documentation is for. Besides, it'd be a waste of time to essentially copy and paste the topic you want. Just type GetCursorInfo in the index and you've got what you want. As far as managed code goes, however, read on...
If all you're trying to do is read-in a cursor image, then you need to look at the Cursor class. It's better to use managed code than to start needlessly P/Invoking stuff from the Win32 API. It has everything you apparently need to read-in a cursor and display it (either as the mouse cursor or a bitmap (with some fiddling) such as a preview pic).
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I'm aware of that. The thing is, i don't understand how to use GetCursorInfo. I just need some help in getting the cursor image which is vital for my program, that's all. Anyway, i'll try reading again.
|
|
|
|
|
If all you want is the cursor image to draw on a surface or something, the Cursor class documentation provides all the information you need - no P/Invoke is necessary. As I said, use managed code whenever possible.
See Cursor.Current and Cursor.Draw() :
Cursor c = Cursor.Current;
Graphics g = this.myControl.CreateGraphics();
c.Draw(g, this.myControl.Bounds);
g.Dispose();
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
No, i just need to know the name of the cursor whether it is a normal cursor showing or wait cursor showing - system-wide (not just within the current form). From my understanding, if you use Cursor.Current, you can only get the name of the cursor that is showing within the form. If the cursor moves outside of the form, the name of the cursor will not be changed.
|
|
|
|
|
As I stated before, recreate the structure and P/Invoke the interface:
[StructLayout(LayoutKind.Sequential)]
public struct CursorInfo
{
public int Size;
public int Flags;
public IntPtr Handle;
public Point Position;
}
public class NativeMethods
{
[DllImport("user32.dll")]
public static extern bool GetCursorInfo(out CursorInfo info);
}
Based on this code and the docs for GetCursorInfo, calling it should be no problem:
CursorInfo info = new CursorInfo();
info.Size = Marshal.SizeOf(info.GetType());
if (NativeMethods.GetCursorInfo(out info))
{
Cursor c = new Cursor(info.Handle);
}
I've tested this and it does work fine.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Thanks a lot. That did the trick.
|
|
|
|
|
Let assume I have a datagrid that contains the following data
ID VAL1 VAL2 VAL3
1 abc def ghi
2 jkl mno pqr
3 stu vwx yz1
4 234 567 890
On the same form I also have a list box with the following values :
2
4
Using the 'SelectIndexChanged" event I want to highlight the relevant ID in the data grid. So lets say I click on '4' the following line in the datagrid would be selected :
4 234 567 890
|
|
|
|
|
I think you can use the below function of datagrid
DataGrid.Select(rowIndex)
I'm not sure about how you'd get the rowindex of that particular id.
- Kannan
|
|
|
|
|
Thats about as far as I got with it.
I just couldnt find a way to get the rowIndex for the selected value in the listbox
|
|
|
|
|
There's many ways. If your DataGrid is not sorted and hasn't been sorted, you can use a myriad of method to get the index of the row in the data source, then use that in DataGrid.Select() . If you knew the index of the column for the key, you could use the DataGrid.Item and loop through rows (using for instead of foreach so you can get the index from, say, i 's current value when you find what you're looking for). It's all in the documentation. Just spend a little time researching what's available before jumping into it. You can use members on DataGrid , the DataSet , a DataRow or DataRowView , or even use a strongly-typed DataSet which provides you with additional options.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Well after some playing I came up with this piece of rather convoluted code, but it does what I need. Here it is just incase anybody else is looking for something similar in the future :
CurrencyManager cm = (CurrencyManager)dgData.BindingContext[dgData.DataSource];
for (int i = 0; i < cm.Count; i++)
{
DataRowView drv = (DataRowView)cm.List[i];
if (item == drv.Row["CDBNO"].ToString())
{
dgData.CurrentRowIndex = i;
dgData.Select(i);
}
}
|
|
|
|
|
A little tip: don't use the DataRow.Item[String] syntax - it performs many lookups and doesn't cache squat! Each time you use it (so in your case, with each loop) it must enumerate all the columns, do case-insensitive string comparisons with each column till it finds the right one, get the index and then does some stuff with column versions before giving you what you want. I suggest you get the column index or DataColumn upfront before starting your loop and using that index or DataColumn instance instead of a string. If done throughout your code, you most likely will notice a performance increase.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I have a COM object written in C++. I want to use it from C#.
For getting values out of the COM object I used functions like:
HRESULT myfunction (/*[out]*/ BSTR* stringvalue, /*[out]*/ int* intvalue);
How can I use these functions from c#?
-Alma-
|
|
|
|
|
After you add a reference to your COM dll you will see the IntelliSence info for its COM objects. Also, you could just go to the Object Browser and check how the syntax looks like.
Your function will probably translate into this:
void myfunction(ref string stringvalue, ref int intvalue);
Alexandre Kojevnikov
MCAD charter member
Leuven, Belgium
|
|
|
|
|
In the object browser my functions syntax looks this way:
HRESULT _stdcall MyGetValue([out] int* value);
IntelliSence info looks like this:
void MyGetValue(out int value);
whatever I put as a parameter it results in an error:
The best overloaded method match for 'MYComObjectDllLib.IMyObject.MyFunction(out int)' has some invalid arguments
-Alma-
|
|
|
|
|
The calling code should look like this:
int myValue;
myCOMObject.MyGetValue(out myValue);
Alexandre Kojevnikov
MCAD charter member
Leuven, Belgium
|
|
|
|
|
Yes, that works! Thanks a lot!
-Alma-
|
|
|
|
|
Yello, I'm suffering from user stupidity at the moment and am hoping someone here can show me the way.
I'm attempting to build a class that contains a collection. I have a method in the class called Bind(), which takes a textbox reference and an object reference as it's parameters. These two passed references are then stored into the collection.
Simple enough I thought, but the problem appears to be, that I cannot make a reference to a reference. The objects contained within the collection no longer reference the original objects. As as an example, I should be able to pull out one of the textboxes from the collection, change the .text property and see the textbox change on the form. Alas, this is not the case.
I have tried just assigning the references to two member variables within the class instead of a collection. Same problem, once the second assigment occurs, I get a copy of the textbox and object and not another reference.
Is there something blindingly simple that I'm missing here? Surely in .NET you can reference a reference? Or is there some default behaviour that I don't understand that forces an object to copy itself in this situation.
Thanks.
Martin
|
|
|
|
|
It's hard to help with a code sample, but here's some code from a quick form I threw together which I hope shows how to do what you want.
using System;<br />
using System.Drawing;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Windows.Forms;<br />
using System.Data;<br />
<br />
namespace ReferenceAReference<br />
{<br />
public class Form1 : System.Windows.Forms.Form<br />
{<br />
private MyCustomObjectCollection myCustomObjectCollection;<br />
private System.Windows.Forms.Button button1;<br />
private System.Windows.Forms.TextBox textBox1;<br />
<br />
private System.ComponentModel.Container components = null;<br />
<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
this.myCustomObjectCollection = new MyCustomObjectCollection();<br />
<br />
<br />
for(int x=0; x < 3; x++)<br />
{<br />
for(int y=0; y < 7; y++)<br />
{<br />
TextBox tb = new TextBox();<br />
tb.Location = new Point(8 + (x*108), 8 + (y*24));<br />
this.Controls.Add(tb);<br />
this.myCustomObjectCollection.Add(new MyCustomObject(tb, null));<br />
}<br />
}<br />
}<br />
<br />
protected override void Dispose( bool disposing )<br />
{<br />
if( disposing )<br />
{<br />
if (components != null) <br />
{<br />
components.Dispose();<br />
}<br />
}<br />
base.Dispose( disposing );<br />
}<br />
<br />
#region Windows Form Designer generated code<br />
private void InitializeComponent()<br />
{<br />
this.button1 = new System.Windows.Forms.Button();<br />
this.textBox1 = new System.Windows.Forms.TextBox();<br />
this.SuspendLayout();<br />
this.button1.Location = new System.Drawing.Point(112, 240);<br />
this.button1.Name = "button1";<br />
this.button1.Size = new System.Drawing.Size(112, 23);<br />
this.button1.TabIndex = 0;<br />
this.button1.Text = "Fill TextBoxes";<br />
this.button1.Click += new System.EventHandler(this.button1_Click);<br />
this.textBox1.Location = new System.Drawing.Point(8, 240);<br />
this.textBox1.Name = "textBox1";<br />
this.textBox1.TabIndex = 1;<br />
this.textBox1.Text = "Test String";<br />
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);<br />
this.ClientSize = new System.Drawing.Size(464, 266);<br />
this.Controls.AddRange(new System.Windows.Forms.Control[] {<br />
this.textBox1,<br />
this.button1});<br />
this.Name = "Form1";<br />
this.Text = "Form1";<br />
this.ResumeLayout(false);<br />
<br />
}<br />
#endregion<br />
<br />
[STAThread]<br />
static void Main() <br />
{<br />
Application.Run(new Form1());<br />
}<br />
<br />
private void button1_Click(object sender, System.EventArgs e)<br />
{<br />
foreach(MyCustomObject o in this.myCustomObjectCollection)<br />
{<br />
o.TextBox.Text = this.textBox1.Text;<br />
}<br />
}<br />
}<br />
<br />
public class MyCustomObject<br />
{<br />
private System.Windows.Forms.TextBox textBox;<br />
private object someObject;<br />
<br />
public System.Windows.Forms.TextBox TextBox<br />
{<br />
get { return this.textBox; }<br />
set { this.textBox = value; }<br />
}<br />
<br />
public object SomeObject<br />
{<br />
get { return this.someObject; }<br />
set { this.someObject = value; }<br />
}<br />
<br />
public MyCustomObject(System.Windows.Forms.TextBox textBox, object someObject)<br />
{<br />
this.textBox = textBox;<br />
this.someObject = someObject;<br />
}<br />
}<br />
<br />
<br />
public class MyCustomObjectCollection : CollectionBase<br />
{<br />
public MyCustomObject Add(MyCustomObject value)<br />
{<br />
base.List.Add(value as object);<br />
return value;<br />
}<br />
<br />
public void AddRange(MyCustomObject[] values)<br />
{<br />
foreach(MyCustomObject item in values)<br />
Add(item);<br />
}<br />
<br />
public void Remove(MyCustomObject value)<br />
{<br />
base.List.Remove(value as object);<br />
}<br />
<br />
public void Insert(int index, MyCustomObject value)<br />
{<br />
base.List.Insert(index, value as object);<br />
}<br />
<br />
public bool Contains(MyCustomObject value)<br />
{<br />
foreach(object o in base.List)<br />
if (value.Equals(o))<br />
return true;<br />
return false;<br />
}<br />
<br />
public bool Contains(MyCustomObjectCollection values)<br />
{<br />
foreach(MyCustomObject t in values)<br />
{<br />
if (Contains(t))<br />
return true;<br />
}<br />
<br />
return false;<br />
}<br />
<br />
public MyCustomObject this[int index]<br />
{<br />
get { return (base.List[index] as MyCustomObject); }<br />
set { base.List[index] = value; }<br />
}<br />
<br />
public int IndexOf(MyCustomObject value)<br />
{<br />
return base.List.IndexOf(value);<br />
}<br />
}<br />
}
|
|
|
|