C# Combox.text string always shows empty string within Timer tick function - c#

I have an application where combox text get populated with data, I have two timer function to read the combox text content, one timer works fine, but the other time always shows combox content is always empty string. I am wondering where things are wrong
private void AddNewDataToSystemButton_Click(object sender, EventArgs e)
{
if (tbAddTrainTrainID.Text != "" && tbAddTrainRegionID.Text != "" && tbAddTrainSegmentID.Text != "")
{
bool isTrainAdded = CommonStore.TrainSimMainDatabase.AddNewTrainInToSystem(Convert.ToInt32(tbAddTrainTrainID.Text),
Convert.ToByte(tbAddTrainRegionID.Text), Convert.ToByte(tbAddTrainSegmentID.Text));
CommonStore.TrainSimMainDatabase.InitializingTrainCollection[Convert.ToInt32(tbAddTrainTrainID.Text)].VehicleList.IsManualTrain = false;
if (isTrainAdded)
{
cbTrainList.Items.Add(tbAddTrainTrainID.Text);
cbRemoveTrainID.Items.Add(tbAddTrainTrainID.Text);
}
else
{
TrainChangeRequestResponse.Text = "Train already exists in the system.";
}
}
}
private void Timer1_Tick(object sender, EventArgs e)
{
if (CommonStore.TrainSimMainDatabase != null)
{
if (CommonStore.TrainSimMainDatabase.InitializingTrainCollection.Count > 0 && cbTrainList.Text != "")
{
// huge code
}
}
}
public void VATCTimer_Tick(object sender, EventArgs e)
{
if (cbTrainList.Text != "")
{
\\ huge code

Related

C# asp email form - checkboxes return as null and won't send

protected void btnSend_Click(object sender, EventArgs e)
{
string _docValues = String.Join("<br>", Request.Form.GetValues("sendme"));
string _vidValues = String.Join("<br>", Request.Form.GetValues("vidsend"));
}
I have an email form with two groups of html checkboxes. I'm gathering the values and putting them into their own strings _docValues and _vidValues. However, this form only sends if I check a checkbox from each of the groups. If I don't select something from _docValues, it gives me an error:
Value cannot be null
How do I assign it a value, even if it's just a white space?
protected void btnSend_Click(object sender, EventArgs e)
{
if (Request.Form.GetValues("sendme") != null && Request.Form.GetValues("vidsend") != null)
{
string _docValues = String.Join("<br>", Request.Form.GetValues("sendme"));
string _vidValues = String.Join("<br>", Request.Form.GetValues("vidsend"));
}
else
{
string _docValues = "";
string _vidValues = "";
}
}
protected string _docValues = string.Empty;
protected string _vidValues = string.Empty;
protected void btnSend_Click(object sender, EventArgs e)
{
if (Request.Form["sendme"] != null)
_docValues = String.Join("<br>", Request.Form.GetValues("sendme"));
if (Request.Form["vidsend"] != null)
_vidValues = String.Join("<br>", Request.Form.GetValues("vidsend"));
}

Load JSON File to ListBox and TextBox C#

I'm working on a Windows Form Application. Textbox index can be saved and shown as in ListBox with this code:
private List<FunctionData> funcParamList = new List<FunctionData>();
...
private void addFuncButton_Click(object sender, EventArgs e)
{
FunctionData funcParams = new FunctionData();
funcParams.blabla1name = blabla1.Text;
funcParams.blabla2name = blabla2.Text;
...
if (funcParams.isValid())
{
funcParamList.Add(funcParams);
functionListBox.Items.Add(functionNameBox.Text);
}
Also I collect objects to TextBox again to edit (by clicking ListBox item) with the following code :
private void getParams(FunctionData data)
{
blabla1.Text = data.blabla1name;
blabla2.Text = data.blabla2name;
functionNameBox.Text = data.functionName;
return;
}
private void functionListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (functionListBox.SelectedItem == null) { return; }
foreach (var obj in funcParamList)
{
if (obj.functionName == functionListBox.SelectedItem.ToString())
{
getParams(obj);
}
}
}
And save them to file as JSON with:
private void saveFileButton_Click(object sender, EventArgs e)
{
fileName = fileNameBox.Text;
string jsonFunc = JsonConvert.SerializeObject(funcParamList);
System.IO.File.WriteAllText(#"<blablapath>\" + fileName + ".txt", jsonFunc);
}
There's 'functionName' object in JSON file that I can use it for showing on ListBox.
My question is: How can I load this file buy Native Load/Open File Dialog and show the objects in ListBox and can edit them again?
And here how I've tried to make it with the following code, but it doesn't work:
private void loadFileButton_Click(object sender, EventArgs e)
{
OpenFileDialog loadFileDialog = new OpenFileDialog();
...
if (loadFileDialog.ShowDialog() == DialogResult.OK)
{
string jsonFileName = loadFileDialog.FileName;
string jsonFile = File.ReadAllText(jsonFileName);
dynamic loadedFile = JsonConvert.DeserializeObject(jsonFile);
//if (functionListBox.SelectedItem == null) { return; }
foreach (var obj in loadedFile)
{
if (obj.functionName != null)
{
functionListBox.Items.Add(obj.functionName);
getParams(obj); // I get exception here
funcParamList.Add(loadedFile);
functionListBox.Refresh();
}
}
}
I've solved the problem by casting 'DeserializeObject' as List and it's done. Here the changes:
...
var loadedFile = JsonConvert.DeserializeObject<List<FunctionData>>(jsonFile);

How to get value from radiobutton to linkbox

I'm trying to get the value of radio button selection to my listbox, but the listbox always gets the same value even though selection was different. Please help, I am a newbie to coding, I couldn't find any answers on the net either..
Here are the codes I've written:
void rdbtnOne_CheckedChanged(object sender, EventArgs e)
{
if (rdbtnOne.Checked == true)
{
rdbtnOne.Text = "Men";
}
else
{
rdbtnOne.Text = "Women";
}
}
void btnOne_Click(object sender, EventArgs e)
{
lstOne.Items.Add(i + rdbtnOne.Text);
i++;
}
Ok, I have found the solution, FINALLY. The reason that my code did not work in the first place was because I tried to give value by equaling rdbtnOne.Text directly. Instead I created another value to equal it. All right here is how it worked for me:
string MenOrWomen;
void rdbtnTwo_CheckedChanged(object sender, EventArgs e)
{
if (rdbtnTwo.Checked.Equals(true))
{
MenOrWomen = "Women";
}
else
{
MenOrWomen = "Men";
}
}
void rdbtnOne_CheckedChanged(object sender, EventArgs e)
{
if (rdbtnOne.Checked.Equals(true))
{
MenOrWomen = "Men";
}
else
{
MenOrWomen = "Women";
}
}
int i = 1;
void btnOne_Click(object sender, EventArgs e)
{
lstOne.Items.Add(i + MenOrWomen);
i++;
}

C# combobox if statement + continue button incorporation

This code doesn't work. I don't know what to fix.
public sealed partial class Home : Page
{
public Home()
{
this.InitializeComponent();
ComboBox1.Items.Add("Hindiiiii");
}
string selection = null;
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
if (ComboBox1.SelectedIndex!=1)
{
selection = ComboBox1.SelectedItem.ToString();
}
}
private void Continue(object sender, RoutedEventArgs e)
{
if(selection != null)
{
if (selection == "Hindiiiii")
this.Frame.Navigate(typeof(MainPage));
else if (selection == "English")
this.Frame.Navigate(typeof(Home));
}
}
When a user selects Hindiiiii on the main screen and clicks continues he is not redirected to the next page (MainPage).
Let's say your main page looks like this:
You can store the selection in a variable:
string selection = null;
private void ComboBox1_SelectedIndex(object sender, EventArgs e)
{
if (ComboBox1.SelectedIndex!=-1)
{
selection = ComboBox1.SelectedItem.ToString();
}
}
Then in your click event you can pass parameters between your pages:
private void Continue(object sender, RoutedEventArgs e)
{
if(selection != null)
this.Frame.Navigate(typeof(SomePage), selection); //send the contents of the variable to another page
}
And let's say you had another page with a TextBox and a TextBlock:
In your other pages' OnNavigatedTo event, you can retrieve the parameters so you don't have to create a page for every selected language:
string selection = null;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
selection = e.Parameter.ToString();
languageTextBlock.Text = selection; //the textblox is now the selected language
//decide what the contents are based on the selection
if (selection == "English")
translation.Text = "Something in English";
else if (selection == "Hindi")
translation.Text = "Something in Hindi";
else if (selection == "German")
translation.Text = "Something in German";
//etc
}
When you go to the next page, this allows you to create your page based on the selected item. This image demonstrates this:
Alternatively, you can solve your problem by creating a page for every possible language:
private void Continue(object sender, RoutedEventArgs e)
{
if(selection != null)
{
if(selection == "English")
this.Frame.Navigate(typeof(EnglishPage));
else if(selection == "Hindi")
this.Frame.Navigate(typeof(HindiPage));
//and so on
}
}
I prefer to do it this way because it's a lot simpler.
Edit: I see Items in the property box but I'm not aware of how to use it to add combo box items. This is the way that I usually see it done:
Of course you'll need to replace MainPage with your page (if it's not already named MainPage).
Another edit:
If you added the items via the properties panel, you have to access the Content. Use this instead, if you want:
string selection = null;
private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ComboBox1.SelectedIndex != -1)
{
//selection = ComboBox1.SelectedItem.ToString();
selection = (ComboBox1.SelectedItem as ComboBoxItem).Content.ToString();
}
}

