Passing data into a separate form [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a button that when pressed is currently outputting messages to a messagebox. Its not very tidy so I created a new form and placed a text box in there. While the 2nd form picks up the data it presents it in another messagebox rather then the textbox like intended. Help is appreciated.
Form2
public Form2(string strTextBox)
{
InitializeComponent();
textBox1.Text = strTextBox;
}
Form 1
private void SaveButton_Click(object sender, EventArgs e)
{
foreach (string error in errorSet)
{
Form2 frm = new Form2(error);
frm.Show();
}
}
There is some more logic in the button_click if it looks a little strange but its pretty redundant for the problem im having.
Thank you

You should loop over your errors collection and store them in a StringBuilder, then, show the Form2 only one time outside the loop (if there is at least an error).
Do not forget to make your textbox MultiLine=true, give it enough height and a vertical scroll bar.
private void SaveButton_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (string error in errorSet)
sb.AppendLine(error);
if(sb.Lenght> 0)
{
Form2 frm = new Form2(sb.ToString());
frm.Show();
}
}

Related

Windows Form: Display unique panel based on list selection [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Using a Windows Form I am trying to replicate the functionality of a "Tab Control" but instead of the panel selection being controlled by a tab selection it would be controlled by a list. Is there a built in way to produce this using neatly like the tab control or should I just check if a list value is equal to some value and if yes display panel else don't?
This is probably a dumb question so sorry for wasting anyone's time and thanks in advance!
Besides, you can also use tabpages in tabcontrol as "panel". Just hide the header via the code
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.Appearance = TabAppearance.FlatButtons;
tabControl1.ItemSize = new Size(0, 1);
tabControl1.SizeMode = TabSizeMode.Fixed;
foreach (TabPage tab in tabControl1.TabPages)
{
tab.Text = "";
}
}
Then select the tabpage like,
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
if(((ListBox)sender).SelectedItem.ToString() == "tabPage2")
{
tabControl1.SelectedTab = tabPage2;
}
//...
}

Loop for click event in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have multiple text boxes on a form and I want to display the name of the textbox when I click on it in a specific textbox. As I have 300 textboxes I dont want to create 300 click Events. Do I need some kind of loop?
Yes, exactly, iterate through all your Controls on your Form and if Control is a TextBox, subscribe to an event like this:
private void subScribeAllTextBoxClickEvents()
{
foreach(var ctrl in this.Controls)
{
var textBox = ctrl as TextBox;
if(textBox != null)
{
textBox.Click += textBox_Click;
}
}
}
private void textBox_Click(object sender, EventArgs e)
{
}
You could call subScribeAllTextBoxClickEvents method for example in your constructor.

Set a printer for app using a Dialog [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My application prints without showing the PrintDialog (and it need to do so) but I'd like to be able to set to which printer it should print so I added an appsetting in the app.config where it stores the printer name. what I want is a dialog where it shows all printer and the user should be able to choose a printer and it will save in the app.config (I could actually do it from the PrintDialog and get the chosen printer but the button says Print and I don't want to confuse the User...)
Thanks
edit
#methodMan Asked for code so I added My code
System.Windows.Forms.PrintDialog ps = new System.Windows.Forms.PrintDialog();
//set the selected printer in the dialog to the current printer
ps.PrinterSettings.PrinterName = MyApp.Properties.Settings.Default.ContinuesLabelPrinter;
var result = ps.ShowDialog();
if(result == System.Windows.Forms.DialogResult.OK)
{
MyApp.Properties.Settings.Default.ContinuesLabelPrinter = ps.PrinterSettings.PrinterName;
}
OK here is something that will work for you I think...Create a windows Form with a listbox and two buttons.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
listBox1.Items.Add(printer);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
}
In your code do this:
Form2 form2 = new Form2();
if(form2.Show() == System.Windows.Forms.DialogResult.OK)
{
MyApp.Properties.Settings.Default.ContinuesLabelPrinter = form2.listBox1.SelectedItem.ToString();
}
form2.Dispose(); // <-- this might not be necessary
You will need to make the listBox1 public for this to work.

