|
Bind data to it, or bind it's properties to something else? Please be specific, as this is a rather ambiguous question.
Both are possible, but rather than go into a length discussion of both, please be specific. Also take a look at the Control.DataBindings property in the .NET Framework SDK that gets installed by default with VS.NET.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for the response. I has more of an explaination here but didn't receive any replies.
I have created a UserControl which contains a ComboBox. This combo box if filled with data in the controls constructor. I created a public property to expose the ComboBox.SelectedValue property so I may bind to it from forms which use the control. The bindings never work. The debugger doesn't compain at all, and the values in the UserControl's ComboBox never changes with the CurrencyManager.Position changes.
|
|
|
|
|
Using the DataBindings property like so doesn't work?:
myUserControl1.DataBindings.Add("SelectedValue", dataSet1, "DataTable1");
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Correct, that binding will not work, hence my dilemma.
Neither of these work:
myUserControl1.DataBindings.Add("SelectedValue", dataSet1, "DataTable1");
myUserControl1.DataBindings.Add( new System.Windows.Forms.Bindings("SelectedValue", dataSet1, "DataTable1"));
Note, setting a value directly to the UserControl property does work.
Thanks for your assistence,
Brett
|
|
|
|
|
Both of those overloads to the same thing. The first actually uses the params to construct a Binding instance and then calls the second overload.
The thing about binding contexts is that they must be bound exactly the same. So, if you bind a ComboBox to a DataTable , then the Binding must also be bound to a DataTable .
I threw together a quick example. The ComboBox.DataSource is bound to a DataTable (in a DataSet ). The DisplayMember and ValueMember specify column names (say, "ID" and "Name").
I also defined a property (SelectedValue ) that reflects ComboBox.SelectedValue and created a binding in my ctor like so:
this.DataBindings.Add("SelectedValue", dataSet1, "Table1.ID"); The really important part is how you get the binding context. For the back and previous buttons, I threw together this:
private void button1_Click(object sender, System.EventArgs e)
{
CurrencyManager cm = (CurrencyManager)this.BindingContext
[this.dataSet1.Table1];
if (cm != null)
if (cm.Position >= 1) cm.Position--;
}
private void button2_Click(object sender, System.EventArgs e)
{
CurrencyManager cm = (CurrencyManager)this.BindingContext
[this.dataSet1.Table1];
if (cm != null)
if (cm.Position < cm.Count - 1) cm.Position++;
} Notice that the BindingContext is the table itself - the same as was bound to the ComboBox .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you. Before I dive into changing some things around, I have a question on the BindingContext. There are 3 TextBoxes and 2 UserControls which are bound to the same DataSet.Table (fyi, my dataset is typed). Would the way I get the BindingContext effect the binding between these three different controls? In other words, the binding is working fine on the the 3 TextBoxes but is not working on the 2 UserControls. If I:
<br />
string o="";<br />
BindingManagerBase bindingManager = this.BindingContext[DataSet, "table"];<br />
foreach(Binding b in bindingManager.Bindings)<br />
{<br />
o += b.Control.ToString() + "\n";<br />
}<br />
MessageBox.Show(o);<br />
The TextBox binding show, but the UserControls do not. And obviously, the Text boxes are bound and change with .Position property. Is there anything other that a set and get required on the UserControl for the binding to work?
|
|
|
|
|
Again, take a look at the BindingContext property documentation in the .NET Framework SDK. It's ncessary that you bind properties to the same data source as the BindingContext .
For example, many people will bind a DataTable to a DataGrid (assign it to DataSource ) instead of setting the DataSource property to the DataSet , and the table name (typed or not) as the DataMember name. If you then use specify a context like BindingContext[dataSet1.dataTable1] , it will not work. A good way is to use BindingContext[dataGrid1.DataSource, dataGrid1.DataMember] . This will make sure that the binding contexts are the same and correct.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am sorry, I don't see how this answers my question. The combobox bindings are being handled in the control. It is the 'simple data binding' to the UserControl's property I can't get to work. I don't think I would pull a BindingContext from [UserControl.myProperty] would I? I don't think I would get a useful binding manager that way. Thank you very much for helping me through this.
|
|
|
|
|
As I said in my previous reply[^], the binding contexts have to be exactly the same object with exactly the same data member (like a table name or collection property name or something). You can't bind to a DataTable in a DataSet in one case when the other binding is to the DataSet itself. I wrote a quick example and it works fine. If you reply to this via email (use the email link) I can grab your email address and send it to you to examine the source (you won't be able to run it since the connection string and command are specific to our database here, but you can always change it).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I tried someting new. I created a new UserControl, but instead of inheriting from System.Windows.Forms.UserControl I inherited from System.Windows.Forms.ComboBox (isn't this now a customcontrol??) Anyway this new user control binds without any problems. I bind to lp2.SelectedValue
Here are the basics.
<br />
public class lp2 : System.Windows.Forms.ComboBox<br />
{<br />
private SqlDataAdapter dataAdapter;<br />
private SqlConnection sqlConnection;<br />
private DataSet dataset;<br />
private System.ComponentModel.Container components = null;<br />
<br />
public lp2()<br />
{<br />
InitializeComponent();<br />
sqlConnection = Utilities.dbUtilities.getWebDbCn();<br />
string selectCommand = "select * from lang";<br />
dataset = new DataSet();<br />
dataAdapter = new SqlDataAdapter(selectCommand, sqlConnection);<br />
dataAdapter.Fill(dataset, "lang");<br />
this.DataSource = dataset;<br />
this.DisplayMember = "lang.langText";<br />
this.ValueMember = "lang.lang";<br />
<br />
}<br />
}<br />
My other UserControl is simular except I have a ComboBox added to the class where this class inherits ComboBox.
Now I guess I need to figure out how to get the older UserControl to work since I will be back to the issue when I have a UserControl with more than one control or component on it.
Are there things I must do in a UserControl to make sure I can bind to it's properties?
|
|
|
|
|
I am an OOP newbie and had a question. I made my example more generic so sorry if it turned out cheesy. Basically, I have three classes in my hierarchy: Bicycle contains a set of Wheels. Wheels contain a set of Spokes. Spokes are just there. Each parent class has at least one child, i.e. Bicycle has at least one wheel, which has at least one spoke. Below is how I implemented it and wanted to get some critique. Should I have used Arrays instead of ArrayLists. Any comments are greatly appreciated.
namespace MyTestNamespace
{
using System;
using System.Collections;
public class Spoke {
public Spoke() { ; }
}
public class Wheel {
private ArrayList spokes;
public Wheel() {
spokes = new ArrayList();
}
public void AddSpoke(Spoke spoke) {
spokes.Add(spoke);
}
public void RemoveSpoke(int index) {
spokes.RemoveAt(index);
}
public ArrayList GetSpokes() {
return spokes;
}
}
public class Bicycle {
private ArrayList wheels;
public Bicycle() {
wheels = new ArrayList();
}
}
}
|
|
|
|
|
This is more of an implementation issue than a OO issue.
To get your implementation to use OO better you should use typed collections. At present C# does not support Generics (but it will in V2.0) so you will have to manually create a typed collection. You can google for some automated tools that will build typed collections.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
Thanks for the pointer, man! Typed collections seems to be what I needed.
|
|
|
|
|
You can also extend CollectionBase and override the methods and property that would normally return object . This is possible because those methods - required because the interface is implemented - are implemented with explicit interface declarations. As Colin said, there's tools available to help you, but you should at least understand what is going on with the source they generate.
If you don't extend CollectionBase or another base class, you have to implement all the interfaces required for your code yourself.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
dear all,
I have some buttons in a toolbar.
on all these buttons, there are some icons.
I want to change the icons on the buttons of my toolbar to another when the mouse is over it, so i use the toolbar_mousehover event to detect it...
However, I don't know how to get the toolbar button value that the mouse is currently hovering it from the arguments.....any ideas?
thanx a lot!
|
|
|
|
|
You could try to cast the sender as a ToolBarButton .
private void Toolbar_Hover(object sender, EventArgs e)
{
ToolBarButton b = sender as ToolBarButton;
if(b != null)
}
- Nick Parker My Blog | My Articles
|
|
|
|
|
This would only give you the ToolBar , actually, since it's the Control raising the event. You have to perform a hit test (which I briefly showed below). Basically, the ToolBar in the .NET BCL sucks. Thank goodness for WinBars in .NET 2.0!
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
That's what I get for not having .NET installed here at work. I just ordered a copy of Visual Studio .NET 2003 last Friday for my next project (conversion from Java to .NET). Thanks for clairfying.
- Nick Parker My Blog | My Articles
|
|
|
|
|
You need to perform a hit test. For some reason, Microsoft didn't define this on the ToolBar class. You do this by getting the MousePosition , transforming those screen coordinates to the ToolBar 's client coordinates, then P/Invoke native APIs:
public class MyToolBar : ToolBar
{
public ToolBarButton HitTest(Point p)
{
IntPtr retval = SendMessage(Handle, TB_HITTEST, IntPtr.Zero, p);
int index = retval.ToInt32();
if (index >= 0)
{
foreach (ToolBarButton button in Buttons)
if (button.Style != ToolBarButtonStyle.Separator)
if (index-- == 0) return button;
}
return null;
}
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint msg,
IntPtr wParam, ref Point p);
private const int TB_HITTEST = 0x0445;
} To use it:
myToolBar1.MouseHover += new EventHandler(myToolBar1_MouseHover);
private void myToolBar1_MouseHover(object sender, EventArgs e)
{
Point p = Control.MousePosition;
p = myToolBar1.PointToClient(p);
ToolBarButton button = myToolBar1.HitTest(p);
if (button != null) MessageBox.Show("You're over the " + button.Text +
" button.");
} This is untested, but should give you some idea of what to do.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi everyone
Does anyone know of a decent (and free! ) C# refactoring plugin for VS.net?
I had a look at "Resharper". The refactoring capabilites were good, but I didn't go for the auto complete drop downs much. Also it seemed to slow down VS.net a lot, and the uninstall left me unable to add a new class or project. (There was a note on their site about this, but it left me having to do a complete VS repair)
I had a look around on google, but didn't see anything.
Has anyone else tried ReSharper? Any thoughts?
Best Wishes
Steven
let the stormy clouds chase everyone from the place
|
|
|
|
|
which refactoring methods do you need?
VS2005 will have quite a few (but not as many and not as nice as Eclipse for Java).
You'll be able to download Beta in few days.
|
|
|
|
|
ahh yes well - the ones that eclipse has for java is exactly what I was thinking of
|
|
|
|
|
The one that I wanted the most from eclipse was to generate getters/setters.
That's why I wrote my own VS add-on for that:
http://www.adersoftware.com/index.cfm?page=vsPropertyGenerator
|
|
|
|
|
cool thanks - i'll look into it
|
|
|
|
|
My problem is this. I want to download a file from the webbserver and save it to a local client. And i the work flow is like this. After the user has register his customer data, he reicive an email with a link to the file on the server and then the dowload starts. I don´t have any idea of how to do this
Could anyone give me some idea´s or example of how to do this or some other way to do this
Sorry for my poor english
|
|
|
|