Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi All

 

I have a form where the textbox (txtID) text is bound to a dataset. When running the app, the appropriate data appears in the textbox. I wish to evaluate the text property to set one of three radio buttons to "checked"

 

Here's the code:

 

If Me.txtID.Text = "Book" Then

RadioButton(0).checked = True

Elseif Me.txtID.Text = "Tax" Then

RadioButton(1).checked = True

ElseIf Me.txtID.Text = "Both" Then

RadioButton(2).checked = True

EndIf

 

When in Break Mode, the value of txtID.Text = "", even though the textbox shows the proper Text on the form.

 

Any ideas?

 

Thanks

Posted

It would be useful to see some of your code, but here's an alternative. This is extra work but it should work.

 

You say the textbox is bound to a dataset, try using a dataview and when you rowfilter the dataview, check the value of the field in the dataview you want to appear in the textbox and set the radio button accordingly.

 

Dim dv as new Dataview

dv.Table = dataset.Tables("Tablename")
dv.rowfilter = "Filter criteria"
textbox.text = dv(0)("Fieldname")
if dv(0)("Fieldname") = "Radio1" then
  Radio1.checked = true
else
 Radio2.checked = true
endif

  • *Experts*
Posted

I wouldn't use the Textbox at all. Use the DataSet to drive the Radio buttons.

 

If you need to update the radio buttons as the value in the dataset changes, then use the columnchanged event. Try something like this (just in C#, sorry):

// In Form_Load or similar, setup the event. In VB I think
// you put " Handles ... ColumnChanged" or something after a function
ds.Tables[0].ColumnChanged += new DataColumnChangeEventHandler(Form1_ColumnChanged);

// Somewhere else in the class, define the event handler:
private void Form1_ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
string fullColumnName = e.Column.Table.TableName.ToLower() + "." + e.Column.ColumnName.ToLower();

switch(fullColumnName)
{
	case "table1.column1":
		if(e.ProposedValue=="Book")
		{
			RadioButton[0].Checked = true;
		}
		else if(e.ProposedValue=="Tax")
		{
			RadioButton[1].Checked = true;
		}
		else // both
		{
			RadioButton[2].Checked = true;
		}
		break;
}
}

 

Of course, I hate seeing hard-coded values like 0, 1, and 2 like that. It would be much more readable to use an Enum for the 3 values.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
I wouldn't use the Textbox at all. Use the DataSet to drive the Radio buttons.

 

If you need to update the radio buttons as the value in the dataset changes, then use the columnchanged event. Try something like this (just in C#, sorry):

// In Form_Load or similar, setup the event. In VB I think
// you put " Handles ... ColumnChanged" or something after a function
ds.Tables[0].ColumnChanged += new DataColumnChangeEventHandler(Form1_ColumnChanged);

// Somewhere else in the class, define the event handler:
private void Form1_ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
string fullColumnName = e.Column.Table.TableName.ToLower() + "." + e.Column.ColumnName.ToLower();

switch(fullColumnName)
{
	case "table1.column1":
		if(e.ProposedValue=="Book")
		{
			RadioButton[0].Checked = true;
		}
		else if(e.ProposedValue=="Tax")
		{
			RadioButton[1].Checked = true;
		}
		else // both
		{
			RadioButton[2].Checked = true;
		}
		break;
}
}

 

Of course, I hate seeing hard-coded values like 0, 1, and 2 like that. It would be much more readable to use an Enum for the 3 values.

 

-Nerseus

 

 

That was my original intention. I wanted to evaluate the field in the dataset, and set a checked condition for the appropriate radio button based on one of three choices. Unfortunately I'm still struggling on making the transition from VB6 (where this was not a problem). I thought about grabbing the value of the field, throwing it in a textbox, reading the value, then setting the approriate condition. I'm just surprised that a fairly straightfoward variable assignment is not being picked up in the conditional statement, even though it appears when the app is running. It seems that the bound control is not allowing (for lack of a better term) "external access" to its assigned value. Is there some additional line of code that's missing here?

 

The variable that is being evaluated is a string in the database. Looks like I need to go back and research datasets a little more - especially on reading a specific field.

 

You have shoved me a little closer to where I want to go, though.

 

Thanks

  • *Experts*
Posted

Whoops - forgot to answer (ask) about your first issue: the Text property not showing a value.

You said in Break mode the txtID.Text property showed empty string but when the form loads up you see the value (because it's bound)? When did you set the breakpoint? Make sure it's after the binding (d'oh!) and after the DataSet is filled. If that's what you were doing then I'm not sure why the Textbox wouldn't have a value in its Text property.

 

Certain controls, such as the DateTimePicker, have a Text property but you usually use a different one, such as the Value property. But on a Textbox all you need is the Text property.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
Whoops - forgot to answer (ask) about your first issue: the Text property not showing a value.

You said in Break mode the txtID.Text property showed empty string but when the form loads up you see the value (because it's bound)? When did you set the breakpoint? Make sure it's after the binding (d'oh!) and after the DataSet is filled. If that's what you were doing then I'm not sure why the Textbox wouldn't have a value in its Text property.

 

Certain controls, such as the DateTimePicker, have a Text property but you usually use a different one, such as the Value property. But on a Textbox all you need is the Text property.

 

-Nerseus

 

Might just have to call it a mystery of the sea...(coming from an accountant/part-time coder no less). The breakpoint was set after the fill and the conditional statements.

 

I went back and wrote an if statement such that

 

If ds.table(0).column = "book" then

radiobutton(0).checked = true

ElseIf ds.table(0).column = "tax" then

radiobutton(1).checked = true

ElseIf ds.table(0).column = "both" then

radiobutton(2).checked = true

EndIf

 

Works just fine now. Thanks for your help and ideas.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...