Creating a find dialog - c#

So for this assignment in class I'm making a richtextbox editor in C# winforms, in this assignment I have to include a find function, but I can't seem to get the hang of it
EDIT: it closes every time I click the search button, but that's not what I want, I want it to close when the user closes the actual dialog via the X in the top right, the search button should just highlight the found string by using the .Find() method
Here's my code up until now:
private void zoekenToolStripMenuItem_Click(object sender, EventArgs e)
{
string searchValue = SearchDialog();
Search(searchValue);
}
public string SearchDialog()
{
Form findDialog = new Form();
findDialog.Width = 500;
findDialog.Height = 142;
findDialog.Text = "Zoeken";
Label textLabel = new Label() { Left = 10, Top = 20, Text = "Zoek naar:", Width = 100 };
TextBox inputBox = new TextBox() { Left = 150, Top = 20, Width = 300};
Button search = new Button() { Text = "Zoek", Left = 350, Width = 100, Top = 70 };
search.Click += (object sender, EventArgs e) => { findDialog.Close(); };
findDialog.Controls.Add(search);
findDialog.Controls.Add(textLabel);
findDialog.Controls.Add(inputBox);
findDialog.ShowDialog();
return (string)inputBox.Text;
}
void Search(string searchValue)
{
rtxtInhoud.Find(searchValue);
}
The part:
search.Click += (object sender, EventArgs e) => { findDialog.Close(); };
is what I'm really stuck on
Thanks in advance
EDIT: here's something that I tried to do, which didn't work
public string SearchDialog()
{
Form findDialog = new Form();
findDialog.Width = 500;
findDialog.Height = 142;
findDialog.Text = "Zoeken";
Label textLabel = new Label() { Left = 10, Top = 20, Text = "Zoek naar:", Width = 100 };
TextBox inputBox = new TextBox() { Left = 150, Top = 20, Width = 300};
Button search = new Button() { Text = "Zoek", Left = 350, Width = 100, Top = 70 };
Button findNext = new Button() { Text = "Volgende", Left 250, Width = 100, Top = 70};
search.Click += (object sender, EventArgs e) => { rtxtInhoud.Find(inputBox.Text); };
findDialog.Controls.Add(search);
findDialog.Controls.Add(textLabel);
findDialog.Controls.Add(inputBox);
findDialog.ShowDialog();
return (string)inputBox.Text;
}
This waits for the Dialog to be closed before highlighting the found string. Which is NOT what I want, I want it to keep the Dialog open, but still highlight the text

it closes every time I click the search button
Yes, that's because you added an event handler to the search button that closes the form:
search.Click += (object sender, EventArgs e) => { findDialog.Close(); };
but that's not what I want, I want it to [...]
Then write the code in that handler such that it does what you actually want it to do, instead of having it close the form.

In your main form code, create a method that searches your text and highlights a found string. Then hook this method up to your search dialog button like the sample below. Also, I think you should call form.Show() instead of .ShowDialog(), so the other form can respond to input.
private void HighlightString(string stringToHighlight)
{
// Code here to search your text and highlight a string.
}
private void SearchDialog()
{
var findDialog = new Form {Width = 500, Height = 142, Text = "Zoeken"};
var textLabel = new Label() {Left = 10, Top = 20, Text = "Zoek naar:", Width = 100};
var inputBox = new TextBox() {Left = 150, Top = 20, Width = 300};
var search = new Button() {Text = "Zoek", Left = 350, Width = 100, Top = 70};
search.Click += (object sender, EventArgs e) => HighlightString(inputBox.Text);
findDialog.Controls.Add(search);
findDialog.Controls.Add(textLabel);
findDialog.Controls.Add(inputBox);
findDialog.Show();
}

Use this code:
Application.Exit();

Related

C# won't allow me to add event handler to a button into a form class

