I'm trying to create a program wherein the users will provide their birthdate from multiple combo boxes. One for month, then date, and third is for year. And combine their values into one string. So far, here's my code:
string bdate = " "+ bday_month.SelectedText + " " + bday_date.SelectedText + ", " + bday_year.SelectedText;
MessageBox.Show(bdate);
The problem with this is that when I have values for all combo boxes, bdate only displays the third combo box value. See below:
Any idea?
Use the ComboBox.Text property instead of the ComboBox.SelectedText:
string bdate = " " + bday_month.Text + " " + bday_date.Text + ", " + bday_year.Text;
MessageBox.Show(bdate);
The ComboBox.SelectedText property returns the text highlighted in the editor, but not the entire text.
comboBox.SelectedText
is a value indicating the currently selected text in the control and
comboBox.Text
is the current text in the ComboBox
so use .Text instead
Related
I have a situation and need some help to solve this.
enter image description here
I am not allowed to embed images directly to the post. As you can see the image, Data is displayed in the gridview, but client wants it to displayed as text one below another like text below the gridivew. However, there is a requirement to display Filenames together which have common date. E.g : 04/18/2015, there are two different file entries and want PAYROLL_******20150429.csv to be displayed under PAYROLL*****_20150422.csv. So no need for third entry, just want 2nd and 3rd to be clubbed together. There could be different files with same date entry, and they need to be clubbed together. The first Name, 191_20150418 is based on the date entry.
I am using Win forms with C#. I have gathered the data in the data table to display in the gridview.
This is my code below to display in the Label:
Label.Text += Environment.NewLine + "Name : " + dtSWAction.Rows[i]["Name"] + Environment.NewLine +
"PPED : " + dtSWAction.Rows[i]["Date"] + Environment.NewLine +
"Files : " + dtSWAction.Rows[i]["Files"] + Environment.NewLine;
Please suggest a better way if adding like this in label is not feasible.
There really isn't anything wrong with doing this:
Label.Text +=
Environment.NewLine + "Name : " + dtSWAction.Rows[i]["Name"] +
Environment.NewLine + "PPED : " + dtSWAction.Rows[i]["Date"] +
Environment.NewLine + "Files : " + dtSWAction.Rows[i]["Files"] +
Environment.NewLine;
However you could use string.Format():
Label.Text += string.Format("{0}Name : {1}{0}PPED : {2}{0}Files : {3}{0}",
Environment.NewLine,
dtSWAction.Rows[i]["Name"],
dtSWAction.Rows[i]["Date"],
dtSWAction.Rows[i]["Files"]);
Or string interpolation if you are using C# 6/.NET 4.6:
Label.Text += $"{Environment.NewLine}Name : {dtSWAction.Rows[i]["Name"]}{Environment.NewLine}PPED : {dtSWAction.Rows[i]["Date"]}{Environment.NewLine}Files : {dtSWAction.Rows[i]["Files"]}{Environment.NewLine}";
It looks like you are using a DataTable for accessing this data, but in the future if you are using a custom defined class you could override the ToString() method of your class using any one of the above methods:
public override string ToString()
{
return string.Format("{0}Name : {1}{0}PPED : {2}{0}Files : {3}{0}",
Environment.NewLine,
dtSWAction.Rows[i]["Name"],
dtSWAction.Rows[i]["Date"],
dtSWAction.Rows[i]["Files"]);
}
Then anytime in the future you could just call it like this:
Label.Text += instanceOfYourClass.ToString();
I'm creating a standard calculator with a history feature. Previous solutions will show in a label box every time the user clicks the "=" button
After converting this string:
string histo = (operand1 + " " + operation + " " + " " + operand2 + " = " + result);
To a list by using this code:
List<string> hist = histo.Split().ToList()
What I want to do next is to print it to a label box to show history. How can I do that? Thank you.
Your question is not very clear and I do not think that label is a good choice to show the history. Combobox would be a better option, if you want to allow users to select items from history.
As far as your question on how to display it in a label is concerned you can use the following code. You can replace "," with Environment.NewLine.
lbl.Text = String.Join(",", hist);
I am having an issue with this blank space " " showing up in my textbox if the table row is null. so i would like to replace this " " with this "". For example here is what i have:
StartTime.Text = row.Cells[5].Text;
so here is pseudo-code of what i am trying to achieve:
if (StartTime.Text == "" " then replace it with " ")
else show the value of StartTime.Text
I know my C# skills is so bad so please help. thanks
what about StartTime.Text = row.Cells[5].Text.Replace(" ", " ")
or i haven't understood the question correctly
I am trying to display 3 values in a message box when the user clicks Submit as a means to verify correct input.
The 3 values come from comboBoxes in the form: age, height, weight.
In my current set up, the box will only say "age:" with the actual numerical value in the top border.
How can I get the 3 comboBox data items to appear inside a message box with appropriate titles?
Like this:
Age: 27
Height: 62
weight: 180
Data are stored in the variables age_Num.Text, height_Num.Text, and weight_Num.Text
MessageBox.Show("Age:", age_Num.Text); //just shows "Age:". Value is in titlebar of mb
In case of a ComboBox you can get the selected text through the ComboBox.SelectedText property.
To build a string from multiple values you can use String.Format().
string age = age_num.SelectedText;
string height = height_Num.SelectedText;
string weight = weight_Num.SelectedText
string text = String.Format(
"Age: {0}, Height: {1}, Weight: {2}", age, height, weight);
MessageBox.Show(text);
You must concatenate those values into a single string. Try this, using StringBuilder:
StringBuilder MessageText = new StringBuilder();
MessageText.AppendLine(string.Format("Age: {0}", age_Num.Text));
MessageText.AppendLine(string.Format("Height: {0}", height_Num.Text));
MessageText.AppendLine(string.Format("Weight: {0}", weight_Num.Text));
MessageBox.Show(MessageText.ToString());
You can use values of combo directly,
MessageBox.Show("Sometext 1:" + cbo1.SelectedValue.ToString() + " Sometext 2:" + cbo2.SelectedValue.ToString() + " Sometext 3:" + cbo3.SelectedValue.ToString());
or as you already have in variables.
MessageBox.Show("Age: " + age_num.Text + " Height: " + age_num.Text + " Sometext 3: " + weight_Num.Text);
If you don't like any of the above, simply create a dialog form set up as you want, with static function to show it modally.
I need a statement to select data from an MS Access database table.
WITHIN selected dates
I have two textboxes in my GUI called StartDate and EndDate
I want to select data within those 2 dates.
I have tried 2 methods.
The first is
" DAY(V.RegDate) between " + Start.ToString("dd")
+ " and " + End.ToString("dd");
" and MONTH(V.RegDate) between " + Start.ToString("MM") + " and "
+ End.ToString("MM");
" and YEAR(V.RegDate) between " + Start.ToString("yyyy") + " and "
+ End.ToString("yyyy");
V.RegDate is the date column from the database.
But it returns me no data when I select 01/08/2010 and 01/09/2010 while there is some data at 25/08/2010.
I think that is because I chose the date separately and since the 2 dates are the same,
returns me nothing
I have tried another way...
" V.RegDate between #" + Start.ToString("dd/MM/yyyy") + "# and #" _
+ End.ToString("dd/MM/yyyy") + "#";
This also return me nothing
Any Ideas????
This pattern works for me:
SELECT sometable.somedate
FROM sometable
WHERE (((sometable.somedate) Between #2/1/2010# And #4/1/2010#));
(in this case it's MDY, I normally prefer ISO-ish style - YYYY/MM/DD because there is no way that Access can screw that up)
Maybe you've got your date set using the American default - MDY instead of the British(?) standard DMY.