Windows Forms: 'Input string was not in a correct format' error

I am creating a webpage re-loader and I am trying to get number of reload using input from user but I am not able to get number of input from user.
I am trying to get user input in textBox2.Text, but I am having this error:
input string was not in a currect format
This error is in this line kkk = System.Int32.Parse(textBox2.Text);
please help me how to get user input properly in an int value.
this is my program code:
public partial class Form1 : Form
{
public int kkk;
public Form1()
{
InitializeComponent();
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (progressBar1.Value != kkk)
{
do
{
try
{
webBrowser1.Navigate(textBox1.Text);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
if(webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
progressBar1.Value = progressBar1.Value + 1;
}
}
MessageBox.Show("Loaded");
}
catch(Exception)
{
MessageBox.Show("failed");
}
}
while(progressBar1.Value !=kkk);
}
}
private void Form1_Load(object sender, EventArgs e)
{
kkk = System.Int32.Parse(textBox2.Text);
progressBar1.Maximum = kkk;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
In your form load event you take the contents of textbox2. text and assign it to kkk. But at that point nothing is inside textBox2 so it throws the error, and rightfully so since the textbox is empty how can it parse to an Int32 if it has no value?
You should assign the value of kkk at some later time during the process. You can always handle the exception before it happens:
int number;
bool result = Int32.TryParse(txtBox2.Text, out number);
if (result)
{
//good conversion you can use number
}
else
{
//not so good
}
But again you are doing this at form load event and I highly doubt there is anything in that textbox based on your code by the time the load event is finished.
The line:
kkk = System.Int32.Parse(textBox2.Text);
is giving an error maybe because it is an empty string which is unable to get parsed to integer. Change it to:
kkk = textBox2.Text.Trim();
if( kkk.Length > 0 ) {
try {
kkk = System.Int32.Parse(kkk);
}
catch { }
}

Categories

Resources