This is a C# web form project I am starting with after a long time away from IDE coding...
I am trying to make a simple custom dialog box class. This is my code.
public static class Dialogo
{
public static int show ()
{
Form dialogo = new Form();
dialogo.Width = 300;
dialogo.Height = 300;
Button btnSim = new Button() { Text = "Sim", Left = 30, Width = 100 };
Button btnNao = new Button() { Text = "Não", Left = 150, Width = 100 };
dialogo.Controls.Add(btnSim);
dialogo.Controls.Add(btnNao);
dialogo.ShowDialog();
// the following two lines are the problematic ones
btnSim += new EventHandler(btnSim_Click);
btnNao += new EventHandler(btnNao_Click);
return -1;
}
}
It's underlining the text within parenthesis and the message says:
The name btnSim_Click' does not exist in the current context
The problem is that I tried to add the following in my code but it doesn't let me put it anywhere (it always says that is something wrong):
private int btnNao_Click (object sender, EventArgs e)
{
return 0;
}
private int btnSim_Click (object sender, EventArgs e)
{
return 1;
}
My objective is that each of the both buttons btnSim and btnNao return a different value (say 1 and 0).
What am I doing wrong?
EventHandler is a delegate for a method that returns void.
Your methods return int.
Try something like this:
public static int show()
{
int returnValue = -1;
using (Form dialogo = new Form())
{
dialogo.Width = 300;
dialogo.Height = 300;
Button btnSim = new Button() { Text = "Sim", Left = 30, Width = 100 };
Button btnNao = new Button() { Text = "Não", Left = 150, Width = 100 };
dialogo.Controls.Add(btnSim);
dialogo.Controls.Add(btnNao);
btnSim.Click += (s, e) => { returnValue = 0; dialogo.DialogResult = DialogResult.OK; };
btnNao.Click += (s, e) => { returnValue = 1; dialogo.DialogResult = DialogResult.OK; };
dialogo.Disposed += (s, e) =>
{
btnSim?.Dispose();
btnSim = null;
btnNao?.Dispose();
btnNao = null;
};
dialogo.ShowDialog();
}
return returnValue;
}

Dynamic radiobuttons, how do i connect them do an event?

I have picture repo, foreach picture i want a radiobutton in my wrappanel. I want to connect all of these radiobuttons to an event, so when one is checked, all the properties of the pictures shows up on screen.
Problem is for some reason i cannot access the events members of radiobutton when creating them.
I've tried google, couldnt find same problem
public void UpdatePictures(PictureRepo pictureRepo)
{
foreach (var picture in pictureRepo.RepoCollection)
{
WP_mainWrapPanel.Children.Add(new RadioButton
{
Margin = new Thickness(2, 10, 2, 10),
Height = 100,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Top,
Content = new Image { Source = new BitmapImage(new Uri(picture.PictureLink, UriKind.Relative)) },
Name = picture.Name.ToString(),
});
}
}
Radiobuttons have an event for if the button is checked, i cannot access it for some reason.
Try this:
private void SetupRadioButton()
{
RadioButton radio1 = new RadioButton
{
Text = "Your Properties Here",
};
radio1.CheckedChanged += Radio1_CheckedChanged;
}
private void Radio1_CheckedChanged(object sender, EventArgs e)
{
throw new NotImplementedException();
}
You need to create your button, and keep a reference to it.Then you can add the event handler.
var btn = new RadioButton
{
Margin = new Thickness(2, 10, 2, 10),
Height = 100,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Top,
Content = new Image { Source = new BitmapImage(new Uri(picture.PictureLink, UriKind.Relative)) },
Name = picture.Name.ToString(),
};
WP_mainWrapPanel.Children.Add(btn);
btn.Checked += btn_Checked;
The event definition looks something like this
private static void btn_Checked(object sender, RoutedEventArgs e)
{
//do stuff
}

Switching to dynamically created TabPage on button click

I'm practicing my coding and am trying to create a till by Dynamically prefill the items that are stored in a text file. I can dynamically create Tabs, Buttons and create menu buttons based on the categories of items. Where I'm struggling is I'm trying to switch the Tab on a button click. The Tabs are given a Name, which is the category ID, and the Text displays the category. In the event that trys to switch the tab I get the following error:
Error CS0266 Cannot implicitly convert type 'object' to 'System.Windows.Forms.TabPage'. An explicit conversion exists (are you missing a cast?)
I'm presuming that I need to Create a tab page or something, but I can't find out how to do this. Been on it for hours! Here's my code....
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
string[] loadedFile = File.ReadAllLines(#"h:\shopItemsV2.txt");
foreach (string line in loadedFile)
{
// Split string and create required variables
string[] newBtnData = line.Split(',');
int catID = int.Parse(newBtnData[0]);
string cat = newBtnData[1];
string item = newBtnData[2];
double price = double.Parse(newBtnData[3]);
// Create tab if needed
if (tabControl1.TabCount < catID)
{
TabPage tp = new TabPage()
{
Text = cat,
Name = catID.ToString()
};
tabControl1.TabPages.Add(tp);
// Add FlowLayoutPanel with unique name
FlowLayoutPanel fp = new FlowLayoutPanel()
{
Name = "fp" + catID.ToString(),
Dock = DockStyle.Fill
};
// Add FlowLayoutPanel to correct Tab
tabControl1.TabPages[catID-1].Controls.Add(fp);
// Create Button for menu
Button newMenuBtn = new Button()
{
Name = cat + "Btn",
Tag = catID,
Width = 100,
Height = 50,
Text = cat,
};
newMenuBtn.Click += SwitchTab;
menuFP.Controls.Add(newMenuBtn);
}
// Create Button
Button newBtn = new Button()
{
Name = item,
Tag = price,
Width = 100,
Height = 100,
Text = item,
};
newBtn.Click += AddItem;
//Add button to correct tab
foreach (TabPage tabP in tabControl1.TabPages)
{
if (tabP.Name == catID.ToString())
{
Control fp = this.Controls.Find("fp"+catID, true).First();
fp.Controls.Add(newBtn);
}
}
}
}
private void SwitchTab(object sender, EventArgs e)
{
// Create button, switch to required Tab
// Tabs named the same as Tag on the button
Button clickedBtn = sender as Button;
tabControl1.SelectedTab = clickedBtn.Tag;
}
}
Any help would be greatly appreciated.
You can store anything in the Tag() property of your button. With that in mind, store a reference to your TabPage!
Change:
// Create Button for menu
Button newMenuBtn = new Button()
{
Name = cat + "Btn",
Tag = catID,
Width = 100,
Height = 50,
Text = cat,
};
To:
// Create Button for menu
Button newMenuBtn = new Button()
{
Name = cat + "Btn",
Tag = tp; // store the reference to the TabPage you created
Width = 100,
Height = 50,
Text = cat,
};
Then the suggestion by Uwe Keim should work:
tabControl1.SelectedTab = clickedBtn.Tag as TabPage;
private void AddNewPr_Click(object sender, EventArgs e)
{
TabPage tab = new TabPage();
_list = new ListBox();
_list2 = new ListBox();
PictureBox picBox = new PictureBox();
picBox.Click = picBox_Click;
//More stuff here
//Add the controls
tabControl1.Controls.Add(tab);
tab.Controls.Add(list);
tab.Controls.Add(list2);
tab.Controls.Add(pictureBox);
}