How to prevent data duplication in a datagridview [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);
if (e.ColumnIndex==5)
{
form.ShowDialog();
}
}
I am trying to view the details of a movie when I press the view details button in the datagridview but for some reason I can't get it to work.
The place of the buttons in the datagridview is 5.
I'd show a ss but unfortunately I cant, yet.
The place of the buttons in the datagridview is 5
It means that the column is the fifth column?
If yes, don't forget that index in .Net are generally zero-based index. So it would be:
if (e.ColumnIndex==4)
Also, good remark from KyleMit, don't create an instance of MovieDetailsForm if you don't use it:
if (e.ColumnIndex==4)
{
MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);
form.ShowDialog();
}
Just to summarize what others have said and to help out your coding style...
private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns["colDetailButton"].DisplayIndex == e.ColumnIndex)
{
// my guess is you also need other data, like the movie's IMDB number
string imdbValue = dataGridView1.Rows[e.RowIndex].Cells["colImdbValue"].Value.ToString();
using (var form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read))
{
form.ImdbValue = imdbValue;
form.ShowDialog();
}
}
else
{
// Remove this debugging code once you get your code working
Console.WriteLine("ColumnIndex {0} was clicked." e.ColumnIndex);
}
}
See this answer as to how to How to handle click event in Button Column in Datagridview? for a good overview of what to do. So long as you only have a single button, you actually don't have to specify the column index at all, which makes your code less fragile to change. Although, Chris is right, that indexes are zero based so you'd need a ColumnIndex of 4 to get the 5th column. You also don't have to new up your form unless you actually want to show it, so I'd move the declaration into the if statement like this:
private void moviesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//make sure click not on header and column is type of ButtonColumn
if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == _
typeof(DataGridViewButtonColumn))
{
MovieDetailsForm form = new MovieDetailsForm(MovieDetailsForm.MovieViewMode.Read);
form.ShowDialog();
}
}

Navigation and memorising text box data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
The program has a panel which holds a text box and the panel has two buttons on each side.
Each button acts as a 'next' (>>) and 'previous' (<<) navigation. I want to be able to navigate to the next panel by clicking '>>' this will clear the text box. Then when I click '<<' I want to go back to the previous panel with the text box containing the data previously added. However I want to do this without having to create two panels on top of each other and setting the visibility to true or false (which I am able to do). I want to achieve this by using only the one panel so the process can be done an infinite number of times. I hope this is clear to understand if you require more information please let me know.
Here is an image of my interface to clarify things:
since you have the page number, why not just create a list (or use a dictionary with the page number as a key), then in the button handler for >> and << collect the text for the current page (and put it in the list or dictionary) and replace it with the text for the previous page (from the list or dictionary).
code could look something like this:
public partial class Form1 : Form
{
Dictionary<Decimal, String> TextInfo;
public Form1()
{
InitializeComponent();
TextInfo= new Dictionary<Decimal, String>();
}
private void Form1_Load(object sender, EventArgs e)
{
numPage.Value = 1;
}
private void bnForward_Click(object sender, EventArgs e)
{
if (TextInfo.ContainsKey(numPage.Value))
{
TextInfo[numPage.Value] = textBox1.Text;
}
else
{
TextInfo.Add(numPage.Value, textBox1.Text);
}
numPage.Value++;
if (TextInfo.ContainsKey(numPage.Value))
{
textBox1.Text = TextInfo[numPage.Value];
}
else
{
textBox1.Text = "";
}
}
private void bnBack_Click(object sender, EventArgs e)
{
if (numPage.Value == 1)
return;
if (TextInfo.ContainsKey(numPage.Value))
{
TextInfo[numPage.Value] = textBox1.Text;
}
else
{
TextInfo.Add(numPage.Value, textBox1.Text);
}
numPage.Value--;
if (TextInfo.ContainsKey(numPage.Value))
{
textBox1.Text = TextInfo[numPage.Value];
}
else
{
textBox1.Text = "";
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
}
}

Categories