C# Forms How to get value from Prompt Dialog [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a class called "AddChips" it's basically Prompt Dialog with some buttons and a TextBox the user enter's some data in it and i want to get the data that he just entered and send it back to some other class let's say "Form1" and give the data to integer called Chips
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 150;
prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
Button leaveApp = new Button() { Text = "No", Left = 250, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { new Form1() { Chips = int.Parse(textBox.Text) }; };
leaveApp.Click += (sender, e) => { Application.Exit(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(leaveApp);
prompt.Controls.Add(textLabel);
textLabel.Width = 300;
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
I have used on prompt form Like this:
public string ValueIWant { get; set; }
private void btnSetValueIWant_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtbxValue.Text))
{
ValueIWant= txtbxValue.Text;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
Close();
}
}
and on the other form i get Value:
frmValue f= new frmValue ();
if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string ValueIWantFromProptForm=f.ValueIWant ;
}
It Works for me.

Why isn't my label displaying all the text?

public int dialog()
{
Form prompt = new Form(); // creates form
//dimensions
prompt.Width = 300;
prompt.Height = 125;
prompt.Text = "Adding Rows"; // title
Label amountLabel = new Label() { Left = 50, Top = 0, Text = "Enter a number from 1-50" }; // label for prompt
amountLabel.Font = new Font("Microsoft Sans Serif", 9.75F);
TextBox value = new TextBox() { Left = 50, Top = 25, Width = prompt.Width / 2 }; // text box for prompt
Button confirmation = new Button() { Text = "Ok", Left = prompt.Width / 2 - 50, Width = 50, Top = 50 }; // ok button
confirmation.Click += (sender, e) => { prompt.Close(); }; // if clicked it will close
prompt.AcceptButton = confirmation; // enter
prompt.KeyPreview = true;
prompt.KeyDown += (sender, e) =>
{
if (e.KeyCode == Keys.Escape) prompt.DialogResult = DialogResult.Cancel; // user presses ESC key to close
};
// adding the controls
prompt.Controls.Add(value);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(amountLabel);
prompt.ShowDialog();
// returning and checking if int block
int num;
Int32.TryParse(value.Text, out num);
return num;
}
This is the full code. The short version of the code is:
Label amountLabel = new Label() { Left = 50, Top = 0, Text = "Enter a number from 1-50" };
prompt.Controls.Add(amountLabel);
The problem is that it will only display up to "Enter a number". It won't display the full text for some reason. I tried shorter "E" and it worked. I even tried "Enter a number from" but it still didn't fully display.
You could enable AutoSize, which is set to true by default if you create it via the designer:
Label amountLabel
= new Label { AutoSize = true, Left = 50, Top = 0, Text = "Enter a number from 1-50" };
Set the width of the label:
Label amountLabel = new Label() { Left = 75, Top = 0, Width = 1000, Text = "Enter a number from 1-50" };
Don't make the width that wide, though. I just wanted to make sure the text fits without testing different values.
I was surprised that it didn't adjust the width automatically.
Set the Label's AutoSize property to true.
Label amountLabel = new Label() { AutoSize=true, Left = 50, Top = 0, Text = "Enter a number from 1-50" };

Categories